Skip to content

Commit fe5e475

Browse files
iwysiuDivjot Arora
authored andcommitted
GODRIVER-1349 fix omitEmpty for interface{} (#249)
1 parent 53ecaa3 commit fe5e475

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

bson/bsoncodec/struct_codec.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,17 @@ func (sc *StructCodec) EncodeValue(r EncodeContext, vw bsonrw.ValueWriter, val r
127127

128128
encoder := desc.encoder
129129

130-
iszero := sc.isZero
131-
if iz, ok := encoder.(CodecZeroer); ok {
132-
iszero = iz.IsTypeZero
130+
var isZero bool
131+
rvInterface := rv.Interface()
132+
if cz, ok := encoder.(CodecZeroer); ok {
133+
isZero = cz.IsTypeZero(rvInterface)
134+
} else if rv.Kind() == reflect.Interface {
135+
// sc.isZero will not treat an interface rv as an interface, so we need to check for the zero interface separately.
136+
isZero = rv.IsNil()
137+
} else {
138+
isZero = sc.isZero(rvInterface)
133139
}
134-
135-
if desc.omitEmpty && iszero(rv.Interface()) {
140+
if desc.omitEmpty && isZero {
136141
continue
137142
}
138143

bson/mgocompat/bson_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1404,7 +1404,7 @@ var twoWayCrossItems = []crossTypeItem{
14041404
{&condMap{map[string]int{"k": 1}}, bson.M{"v": bson.M{"k": 1}}},
14051405
{&condMap{}, map[string][]string{}},
14061406
{&condIface{"yo"}, map[string]string{"v": "yo"}},
1407-
// {&condIface{""}, map[string]string{"v": ""}},
1407+
{&condIface{""}, map[string]string{"v": ""}},
14081408
{&condIface{}, map[string]string{}},
14091409
{&condPtr{&truevar}, map[string]bool{"v": true}},
14101410
{&condPtr{&falsevar}, map[string]bool{"v": false}},

0 commit comments

Comments
 (0)