Created
August 20, 2014 19:47
-
-
Save tstone/9c925076b51b70435d31 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package services.config | |
import scala.concurrent.Future | |
import scala.reflect.runtime.universe._ | |
trait ReflectedAttributes { | |
protected val instanceMirror = runtimeMirror(this.getClass.getClassLoader).reflect(this) | |
protected val members = instanceMirror.symbol.typeSignature.members | |
protected def fields = members | |
.filter(_.typeSignature <:< typeOf[Future[_]]) | |
.collect { case m: MethodSymbol if m.isPublic && m.isGetter && !m.isVal && !m.isAbstract => m } | |
def attributes[A: TypeTag]: Map[String, String] = | |
fields.map { attr => attr.name.toString -> noramlizeType(attr.returnType.toString) }.toMap | |
private def noramlizeType(t: String): String = t.toLowerCase.replace("scala.", "") match { | |
case "int" => "number" | |
case "string" => "text" | |
case "boolean" => "boolean" | |
case opt if opt.startsWith("option[") => noramlizeType(opt.substring(7, opt.length - 1)) | |
case x => ??? // TODO: Recursivley examine type and add to "@types:" | |
} | |
} | |
class DynamicConfig extends AttributeFetcher { | |
def foo = get[String]("foo") | |
} | |
trait ConfigReader[A] { } | |
class Fetcher { } | |
trait AttributeFetcher extends ReflectedAttributes { | |
def get[A](key: String)(implicit fetcher: Fetcher, reader: ConfigReader[A]): Future[A] = ??? | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment