I am trying to create a macro annotation but I need to pass it parameters.
class ToPojo(param1: String, param2: String) extends StaticAnnotation {
inline def apply(defn: Any): Any = meta {
...
}
}
which is used as
@ToPojo("p1", "p2")
case class Entity(a: Int, m: Map[Long, Boolean])
The problem with above code is that the Entity
gets to the apply
as defn
already with the annotation stripped - hence I can not get the parameters from there. Also the param1
and param2
fields are not accessible from the apply
method since it gets inlined.
Could you please point me to the easiest way to overcome this using scala meta? I thought about using two annotations
@ToPojo
@ToPojoParams("p1", "p2")
case class Entity(a: Int, m: Map[Long, Boolean])
But thats hacky and ugly.
Thanks a lot
As described in the ScalaMeta Paradise description under How do I pass an argument to the macro annotation? section
Note that this code is a bit naive and doesn't handle named arguments. If you have many arguments, writing all possible combinations of usual and named arguments is boring. So you may try something like this: