天道酬勤,学无止境

scodec

Scodec combinators: Header contains magic number that is used to discriminate types

I am looking for a way to approach a protocol like the following example: 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 Here, bodyType == 0 will stand for Read, and bodyType != 0 will encode Write. Note that there are a few fields separating discriminator from discriminated value. I've seen an example with byte-ordering. But as far as I understand this "squid" encoded discriminator would not round trip. What's the correct way

2021-06-09 09:58:17    分类:问答    scala   scodec