Created
May 26, 2016 06:14
Star
You must be signed in to star a gist
the description for this gist
This file contains 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
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