Skip to content
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
12 changes: 10 additions & 2 deletions bson/array_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,16 @@ func (ac *arrayCodec) DecodeValue(_ DecodeContext, vr ValueReader, val reflect.V
if !val.CanSet() || val.Type() != tCoreArray {
return ValueDecoderError{Name: "CoreArrayDecodeValue", Types: []reflect.Type{tCoreArray}, Received: val}
}
if vrType := vr.Type(); vrType != TypeArray {
return fmt.Errorf("cannot decode %v into a %s", vrType, val.Type())
switch vr.Type() {
case TypeArray:
case TypeNull:
val.Set(reflect.Zero(val.Type()))
return vr.ReadNull()
case TypeUndefined:
val.Set(reflect.Zero(val.Type()))
return vr.ReadUndefined()
default:
return fmt.Errorf("cannot decode %v into a %s", vr.Type(), val.Type())
}

if val.IsNil() {
Expand Down
15 changes: 12 additions & 3 deletions bson/default_value_decoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ func dDecodeValue(dc DecodeContext, vr ValueReader, val reflect.Value) error {
case TypeNull:
val.Set(reflect.Zero(val.Type()))
return vr.ReadNull()
case TypeUndefined:
val.Set(reflect.Zero(val.Type()))
return vr.ReadUndefined()
default:
return fmt.Errorf("cannot decode %v into a D", vrType)
}
Expand Down Expand Up @@ -1310,9 +1313,15 @@ func coreDocumentDecodeValue(_ DecodeContext, vr ValueReader, val reflect.Value)
if !val.CanSet() || val.Type() != tCoreDocument {
return ValueDecoderError{Name: "CoreDocumentDecodeValue", Types: []reflect.Type{tCoreDocument}, Received: val}
}
vrType := vr.Type()
isDocument := vrType == Type(0) || vrType == TypeEmbeddedDocument || vrType == TypeArray
if !isDocument {
switch vrType := vr.Type(); vrType {
case Type(0), TypeEmbeddedDocument, TypeArray:
case TypeNull:
val.Set(reflect.Zero(val.Type()))
return vr.ReadNull()
case TypeUndefined:
val.Set(reflect.Zero(val.Type()))
return vr.ReadUndefined()
default:
return fmt.Errorf("cannot decode %v into a %s", vrType, val.Type())
}

Expand Down
32 changes: 32 additions & 0 deletions bson/default_value_decoders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2302,6 +2302,22 @@ func TestDefaultValueDecoders(t *testing.T) {
readDocument,
errors.New("copy error"),
},
{
"decode null",
bsoncore.Document(nil),
nil,
&valueReaderWriter{BSONType: TypeNull},
readNull,
nil,
},
{
"decode undefined",
bsoncore.Document(nil),
nil,
&valueReaderWriter{BSONType: TypeUndefined},
readUndefined,
nil,
},
},
},
{
Expand Down Expand Up @@ -2423,6 +2439,22 @@ func TestDefaultValueDecoders(t *testing.T) {
Received: reflect.New(reflect.TypeOf((*bsoncore.Array)(nil))).Elem(),
},
},
{
"decode null",
bsoncore.Array(nil),
nil,
&valueReaderWriter{BSONType: TypeNull},
readNull,
nil,
},
{
"decode undefined",
bsoncore.Array(nil),
nil,
&valueReaderWriter{BSONType: TypeUndefined},
readUndefined,
nil,
},
},
},
}
Expand Down
12 changes: 9 additions & 3 deletions bson/primitive_codecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,15 @@ func rawDecodeValue(_ DecodeContext, vr ValueReader, val reflect.Value) error {
if !val.CanSet() || val.Type() != tRaw {
return ValueDecoderError{Name: "RawDecodeValue", Types: []reflect.Type{tRaw}, Received: val}
}
vrType := vr.Type()
isDocument := vrType == Type(0) || vrType == TypeEmbeddedDocument || vrType == TypeArray
if !isDocument {
switch vrType := vr.Type(); vrType {
case Type(0), TypeEmbeddedDocument, TypeArray:
case TypeNull:
val.Set(reflect.Zero(val.Type()))
return vr.ReadNull()
case TypeUndefined:
val.Set(reflect.Zero(val.Type()))
return vr.ReadUndefined()
default:
return fmt.Errorf("cannot decode %v into a %s", vrType, val.Type())
}

Expand Down
16 changes: 16 additions & 0 deletions bson/primitive_codecs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,22 @@ func TestPrimitiveValueDecoders(t *testing.T) {
readDocument,
errors.New("copy error"),
},
{
"decode null",
Raw(nil),
nil,
&valueReaderWriter{BSONType: TypeNull},
readNull,
nil,
},
{
"decode undefined",
Raw(nil),
nil,
&valueReaderWriter{BSONType: TypeUndefined},
readUndefined,
nil,
},
},
},
}
Expand Down
50 changes: 50 additions & 0 deletions bson/unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -931,5 +931,55 @@ func TestUnmarshalTypeCompatibility(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, want, []byte(val.Foo))
})
t.Run("bson.D into bson.Raw", func(t *testing.T) {
t.Parallel()

data := docToBytes(D{{"foo", D{{"bar", int32(42)}}}})
want := []byte(bsoncore.NewDocumentBuilder().AppendInt32("bar", 42).Build())

var val struct {
Foo Raw
}

err := Unmarshal(data, &val)
assert.NoError(t, err)
assert.Equal(t, want, []byte(val.Foo))
})
t.Run("nil into bson.Raw", func(t *testing.T) {
t.Parallel()

data := docToBytes(D{{"foo", nil}})

var val struct {
Foo Raw
}
err := Unmarshal(data, &val)
assert.NoError(t, err)
assert.Nil(t, val.Foo)
})
t.Run("nil into bsoncore.Document", func(t *testing.T) {
t.Parallel()

data := docToBytes(D{{"foo", nil}})

var val struct {
Foo bsoncore.Document
}
err := Unmarshal(data, &val)
assert.NoError(t, err)
assert.Nil(t, val.Foo)
})
t.Run("nil into bsoncore.Array", func(t *testing.T) {
t.Parallel()

data := docToBytes(D{{"foo", nil}})

var val struct {
Foo bsoncore.Array
}
err := Unmarshal(data, &val)
assert.NoError(t, err)
assert.Nil(t, val.Foo)
})
})
}
Loading