I would like injecting a method 'toCSV' to parse a class into CSV String. My function take two paramters :
My macro :
class model extends scala.annotation.StaticAnnotation {
inline def apply(defn: Any): Any = meta {
defn match {
case cls @ Defn.Class(_, _, _, Ctor.Primary(_, _, paramss), template) =>
def createToCSV(paramss: Seq[Seq[Term.Param]]): Defn.Def = {
val names = paramss.flatten.map(param => param.name.syntax)
val fields = paramss.map(_.map(param => Term.Name(param.name.value)))
q"""def toCSV =
CSVTools.toCSV(Seq(...$names), Seq(Seq(...$fields)))"""
}
val toCSVImpl = createToCSV(paramss)
val templateStats: Seq[Stat] = toCSVImpl +: template.stats.getOrElse(Nil)
cls.copy(templ = template.copy(stats = Some(templateStats)))
case _ =>
println(defn.structure)
abort("@model must annotate a class.")
}
}
}
The variable Term is a Seq[String] and the quasiquotes syntax only accept a Term. Hence, the error below occured :
[error] /home/xxxxx/intellij/workspace/heroes-scala-meta/macros/src/main/scala/examples/model.scala:18: type mismatch when unquoting;
[error] found : scala.collection.immutable.Seq[String]
[error] required: scala.collection.immutable.Seq[scala.collection.immutable.Seq[scala.meta.Term.Arg]]
[error] CSVTools.toCSV(Seq(...$names), Seq(Seq(...$fields)))"""
[error] ^
[error] one error found
[error] (macros/compile:compileIncremental) Compilation failed
Do you have a solution ?
Thank in advance,
This is probably a bit hacky but it seems to work: explicitly map your names
to string literals
class model extends scala.annotation.StaticAnnotation {
inline def apply(defn: Any): Any = meta {
defn match {
case cls@Defn.Class(_, _, _, Ctor.Primary(_, _, paramss), template) =>
def createToCSV(paramss: Seq[Seq[Term.Param]]): Defn.Def = {
val names: Seq[String] = paramss.flatten.map(param => param.name.syntax)
val fields = paramss.map(_.map(param => Term.Name(param.name.value)))
val nameLiterals: Seq[Lit.String] = names.map(n => q"$n".asInstanceOf[Lit.String])
q"""def toCSV =
CSVTools.toCSV(scala.collection.immutable.Seq(..$nameLiterals),
scala.collection.immutable.Seq(scala.collection.immutable.Seq(...$fields)))"""
}
val toCSVImpl = createToCSV(paramss)
val templateStats: Seq[Stat] = toCSVImpl +: template.stats.getOrElse(Nil)
cls.copy(templ = template.copy(stats = Some(templateStats)))
case _ =>
println(defn.structure)
abort("@model must annotate a class.")
}
}
}