move RawMessage to codec package #208
| @@ -5,6 +5,7 @@ import ( | |||||||
| 	"context" | 	"context" | ||||||
| 	"errors" | 	"errors" | ||||||
|  |  | ||||||
|  | 	"go.unistack.org/micro/v3/codec" | ||||||
| 	"go.unistack.org/micro/v3/metadata" | 	"go.unistack.org/micro/v3/metadata" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| @@ -85,33 +86,12 @@ type Event interface { | |||||||
| 	SetError(err error) | 	SetError(err error) | ||||||
| } | } | ||||||
|  |  | ||||||
| // RawMessage is a raw encoded JSON value. |  | ||||||
| // It implements Marshaler and Unmarshaler and can be used to delay decoding or precompute a encoding. |  | ||||||
| type RawMessage []byte |  | ||||||
|  |  | ||||||
| // MarshalJSON returns m as the JSON encoding of m. |  | ||||||
| func (m *RawMessage) MarshalJSON() ([]byte, error) { |  | ||||||
| 	if m == nil { |  | ||||||
| 		return []byte("null"), nil |  | ||||||
| 	} |  | ||||||
| 	return *m, nil |  | ||||||
| } |  | ||||||
|  |  | ||||||
| // UnmarshalJSON sets *m to a copy of data. |  | ||||||
| func (m *RawMessage) UnmarshalJSON(data []byte) error { |  | ||||||
| 	if m == nil { |  | ||||||
| 		return errors.New("RawMessage UnmarshalJSON on nil pointer") |  | ||||||
| 	} |  | ||||||
| 	*m = append((*m)[0:0], data...) |  | ||||||
| 	return nil |  | ||||||
| } |  | ||||||
|  |  | ||||||
| // Message is used to transfer data | // Message is used to transfer data | ||||||
| type Message struct { | type Message struct { | ||||||
| 	// Header contains message metadata | 	// Header contains message metadata | ||||||
| 	Header metadata.Metadata | 	Header metadata.Metadata | ||||||
| 	// Body contains message body | 	// Body contains message body | ||||||
| 	Body RawMessage | 	Body codec.RawMessage | ||||||
| } | } | ||||||
|  |  | ||||||
| // NewMessage create broker message with topic filled | // NewMessage create broker message with topic filled | ||||||
|   | |||||||
| @@ -84,3 +84,24 @@ func MarshalAppend(buf []byte, c Codec, v interface{}, opts ...Option) ([]byte, | |||||||
|  |  | ||||||
| 	return append(buf, mbuf...), nil | 	return append(buf, mbuf...), nil | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // RawMessage is a raw encoded JSON value. | ||||||
|  | // It implements Marshaler and Unmarshaler and can be used to delay decoding or precompute a encoding. | ||||||
|  | type RawMessage []byte | ||||||
|  |  | ||||||
|  | // MarshalJSON returns m as the JSON encoding of m. | ||||||
|  | func (m *RawMessage) MarshalJSON() ([]byte, error) { | ||||||
|  | 	if m == nil { | ||||||
|  | 		return []byte("null"), nil | ||||||
|  | 	} | ||||||
|  | 	return *m, nil | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // UnmarshalJSON sets *m to a copy of data. | ||||||
|  | func (m *RawMessage) UnmarshalJSON(data []byte) error { | ||||||
|  | 	if m == nil { | ||||||
|  | 		return errors.New("RawMessage UnmarshalJSON on nil pointer") | ||||||
|  | 	} | ||||||
|  | 	*m = append((*m)[0:0], data...) | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user