If I have a Map[String,String]("url" -> "xxx", "title" -> "yyy")
, is there an way to generically transform it into a case class Image(url:String, title:String)
?
I can write a helper:
object Image{
def fromMap(params:Map[String,String]) = Image(url=params("url"), title=params("title"))
}
but is there a way to generically write this once for a map to any case class?
Not a full answer to your question, but a start…
It can be done, but it will probably get more tricky than you thought. Each generated Scala class is annotated with the Java annotation
ScalaSignature
, whosebytes
member can be parsed to give you the metadata that you would need (including argument names). The format of this signature is not API, however, so you'll need to parse it yourself (and are likely to change the way you parse it with each new major Scala release).Maybe the best place to start is the lift-json library, which has the ability to create instances of case classes based on JSON data.
Update: I think lift-json actually uses Paranamer to do this, and thus may not parse the bytes of
ScalaSignature
… Which makes this technique work for non-Scala classes, too.Update 2: See Moritz's answer instead, who is better informed than I am.
There's a hack you can transform map to json and then to case class. I used spray-json
and use this dependency in sbt build:
Here's a solution using builtin scala/java reflection:
To use it:
This cannot be done, since you would need to get the companion object's apply method's parameter names and they simply aren't available via reflection. If you have a lot of these case classes, you could parse their declarations and generate the fromMap methods.
First off, there are some safe alternatives you could do if you just want to shorten your code. The companion object can be treated as a function so you could use something like this:
This will return an option containing the result. Alternatively you could use the
ApplicativeBuilder
s in Scalaz which internally do almost the same but with a nicer syntax:If you really need to do this via reflection then the easiest way would be to use Paranamer (as the Lift-Framework does). Paranamer can restore the parameter names by inspecting the bytecode so there is a performance hit and it will not work in all environments due to classloader issues (the REPL for example). If you restrict yourself to classes with only
String
constructor parameters then you could do it like this:(Note that this example can pick a default constructor as it does not check for the parameter count which you would want to do)