Skip to content

Commit 7bd7754

Browse files
author
Isabella Siu
committed
GODRIVER-599 valueEncoders should check if pointer is nil
Change-Id: I26cb2b0cd11a087c4ea285116b94d1451a4d337b
1 parent 745f04e commit 7bd7754

File tree

4 files changed

+131
-3
lines changed

4 files changed

+131
-3
lines changed

bson/bsoncodec/default_value_encoders.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ func (dve DefaultValueEncoders) ObjectIDEncodeValue(ec EncodeContext, vw bsonrw.
207207
case objectid.ObjectID:
208208
oid = t
209209
case *objectid.ObjectID:
210+
if t == nil {
211+
return vw.WriteNull()
212+
}
210213
oid = *t
211214
default:
212215
return ValueEncoderError{
@@ -226,6 +229,9 @@ func (dve DefaultValueEncoders) Decimal128EncodeValue(ec EncodeContext, vw bsonr
226229
case decimal.Decimal128:
227230
d128 = t
228231
case *decimal.Decimal128:
232+
if t == nil {
233+
return vw.WriteNull()
234+
}
229235
d128 = *t
230236
default:
231237
return ValueEncoderError{
@@ -245,6 +251,9 @@ func (dve DefaultValueEncoders) JSONNumberEncodeValue(ec EncodeContext, vw bsonr
245251
case json.Number:
246252
jsnum = t
247253
case *json.Number:
254+
if t == nil {
255+
return vw.WriteNull()
256+
}
248257
jsnum = *t
249258
default:
250259
return ValueEncoderError{
@@ -274,6 +283,9 @@ func (dve DefaultValueEncoders) URLEncodeValue(ec EncodeContext, vw bsonrw.Value
274283
case url.URL:
275284
u = &t
276285
case *url.URL:
286+
if t == nil {
287+
return vw.WriteNull()
288+
}
277289
u = t
278290
default:
279291
return ValueEncoderError{
@@ -293,6 +305,9 @@ func (dve DefaultValueEncoders) TimeEncodeValue(ec EncodeContext, vw bsonrw.Valu
293305
case time.Time:
294306
tt = t
295307
case *time.Time:
308+
if t == nil {
309+
return vw.WriteNull()
310+
}
296311
tt = *t
297312
default:
298313
return ValueEncoderError{
@@ -312,6 +327,9 @@ func (dve DefaultValueEncoders) ByteSliceEncodeValue(ec EncodeContext, vw bsonrw
312327
case []byte:
313328
slcb = t
314329
case *[]byte:
330+
if t == nil {
331+
return vw.WriteNull()
332+
}
315333
slcb = *t
316334
default:
317335
return ValueEncoderError{

bson/bsoncodec/default_value_encoders_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ func TestDefaultValueEncoders(t *testing.T) {
4545
*pjsnum = json.Number("3.14159")
4646
d128 := decimal.NewDecimal128(12345, 67890)
4747

48+
var ptimeNil *(time.Time)
49+
var pobjectidNil *(objectid.ObjectID)
50+
var pd128Nil *(decimal.Decimal128)
51+
var pjsnumNil *(json.Number)
52+
var purlNil *(url.URL)
53+
var pbytesliceNil *[]byte
54+
4855
type subtest struct {
4956
name string
5057
val interface{}
@@ -183,6 +190,7 @@ func TestDefaultValueEncoders(t *testing.T) {
183190
},
184191
{"time.Time", now, nil, nil, bsonrwtest.WriteDateTime, nil},
185192
{"*time.Time", &now, nil, nil, bsonrwtest.WriteDateTime, nil},
193+
{"*time.Time/nil", ptimeNil, nil, nil, bsonrwtest.WriteNull, nil},
186194
},
187195
},
188196
{
@@ -311,6 +319,7 @@ func TestDefaultValueEncoders(t *testing.T) {
311319
&objectid.ObjectID{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C},
312320
nil, nil, bsonrwtest.WriteObjectID, nil,
313321
},
322+
{"*objectid.ObjectID/nil/success", pobjectidNil, nil, nil, bsonrwtest.WriteNull, nil},
314323
},
315324
},
316325
{
@@ -331,6 +340,7 @@ func TestDefaultValueEncoders(t *testing.T) {
331340
},
332341
{"Decimal128/success", d128, nil, nil, bsonrwtest.WriteDecimal128, nil},
333342
{"*Decimal128/success", &d128, nil, nil, bsonrwtest.WriteDecimal128, nil},
343+
{"*Decimal128/nil/success", pd128Nil, nil, nil, bsonrwtest.WriteNull, nil},
334344
},
335345
},
336346
{
@@ -369,6 +379,7 @@ func TestDefaultValueEncoders(t *testing.T) {
369379
pjsnum,
370380
nil, nil, bsonrwtest.WriteDouble, nil,
371381
},
382+
{"*json.Number/nil/success", pjsnumNil, nil, nil, bsonrwtest.WriteNull, nil},
372383
},
373384
},
374385
{
@@ -389,6 +400,7 @@ func TestDefaultValueEncoders(t *testing.T) {
389400
},
390401
{"url.URL", url.URL{Scheme: "http", Host: "example.com"}, nil, nil, bsonrwtest.WriteString, nil},
391402
{"*url.URL", &url.URL{Scheme: "http", Host: "example.com"}, nil, nil, bsonrwtest.WriteString, nil},
403+
{"*url.URL/nil", purlNil, nil, nil, bsonrwtest.WriteNull, nil},
392404
},
393405
},
394406
{
@@ -409,6 +421,7 @@ func TestDefaultValueEncoders(t *testing.T) {
409421
},
410422
{"[]byte", []byte{0x01, 0x02, 0x03}, nil, nil, bsonrwtest.WriteBinary, nil},
411423
{"*[]byte", &([]byte{0x01, 0x02, 0x03}), nil, nil, bsonrwtest.WriteBinary, nil},
424+
{"*[]byte/nil", pbytesliceNil, nil, nil, bsonrwtest.WriteNull, nil},
412425
},
413426
},
414427
{

bson/primitive_codecs.go

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ func (PrimitiveCodecs) JavaScriptEncodeValue(ectx bsoncodec.EncodeContext, vw bs
7070
case JavaScriptCode:
7171
js = t
7272
case *JavaScriptCode:
73+
if t == nil {
74+
return vw.WriteNull()
75+
}
7376
js = *t
7477
default:
7578
return bsoncodec.ValueEncoderError{
@@ -89,6 +92,9 @@ func (PrimitiveCodecs) SymbolEncodeValue(ectx bsoncodec.EncodeContext, vw bsonrw
8992
case Symbol:
9093
symbol = t
9194
case *Symbol:
95+
if t == nil {
96+
return vw.WriteNull()
97+
}
9298
symbol = *t
9399
default:
94100
return bsoncodec.ValueEncoderError{
@@ -170,6 +176,9 @@ func (PrimitiveCodecs) BinaryEncodeValue(ec bsoncodec.EncodeContext, vw bsonrw.V
170176
case Binary:
171177
b = t
172178
case *Binary:
179+
if t == nil {
180+
return vw.WriteNull()
181+
}
173182
b = *t
174183
default:
175184
return bsoncodec.ValueEncoderError{
@@ -214,7 +223,11 @@ func (PrimitiveCodecs) BinaryDecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.V
214223
// UndefinedEncodeValue is the ValueEncoderFunc for Undefined.
215224
func (PrimitiveCodecs) UndefinedEncodeValue(ec bsoncodec.EncodeContext, vw bsonrw.ValueWriter, i interface{}) error {
216225
switch i.(type) {
217-
case Undefinedv2, *Undefinedv2:
226+
case Undefinedv2:
227+
case *Undefinedv2:
228+
if i.(*Undefinedv2) == nil {
229+
return vw.WriteNull()
230+
}
218231
default:
219232
return bsoncodec.ValueEncoderError{
220233
Name: "UndefinedEncodeValue",
@@ -248,6 +261,9 @@ func (PrimitiveCodecs) DateTimeEncodeValue(ec bsoncodec.EncodeContext, vw bsonrw
248261
case DateTime:
249262
dt = t
250263
case *DateTime:
264+
if t == nil {
265+
return vw.WriteNull()
266+
}
251267
dt = *t
252268
default:
253269
return bsoncodec.ValueEncoderError{
@@ -317,6 +333,9 @@ func (PrimitiveCodecs) RegexEncodeValue(ec bsoncodec.EncodeContext, vw bsonrw.Va
317333
case Regex:
318334
regex = t
319335
case *Regex:
336+
if t == nil {
337+
return vw.WriteNull()
338+
}
320339
regex = *t
321340
default:
322341
return bsoncodec.ValueEncoderError{
@@ -356,6 +375,9 @@ func (PrimitiveCodecs) DBPointerEncodeValue(ec bsoncodec.EncodeContext, vw bsonr
356375
case DBPointer:
357376
dbp = t
358377
case *DBPointer:
378+
if t == nil {
379+
return vw.WriteNull()
380+
}
359381
dbp = *t
360382
default:
361383
return bsoncodec.ValueEncoderError{
@@ -395,6 +417,10 @@ func (pc PrimitiveCodecs) DocumentEncodeValue(ec bsoncodec.EncodeContext, vw bso
395417
return bsoncodec.ValueEncoderError{Name: "DocumentEncodeValue", Types: []interface{}{(*Document)(nil), (**Document)(nil)}, Received: i}
396418
}
397419

420+
if doc == nil {
421+
return vw.WriteNull()
422+
}
423+
398424
dw, err := vw.WriteDocument()
399425
if err != nil {
400426
return err
@@ -410,6 +436,9 @@ func (pc PrimitiveCodecs) CodeWithScopeEncodeValue(ec bsoncodec.EncodeContext, v
410436
case CodeWithScope:
411437
cws = t
412438
case *CodeWithScope:
439+
if t == nil {
440+
return vw.WriteNull()
441+
}
413442
cws = *t
414443
default:
415444
return bsoncodec.ValueEncoderError{
@@ -464,6 +493,9 @@ func (PrimitiveCodecs) TimestampEncodeValue(ec bsoncodec.EncodeContext, vw bsonr
464493
case Timestamp:
465494
ts = t
466495
case *Timestamp:
496+
if t == nil {
497+
return vw.WriteNull()
498+
}
467499
ts = *t
468500
default:
469501
return bsoncodec.ValueEncoderError{
@@ -499,7 +531,11 @@ func (PrimitiveCodecs) TimestampDecodeValue(dc bsoncodec.DecodeContext, vr bsonr
499531
// MinKeyEncodeValue is the ValueEncoderFunc for MinKey.
500532
func (PrimitiveCodecs) MinKeyEncodeValue(ec bsoncodec.EncodeContext, vw bsonrw.ValueWriter, i interface{}) error {
501533
switch i.(type) {
502-
case MinKeyv2, *MinKeyv2:
534+
case MinKeyv2:
535+
case *MinKeyv2:
536+
if i.(*MinKeyv2) == nil {
537+
return vw.WriteNull()
538+
}
503539
default:
504540
return bsoncodec.ValueEncoderError{
505541
Name: "MinKeyEncodeValue",
@@ -529,7 +565,11 @@ func (PrimitiveCodecs) MinKeyDecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.V
529565
// MaxKeyEncodeValue is the ValueEncoderFunc for MaxKey.
530566
func (PrimitiveCodecs) MaxKeyEncodeValue(ec bsoncodec.EncodeContext, vw bsonrw.ValueWriter, i interface{}) error {
531567
switch i.(type) {
532-
case MaxKeyv2, *MaxKeyv2:
568+
case MaxKeyv2:
569+
case *MaxKeyv2:
570+
if i.(*MaxKeyv2) == nil {
571+
return vw.WriteNull()
572+
}
533573
default:
534574
return bsoncodec.ValueEncoderError{
535575
Name: "MaxKeyEncodeValue",
@@ -563,6 +603,9 @@ func (PrimitiveCodecs) RawValueEncodeValue(ec bsoncodec.EncodeContext, vw bsonrw
563603
case RawValue:
564604
rawvalue = t
565605
case *RawValue:
606+
if t == nil {
607+
return vw.WriteNull()
608+
}
566609
rawvalue = *t
567610
default:
568611
return bsoncodec.ValueEncoderError{
@@ -622,6 +665,9 @@ func (pc PrimitiveCodecs) ValueEncodeValue(ec bsoncodec.EncodeContext, vw bsonrw
622665
Received: i,
623666
}
624667
}
668+
if val == nil {
669+
return vw.WriteNull()
670+
}
625671

626672
if err := val.Validate(); err != nil {
627673
return err
@@ -687,6 +733,9 @@ func (pc PrimitiveCodecs) ElementSliceEncodeValue(ec bsoncodec.EncodeContext, vw
687733
case []*Element:
688734
slce = t
689735
case *[]*Element:
736+
if t == nil {
737+
return vw.WriteNull()
738+
}
690739
slce = *t
691740
default:
692741
return bsoncodec.ValueEncoderError{
@@ -758,6 +807,9 @@ func (pc PrimitiveCodecs) ArrayEncodeValue(ec bsoncodec.EncodeContext, vw bsonrw
758807
if !ok {
759808
return bsoncodec.ValueEncoderError{Name: "ArrayEncodeValue", Types: []interface{}{(*Array)(nil)}, Received: i}
760809
}
810+
if arr == nil {
811+
return vw.WriteNull()
812+
}
761813

762814
aw, err := vw.WriteArray()
763815
if err != nil {

0 commit comments

Comments
 (0)