我正在尝试按案例类参数生成语句。
case class f(a:String, b:String){
val statement = Macroses.st[f]
}
object MacrosTest {
def start() {
val result = f("1", "2").statement
}
}
结果必须是 INSERT INTO tableName(a, b) VALUES ('1', '2');
这是我的宏
def st[T]:Insert = macro st_impl[T]
def st_impl[A : c.WeakTypeTag](c: Context): c.Expr[Insert] = {
import c.universe._
val methods: List[String] = c.weakTypeOf[A].typeSymbol.
typeSignature.decls.toList.filterNot(_.isMethod)
.map(_.name.toString)
c.Expr[Insert](q"""QueryBuilder.insertInto("tableName")${methods.map(v => s""".value("$v", $v)""").mkString}""")
}
我有例外:
c.universe.Name expected but c.universe.Tree found
如果我使用这个 quasiquote 效果很好。
(q"""QueryBuilder.insertInto("tableName").value("a", a).value("b", b)""")