Skip to content

Commit

Permalink
broker: replace Message.Body []byte slice to RawMessage
Browse files Browse the repository at this point in the history
Signed-off-by: Vasiliy Tolstov <[email protected]>
  • Loading branch information
vtolstov committed Jul 1, 2021
1 parent bd4d4c3 commit eb10702
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion broker/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package broker

import (
"context"
"errors"

"github.com/unistack-org/micro/v3/metadata"
)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit eb10702

Please sign in to comment.