From eb107020c743ca84502740578f63975e11016933 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Thu, 1 Jul 2021 15:10:43 +0300 Subject: [PATCH] broker: replace Message.Body []byte slice to RawMessage Signed-off-by: Vasiliy Tolstov --- broker/broker.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/broker/broker.go b/broker/broker.go index aac5f140..9b5a2430 100644 --- a/broker/broker.go +++ b/broker/broker.go @@ -3,6 +3,7 @@ package broker import ( "context" + "errors" "github.com/unistack-org/micro/v3/metadata" ) @@ -34,10 +35,31 @@ type Event interface { Error() 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 nil, 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 type Message struct { Header metadata.Metadata // contains message metadata - Body []byte // contains message body + Body RawMessage // contains message body } // Subscriber is a convenience return type for the Subscribe method