Scodec组合器:标头包含用于区分类型的幻数

时间:2016-01-10 03:31:36

标签: scala scodec

我正在寻找一种方法来接近协议,如下例所示:

case class Request(bodyType: Int, foo: Int, bar: Int, body: RequestBody)

sealed trait RequestBody
case class Read(key: String) extends RequestBody
case class Write(key: String, value: Array[Byte]) extends RequestBody 

此处,bodyType == 0代表ReadbodyType != 0代表Write。 请注意,有一些字段将鉴别器与鉴别值区分开来。

我见过an example with byte-ordering。 但据我所知,这种“鱿鱼”编码鉴别器不会往返。 解决这个问题的正确方法是什么?

1 个答案:

答案 0 :(得分:5)

有几种方法可以做到,但这是我用过的方法:

import scodec._
import scodec.codecs._
import scodec.bits._

case class Request(bodyType: Int, foo: Int, bar: Int, body: RequestBody)

sealed trait RequestBody
case class Read(key: String) extends RequestBody
object Read {
  implicit val codec: Codec[Read] = ("key" | utf8).as[Read]
  implicit val discriminator: Discriminator[RequestBody, Read, Int] = Discriminator(0)
}
case class Write(key: String, value: ByteVector) extends RequestBody
object Write {
  implicit val codec: Codec[Write] = {
    ("key"   | utf8  ) ::
    ("value" | bytes )
  }.as[Write]
  implicit val discriminator: Discriminator[RequestBody, Write, Int] = Discriminator(1)
}

object Request {
  implicit val codec: Codec[Request] = {
    ("bodyType" | uint16 ).flatPrepend { bodyType =>
    ("foo"      | uint16 ) ::
    ("bar"      | uint16 ) ::
    ("body"     | Codec.coproduct[RequestBody].discriminatedBy(provide(bodyType)).auto)
  }}.as[Request]
}