@@ -23,8 +23,14 @@ import (
23
23
)
24
24
25
25
func TestBasicDecode (t * testing.T ) {
26
+ t .Parallel ()
27
+
26
28
for _ , tc := range unmarshalingTestCases () {
29
+ tc := tc
30
+
27
31
t .Run (tc .name , func (t * testing.T ) {
32
+ t .Parallel ()
33
+
28
34
got := reflect .New (tc .sType ).Elem ()
29
35
vr := bsonrw .NewBSONDocumentReader (tc .data )
30
36
reg := DefaultRegistry
@@ -38,9 +44,17 @@ func TestBasicDecode(t *testing.T) {
38
44
}
39
45
40
46
func TestDecoderv2 (t * testing.T ) {
47
+ t .Parallel ()
48
+
41
49
t .Run ("Decode" , func (t * testing.T ) {
50
+ t .Parallel ()
51
+
42
52
for _ , tc := range unmarshalingTestCases () {
53
+ tc := tc
54
+
43
55
t .Run (tc .name , func (t * testing.T ) {
56
+ t .Parallel ()
57
+
44
58
got := reflect .New (tc .sType ).Interface ()
45
59
vr := bsonrw .NewBSONDocumentReader (tc .data )
46
60
dec , err := NewDecoderWithContext (bsoncodec.DecodeContext {Registry : DefaultRegistry }, vr )
@@ -51,6 +65,8 @@ func TestDecoderv2(t *testing.T) {
51
65
})
52
66
}
53
67
t .Run ("lookup error" , func (t * testing.T ) {
68
+ t .Parallel ()
69
+
54
70
type certainlydoesntexistelsewhereihope func (string , string ) string
55
71
// Avoid unused code lint error.
56
72
_ = certainlydoesntexistelsewhereihope (func (string , string ) string { return "" })
@@ -63,6 +79,8 @@ func TestDecoderv2(t *testing.T) {
63
79
assert .Equal (t , want , got , "Received unexpected error." )
64
80
})
65
81
t .Run ("Unmarshaler" , func (t * testing.T ) {
82
+ t .Parallel ()
83
+
66
84
testCases := []struct {
67
85
name string
68
86
err error
@@ -90,7 +108,11 @@ func TestDecoderv2(t *testing.T) {
90
108
}
91
109
92
110
for _ , tc := range testCases {
111
+ tc := tc
112
+
93
113
t .Run (tc .name , func (t * testing.T ) {
114
+ t .Parallel ()
115
+
94
116
unmarshaler := & testUnmarshaler {err : tc .err }
95
117
dec , err := NewDecoder (tc .vr )
96
118
noerr (t , err )
@@ -110,6 +132,8 @@ func TestDecoderv2(t *testing.T) {
110
132
}
111
133
112
134
t .Run ("Unmarshaler/success bsonrw.ValueReader" , func (t * testing.T ) {
135
+ t .Parallel ()
136
+
113
137
want := bsoncore .BuildDocument (nil , bsoncore .AppendDoubleElement (nil , "pi" , 3.14159 ))
114
138
unmarshaler := & testUnmarshaler {}
115
139
vr := bsonrw .NewBSONDocumentReader (want )
@@ -125,14 +149,20 @@ func TestDecoderv2(t *testing.T) {
125
149
})
126
150
})
127
151
t .Run ("NewDecoder" , func (t * testing.T ) {
152
+ t .Parallel ()
153
+
128
154
t .Run ("error" , func (t * testing.T ) {
155
+ t .Parallel ()
156
+
129
157
_ , got := NewDecoder (nil )
130
158
want := errors .New ("cannot create a new Decoder with a nil ValueReader" )
131
159
if ! cmp .Equal (got , want , cmp .Comparer (compareErrors )) {
132
160
t .Errorf ("Was expecting error but got different error. got %v; want %v" , got , want )
133
161
}
134
162
})
135
163
t .Run ("success" , func (t * testing.T ) {
164
+ t .Parallel ()
165
+
136
166
got , err := NewDecoder (bsonrw .NewBSONDocumentReader ([]byte {}))
137
167
noerr (t , err )
138
168
if got == nil {
@@ -141,7 +171,11 @@ func TestDecoderv2(t *testing.T) {
141
171
})
142
172
})
143
173
t .Run ("NewDecoderWithContext" , func (t * testing.T ) {
174
+ t .Parallel ()
175
+
144
176
t .Run ("errors" , func (t * testing.T ) {
177
+ t .Parallel ()
178
+
145
179
dc := bsoncodec.DecodeContext {Registry : DefaultRegistry }
146
180
_ , got := NewDecoderWithContext (dc , nil )
147
181
want := errors .New ("cannot create a new Decoder with a nil ValueReader" )
@@ -150,6 +184,8 @@ func TestDecoderv2(t *testing.T) {
150
184
}
151
185
})
152
186
t .Run ("success" , func (t * testing.T ) {
187
+ t .Parallel ()
188
+
153
189
got , err := NewDecoderWithContext (bsoncodec.DecodeContext {}, bsonrw .NewBSONDocumentReader ([]byte {}))
154
190
noerr (t , err )
155
191
if got == nil {
@@ -164,6 +200,8 @@ func TestDecoderv2(t *testing.T) {
164
200
})
165
201
})
166
202
t .Run ("Decode doesn't zero struct" , func (t * testing.T ) {
203
+ t .Parallel ()
204
+
167
205
type foo struct {
168
206
Item string
169
207
Qty int
@@ -182,6 +220,8 @@ func TestDecoderv2(t *testing.T) {
182
220
assert .Equal (t , want , got , "Results do not match." )
183
221
})
184
222
t .Run ("Reset" , func (t * testing.T ) {
223
+ t .Parallel ()
224
+
185
225
vr1 , vr2 := bsonrw .NewBSONDocumentReader ([]byte {}), bsonrw .NewBSONDocumentReader ([]byte {})
186
226
dc := bsoncodec.DecodeContext {Registry : DefaultRegistry }
187
227
dec , err := NewDecoderWithContext (dc , vr1 )
@@ -196,6 +236,8 @@ func TestDecoderv2(t *testing.T) {
196
236
}
197
237
})
198
238
t .Run ("SetContext" , func (t * testing.T ) {
239
+ t .Parallel ()
240
+
199
241
dc1 := bsoncodec.DecodeContext {Registry : DefaultRegistry }
200
242
dc2 := bsoncodec.DecodeContext {Registry : NewRegistryBuilder ().Build ()}
201
243
dec , err := NewDecoderWithContext (dc1 , bsonrw .NewBSONDocumentReader ([]byte {}))
@@ -210,6 +252,8 @@ func TestDecoderv2(t *testing.T) {
210
252
}
211
253
})
212
254
t .Run ("SetRegistry" , func (t * testing.T ) {
255
+ t .Parallel ()
256
+
213
257
r1 , r2 := DefaultRegistry , NewRegistryBuilder ().Build ()
214
258
dc1 := bsoncodec.DecodeContext {Registry : r1 }
215
259
dc2 := bsoncodec.DecodeContext {Registry : r2 }
@@ -225,6 +269,8 @@ func TestDecoderv2(t *testing.T) {
225
269
}
226
270
})
227
271
t .Run ("DecodeToNil" , func (t * testing.T ) {
272
+ t .Parallel ()
273
+
228
274
data := docToBytes (D {{"item" , "canvas" }, {"qty" , 4 }})
229
275
vr := bsonrw .NewBSONDocumentReader (data )
230
276
dec , err := NewDecoder (vr )
@@ -236,7 +282,9 @@ func TestDecoderv2(t *testing.T) {
236
282
t .Fatalf ("Decode error mismatch; expected %v, got %v" , ErrDecodeToNil , err )
237
283
}
238
284
})
239
- t .Run ("SetDocumentType embedded map as empty interface" , func (t * testing.T ) {
285
+ t .Run ("DefaultDocuemntD embedded map as empty interface" , func (t * testing.T ) {
286
+ t .Parallel ()
287
+
240
288
type someMap map [string ]interface {}
241
289
242
290
in := make (someMap )
@@ -267,7 +315,9 @@ func TestDecoderv2(t *testing.T) {
267
315
bsonFooOutType := reflect .TypeOf (bsonOut ["foo" ])
268
316
assert .Equal (t , mType , bsonFooOutType , "expected %v to equal %v" , mType .String (), bsonFooOutType .String ())
269
317
})
270
- t .Run ("SetDocumentType for decoding into interface{} alias" , func (t * testing.T ) {
318
+ t .Run ("DefaultDocuemntD for decoding into interface{} alias" , func (t * testing.T ) {
319
+ t .Parallel ()
320
+
271
321
var in interface {} = map [string ]interface {}{"bar" : "baz" }
272
322
273
323
bytes , err := Marshal (in )
@@ -291,7 +341,9 @@ func TestDecoderv2(t *testing.T) {
291
341
assert .Equal (t , dType , bsonOutType ,
292
342
"expected %v to equal %v" , dType .String (), bsonOutType .String ())
293
343
})
294
- t .Run ("SetDocumentType for decoding into non-interface{} alias" , func (t * testing.T ) {
344
+ t .Run ("DefaultDocuemntD for decoding into non-interface{} alias" , func (t * testing.T ) {
345
+ t .Parallel ()
346
+
295
347
var in interface {} = map [string ]interface {}{"bar" : "baz" }
296
348
297
349
bytes , err := Marshal (in )
@@ -315,6 +367,59 @@ func TestDecoderv2(t *testing.T) {
315
367
assert .NotEqual (t , dType , bsonOutType ,
316
368
"expected %v to not equal %v" , dType .String (), bsonOutType .String ())
317
369
})
370
+ t .Run ("DefaultDocumentD for deep struct values" , func (t * testing.T ) {
371
+ t .Parallel ()
372
+
373
+ type emb struct {
374
+ Foo map [int ]interface {} `bson:"foo"`
375
+ }
376
+
377
+ objID := primitive .NewObjectID ()
378
+
379
+ in := emb {
380
+ Foo : map [int ]interface {}{
381
+ 1 : map [string ]interface {}{"bar" : "baz" },
382
+ 2 : map [int ]interface {}{
383
+ 3 : map [string ]interface {}{"bar" : "baz" },
384
+ },
385
+ 4 : map [primitive.ObjectID ]interface {}{
386
+ objID : map [string ]interface {}{"bar" : "baz" },
387
+ },
388
+ },
389
+ }
390
+
391
+ bytes , err := Marshal (in )
392
+ if err != nil {
393
+ t .Fatal (err )
394
+ }
395
+
396
+ dec , err := NewDecoder (bsonrw .NewBSONDocumentReader (bytes ))
397
+ if err != nil {
398
+ t .Fatal (err )
399
+ }
400
+
401
+ dec .DefaultDocumentD ()
402
+
403
+ var out emb
404
+ if err := dec .Decode (& out ); err != nil {
405
+ t .Fatal (err )
406
+ }
407
+
408
+ mType := reflect .TypeOf (primitive.M {})
409
+ bsonOutType := reflect .TypeOf (out )
410
+ assert .NotEqual (t , mType , bsonOutType ,
411
+ "expected %v to not equal %v" , mType .String (), bsonOutType .String ())
412
+
413
+ want := emb {
414
+ Foo : map [int ]interface {}{
415
+ 1 : primitive.D {{Key : "bar" , Value : "baz" }},
416
+ 2 : primitive.D {{Key : "3" , Value : primitive.D {{Key : "bar" , Value : "baz" }}}},
417
+ 4 : primitive.D {{Key : objID .Hex (), Value : primitive.D {{Key : "bar" , Value : "baz" }}}},
418
+ },
419
+ }
420
+
421
+ assert .Equal (t , want , out , "expected %v, got %v" , want , out )
422
+ })
318
423
}
319
424
320
425
type testUnmarshaler struct {
0 commit comments