Skip to content

Commit

Permalink
Support encoding message payload passed as a pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksiys authored and smira committed Aug 8, 2019
1 parent 76b04f5 commit 17a41e3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ func (e *Encoder) Encode(v interface{}) (err error) {
func (e *Encoder) encodeValue(f field, rt reflect.Type, rv reflect.Value) (err error) {
if rv.Kind() == reflect.Interface && !rv.IsNil() {
rv = rv.Elem()
if rv.Kind() == reflect.Ptr {
rv = rv.Elem()
}
rt = rv.Type()

switch rt {
Expand Down
24 changes: 24 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,30 @@ func (s *EncoderSuite) TestEncodeMessageGet() {
s.Assert().EqualValues(messageGet, buf.Bytes())
}

func (s *EncoderSuite) TestEncodeMessageGetPointer() {
var buf bytes.Buffer

getRequest := Request{
Header: RequestHeader{
Version: ProtocolVersion{Major: 1, Minor: 1},
BatchCount: 1,
},
BatchItems: []RequestBatchItem{
{
Operation: OPERATION_GET,
RequestPayload: &GetRequest{
UniqueIdentifier: "49a1ca88-6bea-4fb2-b450-7e58802c3038",
},
},
},
}

err := NewEncoder(&buf).Encode(&getRequest)
s.Assert().NoError(err)

s.Assert().EqualValues(messageGet, buf.Bytes())
}

func TestEncoderSuite(t *testing.T) {
suite.Run(t, new(EncoderSuite))
}

0 comments on commit 17a41e3

Please sign in to comment.