简体   繁体   English

与 Cats 和 Refined Types 一起使用时无法生成 Circe 解码器

[英]Not able to generate Circe Decoder when using with Cats and Refined Types

I wrote this code我写了这段代码

import io.circe._
import io.circe.refined._
import cats.data._
import cats.implicits._
import eu.timepit.refined.auto._

final case class Translation(lang: LanguageCode, name: ProductName)
final case class Product(id: ProductId, names: List[Translation])

object Translation {
    implicit val decode: Decoder[Translation] = Decoder.forProduct2("lang", "name")(Translation.apply)
    implicit val encode: Encoder[Translation] = Encoder.forProduct2("lang", "name")(t => (t.lang, t.name))
}

object Product {
    implicit val decode: Decoder[Product] = Decoder.forProduct2("id", "names")(Product.apply)
    implicit val encode: Encoder[Product] = Encoder.forProduct2("id", "names")(p => (p.id, p.names))
}

This works fine and it compiles.这工作正常并且可以编译。 but if I change my Product Type to use a cats non-empty set.但是如果我更改我的产品类型以使用猫非空集。

final case class Product(id: ProductId, names: NonEmptySet[Translation])

I get a compile time error我收到编译时错误

could not find implicit value for parameter decodeA1:
io.circe.Decoder[cats.data.NonEmptySet[com.abhi.models.Translation]]"

What can I do so that it auto-generates the decoder for the NonEmptySet just like it does for the List?我该怎么做才能像为 List 一样自动生成 NonEmptySet 的解码器?

Looking at the circe source code it provides a Decoder[NonEmptySet[A]] if given a Decoder[A] and a Order[A] .查看circe 源代码,如果给定Decoder[A]Order[A] ,它会提供Decoder[NonEmptySet[A]] ] 。

implicit final def decodeNonEmptySet[A](implicit decodeA: Decoder[A], orderA: Order[A]): Decoder[NonEmptySet[A]] =
    new NonEmptySeqDecoder[A, SortedSet, NonEmptySet[A]](decodeA) {
      final protected def createBuilder(): Builder[A, SortedSet[A]] = SortedSet.newBuilder[A](Order.catsKernelOrderingForOrder(orderA))
      final protected val create: (A, SortedSet[A]) => NonEmptySet[A] = (h, t) => NonEmptySet(h, t)
    }

My guess is you're missing an implicit for Order[Translation] .我的猜测是您缺少Order[Translation]的隐含含义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM