I'm trying to implement codec for PSQL COPY BINARY format. Details are at Tuple section
Int, Bool, String fields are encoded as <4 byte length><var length payload>
I implemented them like this
val psqlUtf8: Codec[String] = variableSizeBytes(int32, utf8)
val psqlBool: Codec[Boolean] = variableSizeBytes(int32, byte).xmap[Boolean](_ == 1, v ⇒ if (v) 1 else 0)
val psqlInt: Codec[Int] = variableSizeBytes(int32, int32)
But to encode NULL they use -1 in length field.
Could you please suggest how I can implement Codec[Option[T]] for such situation ?
The best I could come up with is