package borealis import ( "fmt" "github.com/fxamacker/cbor/v2" ) const ( V1MessageVersion = 1 V1MessageMinLen = 23 ) type V1MessageEncoder struct{} func (encoder *V1MessageEncoder) Encode(stream string, message interface{}) ([]byte, error) { output, err := cbor.Marshal(message) if err != nil { return nil, err } return append([]byte{V1MessageVersion}, output...), nil } func (encoder *V1MessageEncoder) Decode(stream string, input []byte, messagePtr interface{}) error { if len(input) < V1MessageMinLen { return fmt.Errorf("expected message length %d+, but got %d", V1MessageMinLen, len(input)) } if input[0] != V1MessageVersion { return fmt.Errorf("expected message version %d, but got %d", V1MessageVersion, input[0]) } switch message := messagePtr.(type) { case *[]byte: *message = input[1:] return nil default: return cbor.Unmarshal(input[1:], message) } }