From b49e5686804cf8bd0897575f020f53d846813697 Mon Sep 17 00:00:00 2001 From: rickyyangz Date: Mon, 9 Sep 2019 00:26:13 +0800 Subject: [PATCH] bugfix: make write_empty work --- binary-encode.go | 4 ++-- binary_test.go | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/binary-encode.go b/binary-encode.go index 0754f36b..879db992 100644 --- a/binary-encode.go +++ b/binary-encode.go @@ -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 @@ -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 diff --git a/binary_test.go b/binary_test.go index 532748d1..979539e5 100644 --- a/binary_test.go +++ b/binary_test.go @@ -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) {