将地图转换为嵌套案例类

时间:2018-09-18 16:38:19

标签: scala scala-reflect

我想将Map[String, Any]转换为给定的case类,并且该地图可以是嵌套地图。例如,personDataMap应该转换为Person("evan",24,Address(15213,"5000 Forbes Ave"))

case class Address(zip: Int, name: String)
case class Person(name: String, age: Int, addr: Address)
val addrDataMap = Map("zip" -> 15213, "name" -> "5000 Forbes Ave")
val personDataMap = Map("name" -> "evan", "age" -> 24, "addr" -> addrDataMap)

Map[String, Any]转换为非嵌套案例类的代码可以在下面找到:

import com.twitter.util.{Return, Try}
import scala.reflect._
import scala.reflect.runtime.universe._

// Magic from https://stackoverflow.com/a/24100624/7550592
// Convert a Map[String, _] to a given case class CC
class CaseClassDataMapConverter[CC <: Product] {
    def fromMap(dataMap: Map[String, Any])(implicit tt: TypeTag[CC]): Try[CC] = {
        val tClassSymbol = typeOf[CC].typeSymbol.asClass
        val rm = typeTag[CC].mirror
        val classMirror = rm.reflectClass(tClassSymbol)
        val constructor = typeOf[CC].decl(termNames.CONSTRUCTOR).asMethod
        val constructorMirror = classMirror.reflectConstructor(constructor)

        val constructorTries = constructor.paramLists.flatten.map { param: Symbol =>
            val paramName = param.name.toString

            val tryResult: Try[Any] = 
                if (param.typeSignature <:< typeOf[Option[Any]]) {
                    Return(dataMap.get(paramName))
                }
                else {
                    Try(dataMap.getOrElse(paramName, new Exception("Param " + paramName + " not found")))
                }
            println("paramName = " + paramName + ", result = " + tryResult + ", type = " + param.typeSignature)
            tryResult
        }

        Try.collect(constructorTries).flatMap { constructorArgs =>
            Try(constructorMirror(constructorArgs: _*).asInstanceOf[CC])
        }
    }
}

case class Address(zip: Int, name: String)
case class Person(name: String, age: Int, addr: Address)

val converter = new CaseClassDataMapConverter[Person]()

val addrDataMap = Map("zip" -> 15213, "name" -> "5000 Forbes Ave")
val addrTry = fromMap[Address](addrDataMap) // Return(Address(15213,"5000 Forbes Ave"))
val addr = addrTry.get
val personDataMap = Map("name" -> "evan", "age" -> 24, "addr" -> addr)
val personTry = converter.fromMap(personDataMap) // Return(Person(evan,24,Address(15213,5000 Forbes Ave)))
val person = personTry.get

要处理嵌套的案例类,请检查param.typeSignature。如果它是案例类(Product),则我首先将其值(即嵌套的Map)转换为案例类。我添加了一个else-if分支,并将personDataMap.addr更改为使用Map而不是已经创建的case类:

import com.twitter.util.{Return, Try}
import scala.reflect._
import scala.reflect.runtime.universe._

// Magic from https://stackoverflow.com/a/24100624/7550592
// Convert a Map[String, _] to a given case class CC
class CaseClassDataMapConverter[CC <: Product] {
    def fromMap(dataMap: Map[String, Any])(implicit tt: TypeTag[CC]): Try[CC] = {
        val tClassSymbol = typeOf[CC].typeSymbol.asClass
        val rm = typeTag[CC].mirror
        val classMirror = rm.reflectClass(tClassSymbol)
        val constructor = typeOf[CC].decl(termNames.CONSTRUCTOR).asMethod
        val constructorMirror = classMirror.reflectConstructor(constructor)

        val constructorTries = constructor.paramLists.flatten.map { param: Symbol =>
            val paramName = param.name.toString

            val tryResult: Try[Any] = 
                if (param.typeSignature <:< typeOf[Option[Any]]) {
                    Return(dataMap.get(paramName))
                }
                // Need to deal with: type=Address, result=Map(...)
                else if (param.typeSignature <:< typeOf[Product]) {
                    val nestedDataMap = dataMap.getOrElse(paramName, new Exception("Param " + paramName + " not found"))
                    val nestedConverter = new CaseClassDataMapConverter[param]()
                    nestedConverter.fromMap(nestedDataMap)
                }
                else {
                    Try(dataMap.getOrElse(paramName, new Exception("Param " + paramName + " not found")))
                }
            println("paramName = " + paramName + ", result = " + tryResult + ", type = " + param.typeSignature)
            tryResult
        }

        Try.collect(constructorTries).flatMap { constructorArgs =>
            Try(constructorMirror(constructorArgs: _*).asInstanceOf[CC])
        }
    }
}

case class Address(zip: Int, name: String)
case class Person(name: String, age: Int, addr: Address)

val converter = new CaseClassDataMapConverter[Person]()

val addrDataMap = Map("zip" -> 15213, "name" -> "5000 Forbes Ave")
val personDataMap = Map("name" -> "evan", "age" -> 24, "addr" -> addrDataMap)
val personTry = converter.fromMap(personDataMap) // Return(Person(evan,24,Address(15213,5000 Forbes Ave)))
val person = personTry.get

不足为奇,它抱怨:

error: not found: type param
       val nestedConverter = new CaseClassDataMapConverter[param]()

我的问题:在这种情况下是否可以使用类型参数CaseClassDataMapConverter获得一个新的Address?如果是这样,该怎么办?谢谢!

仅供参考:运行环境是scala控制台。

1 个答案:

答案 0 :(得分:0)

您可以摆脱类型参数,只需将TypeTag作为显式参数传递给.fromMap

 class CaseClassDataMapConverter {
    private def fromMap(dataMap: Map[String, Any],  tt: TypeTag[_]): Try[Any] = {   
      ...
    }
 }

 object CaseClassDataMapConverter {
    def fromMap[CC <: Product : TypeTag](dataMap: Map[String, Any]) = 
       new CaseClassDataMapConverter()
         .fromMap(data, typeTag[CC])
         .asInstanceOf[Try[CC]]
 }