【问题标题】:Generic derivation for ADTs in Scala with a custom representationScala 中具有自定义表示的 ADT 的通用派生
【发布时间】:2019-02-06 14:09:54
【问题描述】:

我在这里转述a question from the circe Gitter channel

假设我有一个像这样的 Scala 密封特征层次结构(或 ADT):

sealed trait Item
case class Cake(flavor: String, height: Int) extends Item
case class Hat(shape: String, material: String, color: String) extends Item

...我希望能够在此 ADT 和 JSON 表示之间来回映射,如下所示:

{ "tag": "Cake", "contents": ["cherry", 100] }
{ "tag": "Hat", "contents": ["cowboy", "felt", "black"] }

默认情况下,circe 的泛型派生使用不同的表示:

scala> val item1: Item = Cake("cherry", 100)
item1: Item = Cake(cherry,100)

scala> val item2: Item = Hat("cowboy", "felt", "brown")
item2: Item = Hat(cowboy,felt,brown)

scala> import io.circe.generic.auto._, io.circe.syntax._
import io.circe.generic.auto._
import io.circe.syntax._

scala> item1.asJson.noSpaces
res0: String = {"Cake":{"flavor":"cherry","height":100}}

scala> item2.asJson.noSpaces
res1: String = {"Hat":{"shape":"cowboy","material":"felt","color":"brown"}}

我们可以更接近 circe-generic-extras:

import io.circe.generic.extras.Configuration
import io.circe.generic.extras.auto._

implicit val configuration: Configuration =
   Configuration.default.withDiscriminator("tag")

然后:

scala> item1.asJson.noSpaces
res2: String = {"flavor":"cherry","height":100,"tag":"Cake"}

scala> item2.asJson.noSpaces
res3: String = {"shape":"cowboy","material":"felt","color":"brown","tag":"Hat"}

……但这仍然不是我们想要的。

使用 circe 为 Scala 中的 ADT 通常派生此类实例的最佳方法是什么?

【问题讨论】:

    标签: json scala circe generic-derivation


    【解决方案1】:

    将案例类表示为 JSON 数组

    首先要注意的是 circe-shapes 模块为 Shapeless 的 HLists 提供了实例,这些实例使用了我们想要的案例类的数组表示。例如:

    scala> import io.circe.shapes._
    import io.circe.shapes._
    
    scala> import shapeless._
    import shapeless._
    
    scala> ("foo" :: 1 :: List(true, false) :: HNil).asJson.noSpaces
    res4: String = ["foo",1,[true,false]]
    

    ...Shapeless 本身提供了案例类和HLists 之间的通用映射。我们可以结合这两者来获得我们想要的案例类的通用实例:

    import io.circe.{ Decoder, Encoder }
    import io.circe.shapes.HListInstances
    import shapeless.{ Generic, HList }
    
    trait FlatCaseClassCodecs extends HListInstances {
      implicit def encodeCaseClassFlat[A, Repr <: HList](implicit
        gen: Generic.Aux[A, Repr],
        encodeRepr: Encoder[Repr]
      ): Encoder[A] = encodeRepr.contramap(gen.to)
    
      implicit def decodeCaseClassFlat[A, Repr <: HList](implicit
        gen: Generic.Aux[A, Repr],
        decodeRepr: Decoder[Repr]
      ): Decoder[A] = decodeRepr.map(gen.from)
    }
    
    object FlatCaseClassCodecs extends FlatCaseClassCodecs
    

    然后:

    scala> import FlatCaseClassCodecs._
    import FlatCaseClassCodecs._
    
    scala> Cake("cherry", 100).asJson.noSpaces
    res5: String = ["cherry",100]
    
    scala> Hat("cowboy", "felt", "brown").asJson.noSpaces
    res6: String = ["cowboy","felt","brown"]
    

    请注意,我使用 io.circe.shapes.HListInstances 将我们需要的 circe-shapes 实例与我们的自定义案例类实例捆绑在一起,以最大限度地减少用户必须导入的东西的数量(两者都是符合人体工程学并为了缩短编译时间)。

    编码我们 ADT 的通用表示

    这是一个很好的第一步,但它并没有得到我们想要的 Item 本身的表示。为此,我们需要一些更复杂的机器:

    import io.circe.{ JsonObject, ObjectEncoder }
    import shapeless.{ :+:, CNil, Coproduct, Inl, Inr, Witness }
    import shapeless.labelled.FieldType
    
    trait ReprEncoder[C <: Coproduct] extends ObjectEncoder[C]
    
    object ReprEncoder {
      def wrap[A <: Coproduct](encodeA: ObjectEncoder[A]): ReprEncoder[A] =
        new ReprEncoder[A] {
          def encodeObject(a: A): JsonObject = encodeA.encodeObject(a)
        }
    
      implicit val encodeCNil: ReprEncoder[CNil] = wrap(
        ObjectEncoder.instance[CNil](_ => sys.error("Cannot encode CNil"))
      )
    
      implicit def encodeCCons[K <: Symbol, L, R <: Coproduct](implicit
        witK: Witness.Aux[K],
        encodeL: Encoder[L],
        encodeR: ReprEncoder[R]
      ): ReprEncoder[FieldType[K, L] :+: R] = wrap[FieldType[K, L] :+: R](
        ObjectEncoder.instance {
          case Inl(l) => JsonObject("tag" := witK.value.name, "contents" := (l: L))
          case Inr(r) => encodeR.encodeObject(r)
        }
      )
    }
    

    这告诉我们如何编码 Coproduct 的实例,Shapeless 将其用作 Scala 中密封特征层次结构的通用表示。代码一开始可能令人生畏,但它是一种非常常见的模式,如果您花大量时间使用 Shapeless,您会发现 90% 的代码本质上是样板文件,每当您像这样以归纳方式构建实例时都会看到这些代码。

    解码这些副产品

    解码实现有点糟糕,甚至,但遵循相同的模式:

    import io.circe.{ DecodingFailure, HCursor }
    import shapeless.labelled.field
    
    trait ReprDecoder[C <: Coproduct] extends Decoder[C]
    
    object ReprDecoder {
      def wrap[A <: Coproduct](decodeA: Decoder[A]): ReprDecoder[A] =
        new ReprDecoder[A] {
          def apply(c: HCursor): Decoder.Result[A] = decodeA(c)
        }
    
      implicit val decodeCNil: ReprDecoder[CNil] = wrap(
        Decoder.failed(DecodingFailure("CNil", Nil))
      )
    
      implicit def decodeCCons[K <: Symbol, L, R <: Coproduct](implicit
        witK: Witness.Aux[K],
        decodeL: Decoder[L],
        decodeR: ReprDecoder[R]
      ): ReprDecoder[FieldType[K, L] :+: R] = wrap(
        decodeL.prepare(_.downField("contents")).validate(
          _.downField("tag").focus
            .flatMap(_.as[String].right.toOption)
            .contains(witK.value.name),
          witK.value.name
        )
        .map(l => Inl[FieldType[K, L], R](field[K](l)))
        .or(decodeR.map[FieldType[K, L] :+: R](Inr(_)))
      )
    }
    

    一般来说,我们的Decoder 实现会涉及更多逻辑,因为每个解码步骤都可能失败。

    我们的 ADT 表示

    现在我们可以将它们包装在一起了:

    import shapeless.{ LabelledGeneric, Lazy }
    
    object Derivation extends FlatCaseClassCodecs {
      implicit def encodeAdt[A, Repr <: Coproduct](implicit
        gen: LabelledGeneric.Aux[A, Repr],
        encodeRepr: Lazy[ReprEncoder[Repr]]
      ): ObjectEncoder[A] = encodeRepr.value.contramapObject(gen.to)
    
      implicit def decodeAdt[A, Repr <: Coproduct](implicit
        gen: LabelledGeneric.Aux[A, Repr],
        decodeRepr: Lazy[ReprDecoder[Repr]]
      ): Decoder[A] = decodeRepr.value.map(gen.from)
    }
    

    这看起来与我们上面的FlatCaseClassCodecs 中的定义非常相似,并且想法是相同的:我们通过构建通用表示的实例来为我们的数据类型(案例类或 ADT)定义实例那些数据类型。请注意,我再次扩展 FlatCaseClassCodecs,以最大限度地减少用户的导入。

    在行动

    现在我们可以像这样使用这些实例:

    scala> import Derivation._
    import Derivation._
    
    scala> item1.asJson.noSpaces
    res7: String = {"tag":"Cake","contents":["cherry",100]}
    
    scala> item2.asJson.noSpaces
    res8: String = {"tag":"Hat","contents":["cowboy","felt","brown"]}
    

    ……这正是我们想要的。最好的部分是,这将适用于 Scala 中的任何密封特征层次结构,无论它有多少个案例类或这些案例类有多少成员(尽管一旦你进入其中的几十个,编译时间就会开始受到伤害),假设所有成员类型都有 JSON 表示。

    【讨论】:

    • 如果案例类有一个或多个可选字段(或键/值对),它会起作用吗?
    • @AndriyPlokhotnyuk 不幸的是,您需要进行一些更改以确保您使用标准的Option 编码器和解码器,而不是使用这种机制派生一个(因为Option 只是一个密封的特征层次结构,这正是我们试图为其派生实例的内容)。我会尽快更新答案,以展示你是如何做到这一点的。
    猜你喜欢
    • 2016-07-18
    • 1970-01-01
    • 1970-01-01
    • 2013-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-22
    • 1970-01-01
    相关资源
    最近更新 更多