dad*_*n89 5 annotations scala shapeless
我想将一些函数应用于case类中的字段。使用Annot注释必须“转换”的字段。我正在尝试使用Everywhere和Poly1。目前,我可以将转换功能应用于所有字段,但是无法“传递”有关带注释字段的信息。
import shapeless._
object Test {
case class Annot(
fieldType: String
) extends scala.annotation.StaticAnnotation
case class Address(
street: String,
isCapital: Boolean
)
case class Person(
@Annot("TypeA") lastName: String,
firstName: String,
isTall: Boolean,
address: Address
)
def transformationMethod(element: String, fieldType: String): String = ???
object Transformer extends Poly1 {
implicit def someStringCase: Case.Aux[String, String] = at {
case e => {
transformationMethod(e, "Name")
}
}
//other methods ..
}
def traverseAndModify[T](
expr: T
)(implicit e: Everywhere[Transformer.type, T] { type Result = T }): T = e(expr)
//Usage
val person = Person("LASTName", "FIRSTName", true, Address("My home address", true))
val modified : Person = traverseAndModify[Person](person)
}
Run Code Online (Sandbox Code Playgroud)
我想知道如何修改“无处不在”以将注释也传递给poly函数。所以我想在保利
object AnnotatedTransformer extends Poly1 {
implicit def someStringCase: Case.Aux[(String, Some[Annot]), String] = at {
case (e, Some(Annot(fieldType))) => {
transformationMethod(e, fieldType)
}
}
}
Run Code Online (Sandbox Code Playgroud)