Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: fields with tag "write_empty" are not marshaled correctly #287

Merged
merged 1 commit into from
Sep 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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