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 to solve such a problem?