From a3a434d923eaa1b1905462c7f0fa26a2d3b82e6f Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Sun, 2 Apr 2023 14:10:57 +0300 Subject: [PATCH] move RawMessage to codec package Signed-off-by: Vasiliy Tolstov --- broker/broker.go | 24 ++---------------------- codec/codec.go | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/broker/broker.go b/broker/broker.go index 5fa134e8..f95de4b4 100644 --- a/broker/broker.go +++ b/broker/broker.go @@ -5,6 +5,7 @@ import ( "context" "errors" + "go.unistack.org/micro/v3/codec" "go.unistack.org/micro/v3/metadata" ) @@ -85,33 +86,12 @@ type Event interface { 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 type Message struct { // Header contains message metadata Header metadata.Metadata // Body contains message body - Body RawMessage + Body codec.RawMessage } // NewMessage create broker message with topic filled diff --git a/codec/codec.go b/codec/codec.go index 89a0bdec..20231315 100644 --- a/codec/codec.go +++ b/codec/codec.go @@ -84,3 +84,24 @@ func MarshalAppend(buf []byte, c Codec, v interface{}, opts ...Option) ([]byte, 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 +} -- 2.45.2