Created
July 10, 2012 12:19
-
-
Save kevinwright/3082959 to your computer and use it in GitHub Desktop.
Typesafe conversion from List[Any] to a case class, via 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
case class ZeeingEvent( | |
zid: String, | |
kind: String, | |
showId: String, | |
show_name: Option[String], | |
time: DateTime | |
) { | |
require (kind == "StartedZeeing" || kind == "EndedZeeing") | |
} | |
object ZeeingEvent { | |
implicit val iso = HListIso(ZeeingEvent.apply _, ZeeingEvent.unapply _) | |
def fromList(l: List[Any]): ZeeingEvent = { | |
def toHList[HL <: HList]( | |
implicit | |
iso: HListIso[ZeeingEvent, HL], | |
typeable: Typeable[HL] | |
): Option[HL] = | |
l.cast[HL] | |
toHList map {iso.fromHList} getOrElse { | |
sys.error("couldn't parse list as zeeing event: " + l) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment