Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save BlackPrincess/6a330033fae44b57759efb074dd7cd57 to your computer and use it in GitHub Desktop.
Save BlackPrincess/6a330033fae44b57759efb074dd7cd57 to your computer and use it in GitHub Desktop.
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