From 17a41e3f9bf95a3fd2014a0fffab0e7e749ba709 Mon Sep 17 00:00:00 2001 From: oleksiys Date: Wed, 7 Aug 2019 16:17:07 -0700 Subject: [PATCH] Support encoding message payload passed as a pointer --- encode.go | 3 +++ encode_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/encode.go b/encode.go index c149777..f256229 100644 --- a/encode.go +++ b/encode.go @@ -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 { diff --git a/encode_test.go b/encode_test.go index 19dedb4..71fea9e 100644 --- a/encode_test.go +++ b/encode_test.go @@ -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)) }