39 lines
		
	
	
		
			605 B
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			39 lines
		
	
	
		
			605 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 ve := v.(type) {
 | |
| 	case *[]byte:
 | |
| 		return *ve, nil
 | |
| 	case []byte:
 | |
| 		return ve, nil
 | |
| 	case *Message:
 | |
| 		return ve.Body, nil
 | |
| 	}
 | |
| 	return nil, errors.New("invalid message")
 | |
| }
 | |
| 
 | |
| func (n Marshaler) Unmarshal(d []byte, v interface{}) error {
 | |
| 	switch ve := v.(type) {
 | |
| 	case *[]byte:
 | |
| 		*ve = d
 | |
| 	case *Message:
 | |
| 		ve.Body = d
 | |
| 	}
 | |
| 	return errors.New("invalid message")
 | |
| }
 | |
| 
 | |
| func (n Marshaler) String() string {
 | |
| 	return "bytes"
 | |
| }
 |