Advertisement
Guest User

Untitled

a guest
May 30th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. object CaseClassToMapStringOfString {
  2. import shapeless._
  3. import shapeless.labelled._
  4. import syntax.singleton._
  5. import record._
  6. import ops.record._
  7. import syntax.singleton._
  8.  
  9. case class Color(value: String)
  10. case class ChartOptions(stringOpt: String, intOpt: Int, colorOpt: Color)
  11.  
  12. private def colorToString(c: Color): String = c.value
  13.  
  14. object convertToString extends Poly1 {
  15. // missing parameter type for expanded function ((x$2) => x$2.toString)
  16. // implicit def default[K] =
  17. // at[FieldType[K, String]](field[K](_.toString))
  18.  
  19. implicit def default[K] =
  20. at[K](_.toString)
  21.  
  22. implicit def colorToStringMapper[K] =
  23. at[FieldType[K, Color]](field[K](colorToString _))
  24. }
  25.  
  26. val gen = LabelledGeneric[ChartOptions]
  27. val opt = ChartOptions("one", 2, Color("red"))
  28.  
  29. // 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]]]
  30. val optsRecord = gen.to(opt)
  31.  
  32. // I really want to convert to toString on type by type basis
  33. // But once I map here, I seem to be dropping the keys so then I can't invoke toMap
  34. // Instead of having an HList of FieldTypes, I have an HList of just String
  35. // shapeless.::[String,shapeless.::[String,shapeless.::[String,shapeless.HNil]]]
  36. val mappedRec = optsRecord.map(convertToString)
  37.  
  38. // toMap kind of works here, but now I am just using toString on value which is really just Any.toString
  39. val globalMap: Map[String, String] = optsRecord.toMap.map {
  40. case (key, value) =>
  41. (key.name) -> value.toString
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement