1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-30 10:10:44 +02:00
go-micro/codec/bytes/marshaler.go

41 lines
634 B
Go
Raw Normal View History

2019-01-10 11:42:02 +02:00
package bytes
import (
2024-06-04 22:40:43 +02:00
"go-micro.dev/v5/codec"
2019-01-10 11:42:02 +02:00
)
type Marshaler struct{}
type Message struct {
Header map[string]string
Body []byte
}
func (n Marshaler) Marshal(v interface{}) ([]byte, error) {
2019-12-03 09:25:58 +02:00
switch ve := v.(type) {
2019-01-14 23:30:43 +02:00
case *[]byte:
return *ve, nil
2019-01-10 11:42:02 +02:00
case []byte:
2019-12-03 09:25:58 +02:00
return ve, nil
2019-01-10 11:42:02 +02:00
case *Message:
2019-12-03 09:25:58 +02:00
return ve.Body, nil
2019-01-10 11:42:02 +02:00
}
return nil, codec.ErrInvalidMessage
2019-01-10 11:42:02 +02:00
}
func (n Marshaler) Unmarshal(d []byte, v interface{}) error {
2019-12-03 09:25:58 +02:00
switch ve := v.(type) {
2019-01-10 11:42:02 +02:00
case *[]byte:
*ve = d
return nil
2019-01-10 11:42:02 +02:00
case *Message:
ve.Body = d
return nil
2019-01-10 11:42:02 +02:00
}
return codec.ErrInvalidMessage
2019-01-10 11:42:02 +02:00
}
func (n Marshaler) String() string {
return "bytes"
}