gitee.com/liuxuezhan/go-micro-v1.18.0@v1.0.0/codec/bytes/marshaler.go (about) 1 package bytes 2 3 import ( 4 "errors" 5 ) 6 7 type Marshaler struct{} 8 9 type Message struct { 10 Header map[string]string 11 Body []byte 12 } 13 14 func (n Marshaler) Marshal(v interface{}) ([]byte, error) { 15 switch ve := v.(type) { 16 case *[]byte: 17 return *ve, nil 18 case []byte: 19 return ve, nil 20 case *Message: 21 return ve.Body, nil 22 } 23 return nil, errors.New("invalid message") 24 } 25 26 func (n Marshaler) Unmarshal(d []byte, v interface{}) error { 27 switch ve := v.(type) { 28 case *[]byte: 29 *ve = d 30 case *Message: 31 ve.Body = d 32 } 33 return errors.New("invalid message") 34 } 35 36 func (n Marshaler) String() string { 37 return "bytes" 38 }