mirror of
https://github.com/go-micro/go-micro.git
synced 2024-11-30 08:06:40 +02:00
42 lines
672 B
Go
42 lines
672 B
Go
package bytes
|
|
|
|
import (
|
|
"errors"
|
|
)
|
|
|
|
type Marshaler struct{}
|
|
|
|
type Message struct {
|
|
Header map[string]string
|
|
Body []byte
|
|
}
|
|
|
|
func (n Marshaler) Marshal(v interface{}) ([]byte, error) {
|
|
switch v.(type) {
|
|
case *[]byte:
|
|
ve := v.(*[]byte)
|
|
return *ve, nil
|
|
case []byte:
|
|
return v.([]byte), nil
|
|
case *Message:
|
|
return v.(*Message).Body, nil
|
|
}
|
|
return nil, errors.New("invalid message")
|
|
}
|
|
|
|
func (n Marshaler) Unmarshal(d []byte, v interface{}) error {
|
|
switch v.(type) {
|
|
case *[]byte:
|
|
ve := v.(*[]byte)
|
|
*ve = d
|
|
case *Message:
|
|
ve := v.(*Message)
|
|
ve.Body = d
|
|
}
|
|
return errors.New("invalid message")
|
|
}
|
|
|
|
func (n Marshaler) String() string {
|
|
return "bytes"
|
|
}
|