Scala: convert map to case class

前端 未结 6 2044
说谎
说谎 2020-12-01 01:53

Let\'s say I have this example case class

case class Test(key1: Int, key2: String, key3: String)

And I have a map

myMap = M         


        
相关标签:
6条回答
  • 2020-12-01 02:06

    Here is an alternative non-boilerplate method that uses Scala reflection (Scala 2.10 and above) and doesn't require a separately compiled module:

    import org.specs2.mutable.Specification
    import scala.reflect._
    import scala.reflect.runtime.universe._
    
    case class Test(t: String, ot: Option[String])
    
    package object ccFromMap {
      def fromMap[T: TypeTag: ClassTag](m: Map[String,_]) = {
        val rm = runtimeMirror(classTag[T].runtimeClass.getClassLoader)
        val classTest = typeOf[T].typeSymbol.asClass
        val classMirror = rm.reflectClass(classTest)
        val constructor = typeOf[T].decl(termNames.CONSTRUCTOR).asMethod
        val constructorMirror = classMirror.reflectConstructor(constructor)
    
        val constructorArgs = constructor.paramLists.flatten.map( (param: Symbol) => {
          val paramName = param.name.toString
          if(param.typeSignature <:< typeOf[Option[Any]])
            m.get(paramName)
          else
            m.get(paramName).getOrElse(throw new IllegalArgumentException("Map is missing required parameter named " + paramName))
        })
    
        constructorMirror(constructorArgs:_*).asInstanceOf[T]
      }
    }
    
    class CaseClassFromMapSpec extends Specification {
      "case class" should {
        "be constructable from a Map" in {
          import ccFromMap._
          fromMap[Test](Map("t" -> "test", "ot" -> "test2")) === Test("test", Some("test2"))
          fromMap[Test](Map("t" -> "test")) === Test("test", None)
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-01 02:07

    Two ways of doing this elegantly. The first is to use an unapply, the second to use an implicit class (2.10+) with a type class to do the conversion for you.

    1) The unapply is the simplest and most straight forward way to write such a conversion. It does not do any "magic" and can readily be found if using an IDE. Do note, doing this sort of thing can clutter your companion object and cause your code to sprout dependencies in places you might not want:

    object MyClass{
      def unapply(values: Map[String,String]) = try{
        Some(MyClass(values("key").toInteger, values("next").toFloat))
      } catch{
        case NonFatal(ex) => None
      }
    }
    

    Which could be used like this:

    val MyClass(myInstance) = myMap
    

    be careful, as it would throw an exception if not matched completely.

    2) Doing an implicit class with a type class creates more boilerplate for you but also allows a lot of room to expand the same pattern to apply to other case classes:

    implicit class Map2Class(values: Map[String,String]){
      def convert[A](implicit mapper: MapConvert[A]) = mapper conv (values)
    }
    
    trait MapConvert[A]{
      def conv(values: Map[String,String]): A
    }
    

    and as an example you'd do something like this:

    object MyObject{
      implicit val new MapConvert[MyObject]{
        def conv(values: Map[String, String]) = MyObject(values("key").toInt, values("foo").toFloat)
      }
    }
    

    which could then be used just as you had described above:

    val myInstance = myMap.convert[MyObject]
    

    throwing an exception if no conversion could be made. Using this pattern converting between a Map[String, String] to any object would require just another implicit (and that implicit to be in scope.)

    0 讨论(0)
  • 2020-12-01 02:18

    Jonathan Chow implements a Scala macro (designed for Scala 2.11) that generalizes this behavior and eliminates the boilerplate.

    http://blog.echo.sh/post/65955606729/exploring-scala-macros-map-to-case-class-conversion

    import scala.reflect.macros.Context
    
    trait Mappable[T] {
      def toMap(t: T): Map[String, Any]
      def fromMap(map: Map[String, Any]): T
    }
    
    object Mappable {
      implicit def materializeMappable[T]: Mappable[T] = macro materializeMappableImpl[T]
    
      def materializeMappableImpl[T: c.WeakTypeTag](c: Context): c.Expr[Mappable[T]] = {
        import c.universe._
        val tpe = weakTypeOf[T]
        val companion = tpe.typeSymbol.companionSymbol
    
        val fields = tpe.declarations.collectFirst {
          case m: MethodSymbol if m.isPrimaryConstructor ⇒ m
        }.get.paramss.head
    
        val (toMapParams, fromMapParams) = fields.map { field ⇒
          val name = field.name
          val decoded = name.decoded
          val returnType = tpe.declaration(name).typeSignature
    
          (q"$decoded → t.$name", q"map($decoded).asInstanceOf[$returnType]")
        }.unzip
    
        c.Expr[Mappable[T]] { q"""
          new Mappable[$tpe] {
            def toMap(t: $tpe): Map[String, Any] = Map(..$toMapParams)
            def fromMap(map: Map[String, Any]): $tpe = $companion(..$fromMapParams)
          }
        """ }
      }
    }
    
    0 讨论(0)
  • 2020-12-01 02:20

    I don't love this code, but I suppose this is possible if you can get the map values into a tuple and then use the tupled constructor for your case class. That would look something like this:

    val myMap = Map("k1" -> 1, "k2" -> "val2", "k3" -> "val3")    
    val params = Some(myMap.map(_._2).toList).flatMap{
      case List(a:Int,b:String,c:String) => Some((a,b,c))
      case other => None
    }    
    val myCaseClass = params.map(Test.tupled(_))
    println(myCaseClass)
    

    You have to be careful to make sure the list of values is exactly 3 elements and that they are the correct types. If not, you end up with a None instead. Like I said, not great, but it shows that it is possible.

    0 讨论(0)
  • 2020-12-01 02:22

    This works well for me,if you use jackson for scala:

    def from[T](map: Map[String, Any])(implicit m: Manifest[T]): T = {
      val mapper = new ObjectMapper() with ScalaObjectMapper
      mapper.convertValue(map)
    }
    

    Reference from:Convert a Map<String, String> to a POJO

    0 讨论(0)
  • 2020-12-01 02:25
    commons.mapper.Mappers.mapToBean[CaseClassBean](map)
    

    Details: https://github.com/hank-whu/common4s

    0 讨论(0)
提交回复
热议问题