Generic derivation of AnyVal types with Circe

1.8k views Asked by At

I want to derive Encoder instances for value classes. With the semiauto mechanism I am not able to derive nested classes.

Image the following case class structure

{
  case class Recipient(email: Recipient.Email, name: Recipient.Name) 

  object Recipient {
     case class Email(value: String) extends AnyVal
     case class Name(value: String) extends AnyVal
  }
}

In an ammonite shell ( also add the Recipient case classes )

load.ivy("io.circe" %% "circe-core" % "0.6.1")
load.ivy("io.circe" %% "circe-generic" % "0.6.1")

import io.circe._
import io.circe.generic.semiauto._
import io.circe.syntax._ 

Now deriving a decoder for Email results as expected in

Recipient.Email("[email protected]").asJson(deriveEncoder[Recipient.Email]) 
Json = {
  "value" : "[email protected]"
}

Deriving an Encoder[Recipient] doesn't work

deriveDecoder[Recipient]

could not find Lazy implicit value of type 
io.circe.generic.decoding.DerivedDecoder[$sess.cmd5.Recipient]

What I would like to do is deriving an Encoder[Recipient.Email] that returns the wrapped type. This little piece works if I derive the codec explicitly.

import shapeless.Unwrapped

implicit def encodeAnyVal[W <: AnyVal, U](
        implicit unwrapped: Unwrapped.Aux[W, U],
                 encoderUnwrapped: Encoder[U]): Encoder[W] = {
        Encoder.instance[W](v => encoderUnwrapped(unwrapped.unwrap(v)))
}

Recipient.Email("[email protected]").asJson(encodeAnyVal[Recipient.Email, String])
res11: Json = "[email protected]"

Still I can't derive a Encoder[Recipient]

implicit val emailEncoder: Encoder[Recipient.Email] = encodeAnyVal[Recipient.Email, String] 
implicit val nameEncoder: Encoder[Recipient.Name] = encodeAnyVal[Recipient.Name, String]

deriveDecoder[Recipient] 
cmd14.sc:1: could not find Lazy implicit value of type io.circe.generic.decoding.DerivedDecoder[$sess.cmd5.Recipient]

Has anyone done something similar?

Thanks in advance, Muki

1

There are 1 answers

0
Leammas On BEST ANSWER

You should add implicit witness instance instead of type bound. Going to end up with something like this:

implicit def encodeAnyVal[W, U](
        implicit ev: W <:< Anyval,
                 unwrapped: Unwrapped.Aux[W, U],
                 encoderUnwrapped: Encoder[U]): Encoder[W] = {
        Encoder.instance[W](v => encoderUnwrapped(unwrapped.unwrap(v)))
}