According to Travis Brown, the problem is that "the general output is designed to work with typical instances of Shapeless and expects the FieldType key to be a character." See the Gitter discussion .
My decision:
object Claims { import shapeless._ import shapeless.labelled.FieldType import io.circe._ import io.circe.syntax._ import io.circe.generic.semiauto._ import io.circe.generic.encoding.DerivedObjectEncoder import java.util.UUID abstract case class ClaimOf[V](name: String) extends FieldOf[V] object iss extends ClaimOf[String]("iss") object subj extends ClaimOf[String]("subj") object aud extends ClaimOf[Set[String]]("aud") object client_id extends ClaimOf[UUID]("client_id") implicit final def encodeClaims[K, H, T <: HList](implicit key: Witness.Aux[K], claim: K <:< ClaimOf[H], encodeHead: Lazy[Encoder[H]], encodeTail: Lazy[DerivedObjectEncoder[T]] ): DerivedObjectEncoder[FieldType[K, H] :: T] = new DerivedObjectEncoder[FieldType[K, H] :: T] { final def encodeObject(a: FieldType[K, H] :: T): JsonObject = a match { case h :: t => (key.value.name -> encodeHead.value(h)) +: encodeTail.value.encodeObject(t) } } val encoder = deriveEncoder[FieldType[iss.type, String] :: FieldType[subj.type, String] :: HNil] val rec = (iss ->> "issuer") :: (subj ->> "subject") :: HNil val json = rec.asJson.spaces2 }
The encodeClaims
function is basically a copy of the original function from circe
, replacing the key constraint.
This answer can be used to implement another solution.
source share