2019-01-10 09:42:02 +00:00
|
|
|
package bytes
|
|
|
|
|
|
|
|
import (
|
2020-04-08 12:50:19 +03:00
|
|
|
"github.com/micro/go-micro/v2/codec"
|
2019-01-10 09:42:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Marshaler struct{}
|
|
|
|
|
|
|
|
type Message struct {
|
|
|
|
Header map[string]string
|
|
|
|
Body []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n Marshaler) Marshal(v interface{}) ([]byte, error) {
|
2019-12-03 15:25:58 +08:00
|
|
|
switch ve := v.(type) {
|
2019-01-14 21:30:43 +00:00
|
|
|
case *[]byte:
|
|
|
|
return *ve, nil
|
2019-01-10 09:42:02 +00:00
|
|
|
case []byte:
|
2019-12-03 15:25:58 +08:00
|
|
|
return ve, nil
|
2019-01-10 09:42:02 +00:00
|
|
|
case *Message:
|
2019-12-03 15:25:58 +08:00
|
|
|
return ve.Body, nil
|
2019-01-10 09:42:02 +00:00
|
|
|
}
|
2020-04-08 12:50:19 +03:00
|
|
|
return nil, codec.ErrInvalidMessage
|
2019-01-10 09:42:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n Marshaler) Unmarshal(d []byte, v interface{}) error {
|
2019-12-03 15:25:58 +08:00
|
|
|
switch ve := v.(type) {
|
2019-01-10 09:42:02 +00:00
|
|
|
case *[]byte:
|
|
|
|
*ve = d
|
|
|
|
case *Message:
|
|
|
|
ve.Body = d
|
|
|
|
}
|
2020-04-08 12:50:19 +03:00
|
|
|
return codec.ErrInvalidMessage
|
2019-01-10 09:42:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n Marshaler) String() string {
|
|
|
|
return "bytes"
|
|
|
|
}
|