Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object CaseClassToMapStringOfString {
- import shapeless._
- import shapeless.labelled._
- import syntax.singleton._
- import record._
- import ops.record._
- import syntax.singleton._
- case class Color(value: String)
- case class ChartOptions(stringOpt: String, intOpt: Int, colorOpt: Color)
- private def colorToString(c: Color): String = c.value
- object convertToString extends Poly1 {
- // missing parameter type for expanded function ((x$2) => x$2.toString)
- // implicit def default[K] =
- // at[FieldType[K, String]](field[K](_.toString))
- implicit def default[K] =
- at[K](_.toString)
- implicit def colorToStringMapper[K] =
- at[FieldType[K, Color]](field[K](colorToString _))
- }
- val gen = LabelledGeneric[ChartOptions]
- val opt = ChartOptions("one", 2, Color("red"))
- // shapeless.::[String with shapeless.labelled.KeyTag[Symbol with shapeless.tag.Tagged[String("stringOpt")],String],shapeless.::[Int with shapeless.labelled.KeyTag[Symbol with shapeless.tag.Tagged[String("intOpt")],Int],shapeless.::[com.ccadllc.monocle.ui.components.chartsjs.LineChart.ShapelessExperiment.Color with shapeless.labelled.KeyTag[Symbol with shapeless.tag.Tagged[String("colorOpt")],com.ccadllc.monocle.ui.components.chartsjs.LineChart.ShapelessExperiment.Color],shapeless.HNil]]]
- val optsRecord = gen.to(opt)
- // I really want to convert to toString on type by type basis
- // But once I map here, I seem to be dropping the keys so then I can't invoke toMap
- // Instead of having an HList of FieldTypes, I have an HList of just String
- // shapeless.::[String,shapeless.::[String,shapeless.::[String,shapeless.HNil]]]
- val mappedRec = optsRecord.map(convertToString)
- // toMap kind of works here, but now I am just using toString on value which is really just Any.toString
- val globalMap: Map[String, String] = optsRecord.toMap.map {
- case (key, value) =>
- (key.name) -> value.toString
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement