Created
May 31, 2016 02:57
-
-
Save BlackPrincess/6a330033fae44b57759efb074dd7cd57 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
object autoCreateParam { | |
def apply_impl[A, B: c.WeakTypeTag](c: Context)(columnName: c.Expr[ColumnName[A]], parameter: c.Expr[B]) = { | |
import c.universe._ | |
val B = weakTypeTag[B].tpe | |
val constructor = B.decls.collectFirst { case m: MethodSymbol if m.isPrimaryConstructor => m }.getOrElse { | |
c.abort(c.enclosingPosition, s"Could not find the primary constructor for $B. type $B must be a class or case class, not trait or type parameter") | |
} | |
val paramTuple = constructor.paramLists.head.map { field => | |
val name = field.name.decodedName.toString | |
field.name.toTermName | |
q"$columnName.column($name) -> $parameter.${field.name.toTermName}" | |
} | |
c.Expr[Seq[(SQLSyntax, ParameterBinder)]](q"Seq(..$paramTuple)") | |
} | |
def apply[A, B](columnName: ColumnName[A], parameter: B): Seq[(SQLSyntax, ParameterBinder)] = macro apply_impl[A, B] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment