Skip to content

Instantly share code, notes, and snippets.

@tstone
Created August 20, 2014 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tstone/9c925076b51b70435d31 to your computer and use it in GitHub Desktop.
Save tstone/9c925076b51b70435d31 to your computer and use it in GitHub Desktop.
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