cacheinterface/encoder/binary.go

33 lines
497 B
Go
Raw Normal View History

package encoder
func EncodeValue(encoder Encoder, val any) ([]byte, error) {
var v []byte
if b, ok := val.([]byte); ok {
v = b
} else if s, ok := val.(string); ok {
b = []byte(s)
} else {
return encoder.Marshal(val)
}
return v, nil
}
func DecodeValue(encoder Encoder, b []byte, v any) error {
switch v := v.(type) {
case *[]byte:
if v != nil {
*v = b
return nil
}
case *string:
if v != nil {
*v = string(b)
return nil
}
}
return encoder.Unmarshal(b, v)
}