@@ -18,6 +18,7 @@ import (
1818 "go.mongodb.org/mongo-driver/bson/bsonrw"
1919 "go.mongodb.org/mongo-driver/bson/bsonrw/bsonrwtest"
2020 "go.mongodb.org/mongo-driver/bson/bsontype"
21+ "go.mongodb.org/mongo-driver/bson/primitive"
2122 "go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
2223)
2324
@@ -199,12 +200,12 @@ func TestDecoderv2(t *testing.T) {
199200 dc2 := bsoncodec.DecodeContext {Registry : NewRegistryBuilder ().Build ()}
200201 dec , err := NewDecoderWithContext (dc1 , bsonrw .NewBSONDocumentReader ([]byte {}))
201202 noerr (t , err )
202- if dec .dc != dc1 {
203+ if ! reflect . DeepEqual ( dec .dc , dc1 ) {
203204 t .Errorf ("Decoder should use the Registry provided. got %v; want %v" , dec .dc , dc1 )
204205 }
205206 err = dec .SetContext (dc2 )
206207 noerr (t , err )
207- if dec .dc != dc2 {
208+ if ! reflect . DeepEqual ( dec .dc , dc2 ) {
208209 t .Errorf ("Decoder should use the Registry provided. got %v; want %v" , dec .dc , dc2 )
209210 }
210211 })
@@ -214,12 +215,12 @@ func TestDecoderv2(t *testing.T) {
214215 dc2 := bsoncodec.DecodeContext {Registry : r2 }
215216 dec , err := NewDecoder (bsonrw .NewBSONDocumentReader ([]byte {}))
216217 noerr (t , err )
217- if dec .dc != dc1 {
218+ if ! reflect . DeepEqual ( dec .dc , dc1 ) {
218219 t .Errorf ("Decoder should use the Registry provided. got %v; want %v" , dec .dc , dc1 )
219220 }
220221 err = dec .SetRegistry (r2 )
221222 noerr (t , err )
222- if dec .dc != dc2 {
223+ if ! reflect . DeepEqual ( dec .dc , dc2 ) {
223224 t .Errorf ("Decoder should use the Registry provided. got %v; want %v" , dec .dc , dc2 )
224225 }
225226 })
@@ -235,6 +236,85 @@ func TestDecoderv2(t *testing.T) {
235236 t .Fatalf ("Decode error mismatch; expected %v, got %v" , ErrDecodeToNil , err )
236237 }
237238 })
239+ t .Run ("SetDocumentType embedded map as empty interface" , func (t * testing.T ) {
240+ type someMap map [string ]interface {}
241+
242+ in := make (someMap )
243+ in ["foo" ] = map [string ]interface {}{"bar" : "baz" }
244+
245+ bytes , err := Marshal (in )
246+ if err != nil {
247+ t .Fatal (err )
248+ }
249+
250+ var bsonOut someMap
251+ dec , err := NewDecoder (bsonrw .NewBSONDocumentReader (bytes ))
252+ if err != nil {
253+ t .Fatal (err )
254+ }
255+ dec .DefaultDocumentM ()
256+ if err := dec .Decode (& bsonOut ); err != nil {
257+ t .Fatal (err )
258+ }
259+
260+ // Ensure that interface{}-typed top-level data is converted to the document type.
261+ bsonOutType := reflect .TypeOf (bsonOut )
262+ inType := reflect .TypeOf (in )
263+ assert .Equal (t , inType , bsonOutType , "expected %v to equal %v" , inType .String (), bsonOutType .String ())
264+
265+ // Ensure that the embedded type is a primitive map.
266+ mType := reflect .TypeOf (primitive.M {})
267+ bsonFooOutType := reflect .TypeOf (bsonOut ["foo" ])
268+ assert .Equal (t , mType , bsonFooOutType , "expected %v to equal %v" , mType .String (), bsonFooOutType .String ())
269+ })
270+ t .Run ("SetDocumentType for decoding into interface{} alias" , func (t * testing.T ) {
271+ var in interface {} = map [string ]interface {}{"bar" : "baz" }
272+
273+ bytes , err := Marshal (in )
274+ if err != nil {
275+ t .Fatal (err )
276+ }
277+
278+ var bsonOut interface {}
279+ dec , err := NewDecoder (bsonrw .NewBSONDocumentReader (bytes ))
280+ if err != nil {
281+ t .Fatal (err )
282+ }
283+ dec .DefaultDocumentD ()
284+ if err := dec .Decode (& bsonOut ); err != nil {
285+ t .Fatal (err )
286+ }
287+
288+ // Ensure that interface{}-typed top-level data is converted to the document type.
289+ dType := reflect .TypeOf (primitive.D {})
290+ bsonOutType := reflect .TypeOf (bsonOut )
291+ assert .Equal (t , dType , bsonOutType ,
292+ "expected %v to equal %v" , dType .String (), bsonOutType .String ())
293+ })
294+ t .Run ("SetDocumentType for decoding into non-interface{} alias" , func (t * testing.T ) {
295+ var in interface {} = map [string ]interface {}{"bar" : "baz" }
296+
297+ bytes , err := Marshal (in )
298+ if err != nil {
299+ t .Fatal (err )
300+ }
301+
302+ var bsonOut struct {}
303+ dec , err := NewDecoder (bsonrw .NewBSONDocumentReader (bytes ))
304+ if err != nil {
305+ t .Fatal (err )
306+ }
307+ dec .DefaultDocumentD ()
308+ if err := dec .Decode (& bsonOut ); err != nil {
309+ t .Fatal (err )
310+ }
311+
312+ // Ensure that typed top-level data is not converted to the document type.
313+ dType := reflect .TypeOf (primitive.D {})
314+ bsonOutType := reflect .TypeOf (bsonOut )
315+ assert .NotEqual (t , dType , bsonOutType ,
316+ "expected %v to not equal %v" , dType .String (), bsonOutType .String ())
317+ })
238318}
239319
240320type testUnmarshaler struct {
0 commit comments