Created
July 27, 2016 21:51
-
-
Save calippo/892ce793c9696b330e55772099056b7a to your computer and use it in GitHub Desktop.
Convert case class to map in shapeless
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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