Skip to content

Commit 6119f96

Browse files
GODRIVER-2766 support inherited defaultDocumentType (#1202)
1 parent b6bdeda commit 6119f96

File tree

2 files changed

+114
-4
lines changed

2 files changed

+114
-4
lines changed

bson/bsoncodec/struct_codec.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,12 @@ func (sc *StructCodec) DecodeValue(r DecodeContext, vr bsonrw.ValueReader, val r
326326
}
327327
field = field.Addr()
328328

329-
dctx := DecodeContext{Registry: r.Registry, Truncate: fd.truncate || r.Truncate}
329+
dctx := DecodeContext{
330+
Registry: r.Registry,
331+
Truncate: fd.truncate || r.Truncate,
332+
defaultDocumentType: r.defaultDocumentType,
333+
}
334+
330335
if fd.decoder == nil {
331336
return newDecodeError(fd.name, ErrNoDecoder{Type: field.Elem().Type()})
332337
}

bson/decoder_test.go

Lines changed: 108 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,14 @@ import (
2323
)
2424

2525
func TestBasicDecode(t *testing.T) {
26+
t.Parallel()
27+
2628
for _, tc := range unmarshalingTestCases() {
29+
tc := tc
30+
2731
t.Run(tc.name, func(t *testing.T) {
32+
t.Parallel()
33+
2834
got := reflect.New(tc.sType).Elem()
2935
vr := bsonrw.NewBSONDocumentReader(tc.data)
3036
reg := DefaultRegistry
@@ -38,9 +44,17 @@ func TestBasicDecode(t *testing.T) {
3844
}
3945

4046
func TestDecoderv2(t *testing.T) {
47+
t.Parallel()
48+
4149
t.Run("Decode", func(t *testing.T) {
50+
t.Parallel()
51+
4252
for _, tc := range unmarshalingTestCases() {
53+
tc := tc
54+
4355
t.Run(tc.name, func(t *testing.T) {
56+
t.Parallel()
57+
4458
got := reflect.New(tc.sType).Interface()
4559
vr := bsonrw.NewBSONDocumentReader(tc.data)
4660
dec, err := NewDecoderWithContext(bsoncodec.DecodeContext{Registry: DefaultRegistry}, vr)
@@ -51,6 +65,8 @@ func TestDecoderv2(t *testing.T) {
5165
})
5266
}
5367
t.Run("lookup error", func(t *testing.T) {
68+
t.Parallel()
69+
5470
type certainlydoesntexistelsewhereihope func(string, string) string
5571
// Avoid unused code lint error.
5672
_ = certainlydoesntexistelsewhereihope(func(string, string) string { return "" })
@@ -63,6 +79,8 @@ func TestDecoderv2(t *testing.T) {
6379
assert.Equal(t, want, got, "Received unexpected error.")
6480
})
6581
t.Run("Unmarshaler", func(t *testing.T) {
82+
t.Parallel()
83+
6684
testCases := []struct {
6785
name string
6886
err error
@@ -90,7 +108,11 @@ func TestDecoderv2(t *testing.T) {
90108
}
91109

92110
for _, tc := range testCases {
111+
tc := tc
112+
93113
t.Run(tc.name, func(t *testing.T) {
114+
t.Parallel()
115+
94116
unmarshaler := &testUnmarshaler{err: tc.err}
95117
dec, err := NewDecoder(tc.vr)
96118
noerr(t, err)
@@ -110,6 +132,8 @@ func TestDecoderv2(t *testing.T) {
110132
}
111133

112134
t.Run("Unmarshaler/success bsonrw.ValueReader", func(t *testing.T) {
135+
t.Parallel()
136+
113137
want := bsoncore.BuildDocument(nil, bsoncore.AppendDoubleElement(nil, "pi", 3.14159))
114138
unmarshaler := &testUnmarshaler{}
115139
vr := bsonrw.NewBSONDocumentReader(want)
@@ -125,14 +149,20 @@ func TestDecoderv2(t *testing.T) {
125149
})
126150
})
127151
t.Run("NewDecoder", func(t *testing.T) {
152+
t.Parallel()
153+
128154
t.Run("error", func(t *testing.T) {
155+
t.Parallel()
156+
129157
_, got := NewDecoder(nil)
130158
want := errors.New("cannot create a new Decoder with a nil ValueReader")
131159
if !cmp.Equal(got, want, cmp.Comparer(compareErrors)) {
132160
t.Errorf("Was expecting error but got different error. got %v; want %v", got, want)
133161
}
134162
})
135163
t.Run("success", func(t *testing.T) {
164+
t.Parallel()
165+
136166
got, err := NewDecoder(bsonrw.NewBSONDocumentReader([]byte{}))
137167
noerr(t, err)
138168
if got == nil {
@@ -141,7 +171,11 @@ func TestDecoderv2(t *testing.T) {
141171
})
142172
})
143173
t.Run("NewDecoderWithContext", func(t *testing.T) {
174+
t.Parallel()
175+
144176
t.Run("errors", func(t *testing.T) {
177+
t.Parallel()
178+
145179
dc := bsoncodec.DecodeContext{Registry: DefaultRegistry}
146180
_, got := NewDecoderWithContext(dc, nil)
147181
want := errors.New("cannot create a new Decoder with a nil ValueReader")
@@ -150,6 +184,8 @@ func TestDecoderv2(t *testing.T) {
150184
}
151185
})
152186
t.Run("success", func(t *testing.T) {
187+
t.Parallel()
188+
153189
got, err := NewDecoderWithContext(bsoncodec.DecodeContext{}, bsonrw.NewBSONDocumentReader([]byte{}))
154190
noerr(t, err)
155191
if got == nil {
@@ -164,6 +200,8 @@ func TestDecoderv2(t *testing.T) {
164200
})
165201
})
166202
t.Run("Decode doesn't zero struct", func(t *testing.T) {
203+
t.Parallel()
204+
167205
type foo struct {
168206
Item string
169207
Qty int
@@ -182,6 +220,8 @@ func TestDecoderv2(t *testing.T) {
182220
assert.Equal(t, want, got, "Results do not match.")
183221
})
184222
t.Run("Reset", func(t *testing.T) {
223+
t.Parallel()
224+
185225
vr1, vr2 := bsonrw.NewBSONDocumentReader([]byte{}), bsonrw.NewBSONDocumentReader([]byte{})
186226
dc := bsoncodec.DecodeContext{Registry: DefaultRegistry}
187227
dec, err := NewDecoderWithContext(dc, vr1)
@@ -196,6 +236,8 @@ func TestDecoderv2(t *testing.T) {
196236
}
197237
})
198238
t.Run("SetContext", func(t *testing.T) {
239+
t.Parallel()
240+
199241
dc1 := bsoncodec.DecodeContext{Registry: DefaultRegistry}
200242
dc2 := bsoncodec.DecodeContext{Registry: NewRegistryBuilder().Build()}
201243
dec, err := NewDecoderWithContext(dc1, bsonrw.NewBSONDocumentReader([]byte{}))
@@ -210,6 +252,8 @@ func TestDecoderv2(t *testing.T) {
210252
}
211253
})
212254
t.Run("SetRegistry", func(t *testing.T) {
255+
t.Parallel()
256+
213257
r1, r2 := DefaultRegistry, NewRegistryBuilder().Build()
214258
dc1 := bsoncodec.DecodeContext{Registry: r1}
215259
dc2 := bsoncodec.DecodeContext{Registry: r2}
@@ -225,6 +269,8 @@ func TestDecoderv2(t *testing.T) {
225269
}
226270
})
227271
t.Run("DecodeToNil", func(t *testing.T) {
272+
t.Parallel()
273+
228274
data := docToBytes(D{{"item", "canvas"}, {"qty", 4}})
229275
vr := bsonrw.NewBSONDocumentReader(data)
230276
dec, err := NewDecoder(vr)
@@ -236,7 +282,9 @@ func TestDecoderv2(t *testing.T) {
236282
t.Fatalf("Decode error mismatch; expected %v, got %v", ErrDecodeToNil, err)
237283
}
238284
})
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+
240288
type someMap map[string]interface{}
241289

242290
in := make(someMap)
@@ -267,7 +315,9 @@ func TestDecoderv2(t *testing.T) {
267315
bsonFooOutType := reflect.TypeOf(bsonOut["foo"])
268316
assert.Equal(t, mType, bsonFooOutType, "expected %v to equal %v", mType.String(), bsonFooOutType.String())
269317
})
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+
271321
var in interface{} = map[string]interface{}{"bar": "baz"}
272322

273323
bytes, err := Marshal(in)
@@ -291,7 +341,9 @@ func TestDecoderv2(t *testing.T) {
291341
assert.Equal(t, dType, bsonOutType,
292342
"expected %v to equal %v", dType.String(), bsonOutType.String())
293343
})
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+
295347
var in interface{} = map[string]interface{}{"bar": "baz"}
296348

297349
bytes, err := Marshal(in)
@@ -315,6 +367,59 @@ func TestDecoderv2(t *testing.T) {
315367
assert.NotEqual(t, dType, bsonOutType,
316368
"expected %v to not equal %v", dType.String(), bsonOutType.String())
317369
})
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+
})
318423
}
319424

320425
type testUnmarshaler struct {

0 commit comments

Comments
 (0)