Skip to content

Instantly share code, notes, and snippets.

Created May 26, 2016 06:14
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save anonymous/69ac6fed2f8cc649df4e6d1732a516ce to your computer and use it in GitHub Desktop.
the description for this gist
object bindable {
def bindableImpl[A: c.WeakTypeTag, B: c.WeakTypeTag](c: whitebox.Context): c.Expr[PathBindable[A]] = {
import c.universe._
val className = weakTypeOf[A].typeSymbol.name.toTypeName
val simpleType = weakTypeOf[B].typeSymbol.name.toTypeName
val bindable = q"""
import play.api.mvc.PathBindable
new PathBindable[$className] {
val binder = implicitly[PathBindable[$simpleType]]
override def bind(key: String, value: String): Either[String, $className] = binder.bind(key, value).right.map(a => ${className.toTermName}(a))
override def unbind(key: String, value: $className): String = binder.unbind(key, value.value)
}
"""
c.Expr(bindable)
}
}
object json {
def formatImpl[A: c.WeakTypeTag, B: c.WeakTypeTag](c: whitebox.Context): c.Expr[Format[A]] = {
import c.universe._
val valueType = weakTypeOf[A].typeSymbol.name.toTypeName
val simpleType = weakTypeOf[B].typeSymbol.name.toTypeName
val format = q"""
import play.api.libs.json._
Format(
JsPath.read[$simpleType].map(s => ${valueType.toTermName}(s)),
new Writes[$valueType] { def writes(o: $valueType) = Json.toJson(o.value) }
)
"""
c.Expr(format)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment