Skip to content

Instantly share code, notes, and snippets.

@danbills
Created June 20, 2017 01:19
Show Gist options
  • Save danbills/a6fe54d7e89c7e3e232fcab69a6f6fe8 to your computer and use it in GitHub Desktop.
Save danbills/a6fe54d7e89c7e3e232fcab69a6f6fe8 to your computer and use it in GitHub Desktop.
Bug deriving Either decoder using generic.auto in Circe. (Run w/ Ammonite)
import $ivy.`io.circe::circe-core:0.6.1`
import $ivy.`io.circe::circe-generic:0.6.1`
import $ivy.`io.circe::circe-parser:0.6.1`
import io.circe._
import io.circe.syntax._
import io.circe.parser._
import io.circe.generic.auto._
case class Hi(s: Either[Long, String])
case class S(s: String)
//Fails
assert(decode[Hi]("""{"s":"some"}""").isLeft)
//Fails
assert(decode[Hi]("""{"s":44}""").isLeft)
//works
assert(decode[S]("""{"s":"some"}""").isRight)
@danbills
Copy link
Author

I was able to resolve with

implicit def h[A,B](implicit a: Decoder[A], b: Decoder[B]): Decoder[Either[A,B]] = {
        val l: Decoder[Either[A,B]]= a.map(Left.apply)
        val r: Decoder[Either[A,B]]= b.map(Right.apply)
        l or r
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment