Skip to content

Instantly share code, notes, and snippets.

@calippo
Created July 27, 2016 21:51
Show Gist options
  • Save calippo/892ce793c9696b330e55772099056b7a to your computer and use it in GitHub Desktop.
Save calippo/892ce793c9696b330e55772099056b7a to your computer and use it in GitHub Desktop.
Convert case class to map in shapeless
object Mappable {
implicit class ToMapOps[A](val a: A) extends AnyVal {
import shapeless._
import ops.record._
def toMap[L <: HList](implicit
gen: LabelledGeneric.Aux[A, L],
tmr: ToMap[L]
): Map[String, Any] = {
val m: Map[tmr.Key, tmr.Value] = tmr(gen.to(a))
m.map { case (k: Symbol, v) => k.name -> v }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment