Caballero Caballero - 1 year ago 149
Scala Question

Scala: convert map to case class

Let's say I have this example case class

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


And I have a map

myMap = Map("k1" -> 1, "k2" -> "val2", "k3" -> "val3")


I need to convert this map to my case class in several places of the code, something like this:

myMap.asInstanceOf[Test]


What would be the easiest way of doing that? Can I somehow use implicit for this?

Answer Source

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.)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download