首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >与Cats和精化类型一起使用时无法生成Circe解码器

与Cats和精化类型一起使用时无法生成Circe解码器
EN

Stack Overflow用户
提问于 2019-10-21 03:17:13
回答 1查看 351关注 0票数 2

我写了这段代码

代码语言:javascript
运行
复制
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))
}

这可以很好地工作,并且可以编译。但是,如果我将Product Type更改为使用cats非空集。

代码语言:javascript
运行
复制
final case class Product(id: ProductId, names: NonEmptySet[Translation])

我得到一个编译时错误

代码语言:javascript
运行
复制
could not find implicit value for parameter decodeA1:
io.circe.Decoder[cats.data.NonEmptySet[com.abhi.models.Translation]]"

我怎么做才能让它自动生成NonEmptySet的解码器,就像它为列表生成解码器一样?

EN

Stack Overflow用户

发布于 2019-10-21 21:19:07

看一下circe source code,如果给它一个Decoder[NonEmptySet[A]]和一个Decoder[A],它就会提供一个Order[A]

代码语言:javascript
运行
复制
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)
    }

我的猜测是你遗漏了一个隐含的Order[Translation]

票数 2
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58476443

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档