Given a function f
, that, given a Map[String, MyType]
, returns a HList
:
package net
import shapeless._
sealed trait MyType
case object MyInt extends MyType
case object MyStr extends MyType
object Mapper {
def f(m: Map[String, MyType]): HList = m.foldLeft[HList](HNil){ (acc, elem) =>
val (key, t) = elem
t match {
case MyInt => classOf[Int] :: acc
case MyStr => classOf[String] :: acc
}
}
}
I tested it:
import net._
val list = Map("foo" -> MyInt, "bar" -> MyStr)
scala> Mapper.f(list)
res0: shapeless.HList = class java.lang.String :: int :: HNil
How can I use the above approach (or another one) to build a case class
with members matching the String
keys, and the types given by the output of f
?
So, I'm looking for:
g(Map("foo" -> MyInt, "bar" -> MyStr))
to output case class X(foo: Int, bar: String)
where X
is arbitrarily chosen, i.e. not important at this point.
I thought of using Generic[X]
, but I don't know how to get a Generic
without a case class
first.