pen*_*365 4 scala shapeless scala-cats
我正在尝试解码一些真正糟糕的 JSON。每个对象的类型信息都在标记为type、 ie"type": "event"等的字段中编码。我使用Circe进行 JSON 编码/解码。该库使用类型类,其中相关的类型类是def apply(c: HCursor): Decoder.Result[A]. 问题是任何解码器都对类型不变,A. 这是一个具体的例子
sealed trait MotherEvent {
val id: UUID
val timestamp: DateTime
}
implicit val decodeJson: Decoder[MotherEvent] = new Decoder[MotherEvent] {
def apply(c: HCursor) = {
c.downField("type").focus match {
case Some(x) => x.asString match {
case Some(string) if string == "flight" => FlightEvent.decodeJson(c)
case Some(string) if string == "hotel" => // etc
// like a bunch of these
case None => Xor.Left(DecodingFailure("type is not a string", c.history))
}
case None => Xor.Left(DecodingFailure("not type found", c.history))
}
}
sealed trait FlightEvents(id: UUID, timestamp: DateTime, flightId: Int)
case class Arrival(id: UUID, timestamp: DateTime, flightId: Int) extends Event // a metric ton of additional fields
case class Departure(id: UUID, timestamp: DateTime, flightId: Int) extends Event // samsies as Arrival
Run Code Online (Sandbox Code Playgroud)
解码工作正常,但MotherEvent总是返回
val jsonString = // from wherevs, where the json string is flightevent
val x = decode[MotherEvent](jsonString)
println(x) // prints (cats.data.Xor[io.circe.Error, MotherEvent] = Right(FlightEvent)
println(x.flightId) // ERROR- flightId is not a member of MotherEvent
Run Code Online (Sandbox Code Playgroud)
当然,我想要一个 FlightEvent 而不是 Mother 事件。一种可能的解决方案是创建一个具有 60 或 70 个字段的“母亲”类型,但我已经讨厌自己并想退出编程,只考虑Option[A]基于该type字段填充的70 个字段。
谁能想到一个好的解决方案?
所以,我最终接受了一个不变量A并依赖于 Shapeless Coproduct。这会导致一些额外的代码重复,但它极大地简化了我对问题的思考方式以及Decoder[A]处理方式。由于这是一个简单的数据摄取程序的一部分,因此将额外的工作映射Coproduct到数据库上并为由 DB 表示的数据提供更清晰的类型相对容易。这是一个如何组合在一起的玩具示例:
import shapeless._
import io.circe._, io.circe.Decoder.instance
case class FlightEvent(id: UUID, departureTime: DateTime, arrivalTime: DateTime)
case class HotelEvent(id: UUID, city: String)
case class CarEvent(id: UUID, carrier: String)
// assume valid Decoder typeclasses in each companion object
type FHC = FlightEvent :+: HotelEvent :+: CarEvent :+: CNil
type FHCs = List[FHC]
implicit val decodeFHC: Decoder[FHC] = instance { c =>
c.downField("type".focus match {
case Some(t) => t.asString match {
case Some(string) if string == "flight" => FlightEvent.decodeJson(c) map { Coproduct[FHC](_) }
case Some(string) if string == "hotel" => HotelEvent.decodeJson(c) map { Coproduct[FHC](_) }
case Some(string) if string == "car" => CarEvent.decodeJson(c) map { Coproduct[FHC](_) }
case Some(string) => Xor.Left(DecodingFailure(s"unkown type $string", c.history))
case None => Xor.Left(DecodingFailure("json field \"type\", string expected", c.history))
}
case None => Xor.Left(DecodingFailure("json field \"type\" not found", c.history))
}
}
Run Code Online (Sandbox Code Playgroud)
这是我第一次使用 shapeless,所以很有可能有一种更简洁的方法来实现它。我确实喜欢正交如何Coproduct做到这一点。