宏拼接期间"error: missing parameter type"



编辑:我发现了我的错误-递归情况的准引号中有一个错误,导致它返回格式错误的序列


我正在尝试创建一个宏,该宏将把事例类T变成updateMap: Map[String, (play.api.libs.json.JsValue) => Try[(T) => T]](如何使用scala宏创建函数对象(创建Map[String,(T)=>T]),其中映射的键是case类的字段名-其思想是,给定JsObject("key" -> JsValue),我们可以使用keyupdateMap检索适当的更新方法,然后使用JsValue应用更新。我在非递归的情况下进行了这项工作,即给定一个没有任何其他事例类作为字段的事例类。但是,我想扩展这个宏,以便它可以为包含其他事例类的事例类生成updateMap,例如

case class Inner(innerStr: String)
case class Outer(outerStr: String, inner: Inner)
updateMap[Outer] = {
// updateMap[Inner]
val innerMap = Map("innerStr" -> (str: String) => 
Try { (i: Inner) => i.copy(innerStr = str) } )
// updateMap[Outer]
Map("outerStr" -> (str: String) => 
Try { (o: Outer) => o.copy(outerStr = str) },
"inner.innerStr" -> (str: String) => 
Try { (o: Outer) => innerMap.get("innerStr").get(str).flatMap(lens => o.copy(inner = lens(o.inner))) })}

换言之,给定updateMap[Outer],我将能够直接更新对象的outerStr字段,或者在任何一种情况下,我都能够更新对象的inner.innerStr字段,以获得Try[Outer]

代码对于非递归情况(copyMapRec[Inner]())是正确的,但递归情况(copyMapRec[Outer]())给了我一个"错误:缺少参数类型"的错误——我假设我要么需要在某个地方提供一个隐式参数,要么我对拼接有根本的误解。

下面的代码使用(String) => Try[(T) => T]而不是(JsValue) => Try[(T) => T],这样我就不需要将播放框架导入到REPL中。我使用隐式转换将JsValue(或String)转换为适当的类型(这发生在基本大小写准引号中的val x: $fieldType = str行中;如果没有适当的隐式转换,则会出现编译器错误)。

import scala.language.experimental.macros
def copyMapImplRec[T: c.WeakTypeTag](c: scala.reflect.macros.Context)(blacklist: c.Expr[String]*): c.Expr[Map[String, (String) => scala.util.Try[(T) => T]]] = {
import c.universe._
// Fields that should be omitted from the map
val blacklistList: Seq[String] = blacklist.map(e => c.eval(c.Expr[String](c.resetAllAttrs(e.tree))))
def rec(tpe: Type): c.Expr[Map[String, (String) => scala.util.Try[(T) => T]]] = {
val typeName = tpe.typeSymbol.name.decoded
// All fields in the case class's primary constructor, minus the blacklisted fields
val fields = tpe.declarations.collectFirst {
case m: MethodSymbol if m.isPrimaryConstructor => m
}.get.paramss.head.filterNot(field => blacklistList.contains(typeName + "." + field.name.decoded))
// Split the fields into case classes and non case classes
val recursive = fields.filter(f => f.typeSignature.typeSymbol.isClass && f.typeSignature.typeSymbol.asClass.isCaseClass)
val nonRecursive = fields.filterNot(f => f.typeSignature.typeSymbol.isClass && f.typeSignature.typeSymbol.asClass.isCaseClass)
val recursiveMethods = recursive.map {
field => {
val fieldName = field.name
val fieldNameDecoded = fieldName.decoded
// Get the c.Expr[Map] for this case class
val map = rec(field.typeSignature)
// Construct an "inner.innerStr -> " seq of tuples from the "innerStr -> " seq of tuples
q"""{
val innerMap = $map
innerMap.toSeq.map(tuple => ($fieldNameDecoded + "." + tuple._1) -> {
(str: String) => {
val innerUpdate = tuple._2(str)
innerUpdate.map(innerUpdate => (outer: $tpe) => outer.copy($fieldName = innerUpdate(outer.$fieldName)))
}
})}"""
}
}
val nonRecursiveMethods = nonRecursive.map {
field => {
val fieldName = field.name
val fieldNameDecoded = fieldName.decoded
val fieldType = field.typeSignature
val fieldTypeName = fieldType.toString
q"""{
$fieldNameDecoded -> {
(str: String) => scala.util.Try {
val x: $fieldType = str
(t: $tpe) => t.copy($fieldName = x)
}.recoverWith {
case e: Exception => scala.util.Failure(new IllegalArgumentException("Failed to parse " + str + " as " + $typeName + "." + $fieldNameDecoded + ": " + $fieldTypeName))
}
}}"""
}
}
// Splice in all of the sequences of tuples, flatten the sequence, and construct a map
c.Expr[Map[String, (String) => scala.util.Try[(T) => T]]] {
q"""{ Map((List(..$recursiveMethods).flatten ++ List(..$nonRecursiveMethods)):_*) }"""
}
}
rec(weakTypeOf[T])
}
def copyMapRec[T](blacklist: String*) = macro copyMapImplRec[T]

我解决了这个问题-最初在我的recursiveMethods准引号中有innerMap.toSeq(...)而不是innerMap.toSeq.map(...)-我忽略了在REPL第一个中测试代码

最新更新