Skip to content

Commit

Permalink
Merge pull request #287 from rickyyangz/write_empty
Browse files Browse the repository at this point in the history
bugfix: fields with tag "write_empty" are not marshaled correctly
  • Loading branch information
Zaki Manian committed Sep 12, 2019
2 parents 2f5acf9 + b49e568 commit ccb336d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
4 changes: 2 additions & 2 deletions binary-encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ func (cdc *Codec) encodeReflectBinaryStruct(w io.Writer, info *TypeInfo, rv refl
var frv = rv.Field(field.Index)
var frvIsPtr = frv.Kind() == reflect.Ptr
var dfrv, isDefault = isDefaultValue(frv)
if isDefault && !fopts.WriteEmpty {
if isDefault && !field.WriteEmpty {
// Do not encode default value fields
// (except when `amino:"write_empty"` is set).
continue
Expand All @@ -452,7 +452,7 @@ func (cdc *Codec) encodeReflectBinaryStruct(w io.Writer, info *TypeInfo, rv refl
}
} else {
// write empty if explicitly set or if this is a pointer:
writeEmpty := fopts.WriteEmpty || frvIsPtr
writeEmpty := field.WriteEmpty || frvIsPtr
err = cdc.writeFieldIfNotEmpty(buf, field.BinFieldNum, finfo, fopts, field.FieldOptions, dfrv, writeEmpty, false)
if err != nil {
return
Expand Down
6 changes: 2 additions & 4 deletions binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,11 @@ func TestForceWriteEmpty(t *testing.T) {

b, err := cdc.MarshalBinaryBare(OuterWriteEmpty{})
assert.NoError(t, err)
assert.NotZero(t, len(b), "amino:\"write_empty\" did not work")
assert.Equal(t, []byte{10, 5, 13, 0, 0, 0, 0, 16, 0}, b)

b, err = cdc.MarshalBinaryBare(InnerWriteEmpty{})
assert.NoError(t, err)
t.Log(b)
// TODO(ismail): this alone won't be encoded:
//assert.NotZero(t, len(b), "amino:\"write_empty\" did not work")
assert.Equal(t, []byte{13, 0, 0, 0, 0}, b)
}

func TestStructSlice(t *testing.T) {
Expand Down

0 comments on commit ccb336d

Please sign in to comment.