github.com/pavlo67/common@v0.5.3/common/serialization/marshaler.go (about) 1 package serialization 2 3 import ( 4 "encoding/json" 5 6 "gopkg.in/yaml.v3" 7 ) 8 9 type Marshaler interface { 10 Marshal(v interface{}) ([]byte, error) 11 Unmarshal(data []byte, v interface{}) error 12 } 13 14 var MarshalerYAML = MarshalerStruct{yaml.Marshal, yaml.Unmarshal} 15 var MarshalerJSON = MarshalerStruct{json.Marshal, json.Unmarshal} 16 17 // MarshalerStruct ---------------------------------------------------------------------------------------- 18 19 type Marshal func(v interface{}) ([]byte, error) 20 type Unmarshal func(data []byte, v interface{}) error 21 22 var _ Marshaler = &MarshalerStruct{} 23 24 type MarshalerStruct struct { 25 marshal Marshal 26 unmarshal Unmarshal 27 } 28 29 func (cs MarshalerStruct) Marshal(v interface{}) ([]byte, error) { 30 return cs.marshal(v) 31 } 32 33 func (cs MarshalerStruct) Unmarshal(data []byte, v interface{}) error { 34 return cs.unmarshal(data, v) 35 }