ceen/codec/binary.go

48 lines
821 B
Go
Raw Normal View History

2022-06-16 22:19:06 +02:00
package codec
import (
"encoding"
"fmt"
)
var (
Binary Codec = &binaryCodec{}
)
type binaryCodec struct{}
func (*binaryCodec) Name() string {
return "binary"
}
func (*binaryCodec) Marshal(v interface{}) ([]byte, error) {
// Check for native implementation.
if m, ok := v.(encoding.BinaryMarshaler); ok {
return m.MarshalBinary()
}
// Otherwise assume byte slice.
b, ok := v.([]byte)
if !ok {
return nil, fmt.Errorf("value not []byte")
}
return b, nil
}
func (*binaryCodec) Unmarshal(b []byte, v interface{}) error {
// Check for native implementation.
if u, ok := v.(encoding.BinaryUnmarshaler); ok {
return u.UnmarshalBinary(b)
}
// Otherwise assume byte slice.
bp, ok := v.(*[]byte)
if !ok {
return fmt.Errorf("value must be *[]byte")
}
*bp = append((*bp)[:0], b...)
return nil
}