@@ -18,6 +18,7 @@ import (
18
18
"go.mongodb.org/mongo-driver/bson/bsonrw"
19
19
"go.mongodb.org/mongo-driver/bson/bsonrw/bsonrwtest"
20
20
"go.mongodb.org/mongo-driver/bson/bsontype"
21
+ "go.mongodb.org/mongo-driver/bson/primitive"
21
22
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
22
23
)
23
24
@@ -199,12 +200,12 @@ func TestDecoderv2(t *testing.T) {
199
200
dc2 := bsoncodec.DecodeContext {Registry : NewRegistryBuilder ().Build ()}
200
201
dec , err := NewDecoderWithContext (dc1 , bsonrw .NewBSONDocumentReader ([]byte {}))
201
202
noerr (t , err )
202
- if dec .dc != dc1 {
203
+ if ! reflect . DeepEqual ( dec .dc , dc1 ) {
203
204
t .Errorf ("Decoder should use the Registry provided. got %v; want %v" , dec .dc , dc1 )
204
205
}
205
206
err = dec .SetContext (dc2 )
206
207
noerr (t , err )
207
- if dec .dc != dc2 {
208
+ if ! reflect . DeepEqual ( dec .dc , dc2 ) {
208
209
t .Errorf ("Decoder should use the Registry provided. got %v; want %v" , dec .dc , dc2 )
209
210
}
210
211
})
@@ -214,12 +215,12 @@ func TestDecoderv2(t *testing.T) {
214
215
dc2 := bsoncodec.DecodeContext {Registry : r2 }
215
216
dec , err := NewDecoder (bsonrw .NewBSONDocumentReader ([]byte {}))
216
217
noerr (t , err )
217
- if dec .dc != dc1 {
218
+ if ! reflect . DeepEqual ( dec .dc , dc1 ) {
218
219
t .Errorf ("Decoder should use the Registry provided. got %v; want %v" , dec .dc , dc1 )
219
220
}
220
221
err = dec .SetRegistry (r2 )
221
222
noerr (t , err )
222
- if dec .dc != dc2 {
223
+ if ! reflect . DeepEqual ( dec .dc , dc2 ) {
223
224
t .Errorf ("Decoder should use the Registry provided. got %v; want %v" , dec .dc , dc2 )
224
225
}
225
226
})
@@ -235,6 +236,85 @@ func TestDecoderv2(t *testing.T) {
235
236
t .Fatalf ("Decode error mismatch; expected %v, got %v" , ErrDecodeToNil , err )
236
237
}
237
238
})
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
+ })
238
318
}
239
319
240
320
type testUnmarshaler struct {
0 commit comments