Skip to content

Instantly share code, notes, and snippets.

@danslapman
Created May 8, 2019 12:15
Show Gist options
  • Save danslapman/dd7a8742817159d818697738b136e646 to your computer and use it in GitHub Desktop.
Save danslapman/dd7a8742817159d818697738b136e646 to your computer and use it in GitHub Desktop.
Encoding enumeratum enums with morphling
case class TEnum[P[_], R, A](repr: P[R], ve: ValueEnum[R, A])(implicit val ev: A <:< ValueEnumEntry[R]) extends TType[P, A]
..
def tEnum[I, E](repr: Schema[TSchema, I], ve: ValueEnum[I, E])(implicit ev: E <:< ValueEnumEntry[I]): Schema[TSchema, E] =
prim(HMutu(TEnum(repr, ve)))
..
override val encoder: TSchema ~> Encoder = new (TSchema ~> Encoder) {
override def apply[A](ts: TSchema[A]): Encoder[A] = ts.unmutu match {
...
case te: TEnum[Schema[TSchema, ?], r, A] =>
te.repr.encoder.contramap[A](a => te.ev(a).value)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment