Skip to content

Commit bce51d3

Browse files
author
Isabella Siu
committed
GODRIVER-657 handle nil in EmptyInterfaceEncodeValue
Change-Id: Icaab6196e15f85c4b069c89116ef95891b9612d4
1 parent 788afa3 commit bce51d3

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

bson/bsoncodec/default_value_encoders.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,9 @@ func (dve DefaultValueEncoders) SliceEncodeValue(ec EncodeContext, vw bsonrw.Val
444444

445445
// EmptyInterfaceEncodeValue is the ValueEncoderFunc for interface{}.
446446
func (dve DefaultValueEncoders) EmptyInterfaceEncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, i interface{}) error {
447+
if i == nil {
448+
return vw.WriteNull()
449+
}
447450
encoder, err := ec.LookupEncoder(reflect.TypeOf(i))
448451
if err != nil {
449452
return err

bson/bsoncodec/default_value_encoders_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,13 @@ func TestDefaultValueEncoders(t *testing.T) {
430430
{"*[]byte/nil", pbytesliceNil, nil, nil, bsonrwtest.WriteNull, nil},
431431
},
432432
},
433+
{
434+
"EmptyInterfaceEncodeValue",
435+
ValueEncoderFunc(dve.EmptyInterfaceEncodeValue),
436+
[]subtest{
437+
{"interface/nil", nil, nil, nil, bsonrwtest.WriteNull, nil},
438+
},
439+
},
433440
{
434441
"ValueMarshalerEncodeValue",
435442
ValueEncoderFunc(dve.ValueMarshalerEncodeValue),

0 commit comments

Comments
 (0)