Skip to content

Commit

Permalink
codec: add MarshalAppend func
Browse files Browse the repository at this point in the history
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
  • Loading branch information
vtolstov committed Sep 30, 2021
1 parent 3247d14 commit 8abc913
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions codec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,20 @@ type Message struct {
func NewMessage(t MessageType) *Message {
return &Message{Type: t, Header: metadata.New(0)}
}

// MarshalAppend calls codec.Marshal(v) and returns the data appended to buf.
// If codec implements MarshalAppend, that is called instead.
func MarshalAppend(buf []byte, c Codec, v interface{}, opts ...Option) ([]byte, error) {
if nc, ok := c.(interface {
MarshalAppend([]byte, interface{}, ...Option) ([]byte, error)
}); ok {
return nc.MarshalAppend(buf, v, opts...)
}

mbuf, err := c.Marshal(v, opts...)
if err != nil {
return nil, err
}

return append(buf, mbuf...), nil
}

0 comments on commit 8abc913

Please sign in to comment.