-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathdefault_value_decoders.go
1819 lines (1597 loc) · 53.6 KB
/
default_value_decoders.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package bsoncodec
import (
"encoding/json"
"errors"
"fmt"
"math"
"net/url"
"reflect"
"strconv"
"time"
"go.mongodb.org/mongo-driver/bson/bsonrw"
"go.mongodb.org/mongo-driver/bson/bsontype"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
)
var (
defaultValueDecoders DefaultValueDecoders
errCannotTruncate = errors.New("float64 can only be truncated to a lower precision type when truncation is enabled")
)
type decodeBinaryError struct {
subtype byte
typeName string
}
func (d decodeBinaryError) Error() string {
return fmt.Sprintf("only binary values with subtype 0x00 or 0x02 can be decoded into %s, but got subtype %v", d.typeName, d.subtype)
}
func newDefaultStructCodec() *StructCodec {
codec, err := NewStructCodec(DefaultStructTagParser)
if err != nil {
// This function is called from the codec registration path, so errors can't be propagated. If there's an error
// constructing the StructCodec, we panic to avoid losing it.
panic(fmt.Errorf("error creating default StructCodec: %w", err))
}
return codec
}
// DefaultValueDecoders is a namespace type for the default ValueDecoders used
// when creating a registry.
//
// Deprecated: Use [go.mongodb.org/mongo-driver/bson.NewRegistry] to get a registry with all default
// value decoders registered.
type DefaultValueDecoders struct{}
// RegisterDefaultDecoders will register the decoder methods attached to DefaultValueDecoders with
// the provided RegistryBuilder.
//
// There is no support for decoding map[string]interface{} because there is no decoder for
// interface{}, so users must either register this decoder themselves or use the
// EmptyInterfaceDecoder available in the bson package.
//
// Deprecated: Use [go.mongodb.org/mongo-driver/bson.NewRegistry] to get a registry with all default
// value decoders registered.
func (dvd DefaultValueDecoders) RegisterDefaultDecoders(rb *RegistryBuilder) {
if rb == nil {
panic(errors.New("argument to RegisterDefaultDecoders must not be nil"))
}
intDecoder := decodeAdapter{dvd.IntDecodeValue, dvd.intDecodeType}
floatDecoder := decodeAdapter{dvd.FloatDecodeValue, dvd.floatDecodeType}
rb.
RegisterTypeDecoder(tD, ValueDecoderFunc(dvd.DDecodeValue)).
RegisterTypeDecoder(tBinary, decodeAdapter{dvd.BinaryDecodeValue, dvd.binaryDecodeType}).
RegisterTypeDecoder(tUndefined, decodeAdapter{dvd.UndefinedDecodeValue, dvd.undefinedDecodeType}).
RegisterTypeDecoder(tDateTime, decodeAdapter{dvd.DateTimeDecodeValue, dvd.dateTimeDecodeType}).
RegisterTypeDecoder(tNull, decodeAdapter{dvd.NullDecodeValue, dvd.nullDecodeType}).
RegisterTypeDecoder(tRegex, decodeAdapter{dvd.RegexDecodeValue, dvd.regexDecodeType}).
RegisterTypeDecoder(tDBPointer, decodeAdapter{dvd.DBPointerDecodeValue, dvd.dBPointerDecodeType}).
RegisterTypeDecoder(tTimestamp, decodeAdapter{dvd.TimestampDecodeValue, dvd.timestampDecodeType}).
RegisterTypeDecoder(tMinKey, decodeAdapter{dvd.MinKeyDecodeValue, dvd.minKeyDecodeType}).
RegisterTypeDecoder(tMaxKey, decodeAdapter{dvd.MaxKeyDecodeValue, dvd.maxKeyDecodeType}).
RegisterTypeDecoder(tJavaScript, decodeAdapter{dvd.JavaScriptDecodeValue, dvd.javaScriptDecodeType}).
RegisterTypeDecoder(tSymbol, decodeAdapter{dvd.SymbolDecodeValue, dvd.symbolDecodeType}).
RegisterTypeDecoder(tByteSlice, defaultByteSliceCodec).
RegisterTypeDecoder(tTime, defaultTimeCodec).
RegisterTypeDecoder(tEmpty, defaultEmptyInterfaceCodec).
RegisterTypeDecoder(tCoreArray, defaultArrayCodec).
RegisterTypeDecoder(tOID, decodeAdapter{dvd.ObjectIDDecodeValue, dvd.objectIDDecodeType}).
RegisterTypeDecoder(tDecimal, decodeAdapter{dvd.Decimal128DecodeValue, dvd.decimal128DecodeType}).
RegisterTypeDecoder(tJSONNumber, decodeAdapter{dvd.JSONNumberDecodeValue, dvd.jsonNumberDecodeType}).
RegisterTypeDecoder(tURL, decodeAdapter{dvd.URLDecodeValue, dvd.urlDecodeType}).
RegisterTypeDecoder(tCoreDocument, ValueDecoderFunc(dvd.CoreDocumentDecodeValue)).
RegisterTypeDecoder(tCodeWithScope, decodeAdapter{dvd.CodeWithScopeDecodeValue, dvd.codeWithScopeDecodeType}).
RegisterDefaultDecoder(reflect.Bool, decodeAdapter{dvd.BooleanDecodeValue, dvd.booleanDecodeType}).
RegisterDefaultDecoder(reflect.Int, intDecoder).
RegisterDefaultDecoder(reflect.Int8, intDecoder).
RegisterDefaultDecoder(reflect.Int16, intDecoder).
RegisterDefaultDecoder(reflect.Int32, intDecoder).
RegisterDefaultDecoder(reflect.Int64, intDecoder).
RegisterDefaultDecoder(reflect.Uint, defaultUIntCodec).
RegisterDefaultDecoder(reflect.Uint8, defaultUIntCodec).
RegisterDefaultDecoder(reflect.Uint16, defaultUIntCodec).
RegisterDefaultDecoder(reflect.Uint32, defaultUIntCodec).
RegisterDefaultDecoder(reflect.Uint64, defaultUIntCodec).
RegisterDefaultDecoder(reflect.Float32, floatDecoder).
RegisterDefaultDecoder(reflect.Float64, floatDecoder).
RegisterDefaultDecoder(reflect.Array, ValueDecoderFunc(dvd.ArrayDecodeValue)).
RegisterDefaultDecoder(reflect.Map, defaultMapCodec).
RegisterDefaultDecoder(reflect.Slice, defaultSliceCodec).
RegisterDefaultDecoder(reflect.String, defaultStringCodec).
RegisterDefaultDecoder(reflect.Struct, newDefaultStructCodec()).
RegisterDefaultDecoder(reflect.Ptr, NewPointerCodec()).
RegisterTypeMapEntry(bsontype.Double, tFloat64).
RegisterTypeMapEntry(bsontype.String, tString).
RegisterTypeMapEntry(bsontype.Array, tA).
RegisterTypeMapEntry(bsontype.Binary, tBinary).
RegisterTypeMapEntry(bsontype.Undefined, tUndefined).
RegisterTypeMapEntry(bsontype.ObjectID, tOID).
RegisterTypeMapEntry(bsontype.Boolean, tBool).
RegisterTypeMapEntry(bsontype.DateTime, tDateTime).
RegisterTypeMapEntry(bsontype.Regex, tRegex).
RegisterTypeMapEntry(bsontype.DBPointer, tDBPointer).
RegisterTypeMapEntry(bsontype.JavaScript, tJavaScript).
RegisterTypeMapEntry(bsontype.Symbol, tSymbol).
RegisterTypeMapEntry(bsontype.CodeWithScope, tCodeWithScope).
RegisterTypeMapEntry(bsontype.Int32, tInt32).
RegisterTypeMapEntry(bsontype.Int64, tInt64).
RegisterTypeMapEntry(bsontype.Timestamp, tTimestamp).
RegisterTypeMapEntry(bsontype.Decimal128, tDecimal).
RegisterTypeMapEntry(bsontype.MinKey, tMinKey).
RegisterTypeMapEntry(bsontype.MaxKey, tMaxKey).
RegisterTypeMapEntry(bsontype.Type(0), tD).
RegisterTypeMapEntry(bsontype.EmbeddedDocument, tD).
RegisterHookDecoder(tValueUnmarshaler, ValueDecoderFunc(dvd.ValueUnmarshalerDecodeValue)).
RegisterHookDecoder(tUnmarshaler, ValueDecoderFunc(dvd.UnmarshalerDecodeValue))
}
// DDecodeValue is the ValueDecoderFunc for primitive.D instances.
//
// Deprecated: Use [go.mongodb.org/mongo-driver/bson.NewRegistry] to get a registry with all default
// value decoders registered.
func (dvd DefaultValueDecoders) DDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
if !val.IsValid() || !val.CanSet() || val.Type() != tD {
return ValueDecoderError{Name: "DDecodeValue", Kinds: []reflect.Kind{reflect.Slice}, Received: val}
}
switch vrType := vr.Type(); vrType {
case bsontype.Type(0), bsontype.EmbeddedDocument:
dc.Ancestor = tD
case bsontype.Null:
val.Set(reflect.Zero(val.Type()))
return vr.ReadNull()
default:
return fmt.Errorf("cannot decode %v into a primitive.D", vrType)
}
dr, err := vr.ReadDocument()
if err != nil {
return err
}
decoder, err := dc.LookupDecoder(tEmpty)
if err != nil {
return err
}
tEmptyTypeDecoder, _ := decoder.(typeDecoder)
// Use the elements in the provided value if it's non nil. Otherwise, allocate a new D instance.
var elems primitive.D
if !val.IsNil() {
val.SetLen(0)
elems = val.Interface().(primitive.D)
} else {
elems = make(primitive.D, 0)
}
for {
key, elemVr, err := dr.ReadElement()
if errors.Is(err, bsonrw.ErrEOD) {
break
} else if err != nil {
return err
}
// Pass false for convert because we don't need to call reflect.Value.Convert for tEmpty.
elem, err := decodeTypeOrValueWithInfo(decoder, tEmptyTypeDecoder, dc, elemVr, tEmpty, false)
if err != nil {
return err
}
elems = append(elems, primitive.E{Key: key, Value: elem.Interface()})
}
val.Set(reflect.ValueOf(elems))
return nil
}
func (dvd DefaultValueDecoders) booleanDecodeType(_ DecodeContext, vr bsonrw.ValueReader, t reflect.Type) (reflect.Value, error) {
if t.Kind() != reflect.Bool {
return emptyValue, ValueDecoderError{
Name: "BooleanDecodeValue",
Kinds: []reflect.Kind{reflect.Bool},
Received: reflect.Zero(t),
}
}
var b bool
var err error
switch vrType := vr.Type(); vrType {
case bsontype.Int32:
i32, err := vr.ReadInt32()
if err != nil {
return emptyValue, err
}
b = (i32 != 0)
case bsontype.Int64:
i64, err := vr.ReadInt64()
if err != nil {
return emptyValue, err
}
b = (i64 != 0)
case bsontype.Double:
f64, err := vr.ReadDouble()
if err != nil {
return emptyValue, err
}
b = (f64 != 0)
case bsontype.Boolean:
b, err = vr.ReadBoolean()
case bsontype.Null:
err = vr.ReadNull()
case bsontype.Undefined:
err = vr.ReadUndefined()
default:
return emptyValue, fmt.Errorf("cannot decode %v into a boolean", vrType)
}
if err != nil {
return emptyValue, err
}
return reflect.ValueOf(b), nil
}
// BooleanDecodeValue is the ValueDecoderFunc for bool types.
//
// Deprecated: Use [go.mongodb.org/mongo-driver/bson.NewRegistry] to get a registry with all default
// value decoders registered.
func (dvd DefaultValueDecoders) BooleanDecodeValue(dctx DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
if !val.IsValid() || !val.CanSet() || val.Kind() != reflect.Bool {
return ValueDecoderError{Name: "BooleanDecodeValue", Kinds: []reflect.Kind{reflect.Bool}, Received: val}
}
elem, err := dvd.booleanDecodeType(dctx, vr, val.Type())
if err != nil {
return err
}
val.SetBool(elem.Bool())
return nil
}
func (DefaultValueDecoders) intDecodeType(dc DecodeContext, vr bsonrw.ValueReader, t reflect.Type) (reflect.Value, error) {
var i64 int64
var err error
switch vrType := vr.Type(); vrType {
case bsontype.Int32:
i32, err := vr.ReadInt32()
if err != nil {
return emptyValue, err
}
i64 = int64(i32)
case bsontype.Int64:
i64, err = vr.ReadInt64()
if err != nil {
return emptyValue, err
}
case bsontype.Double:
f64, err := vr.ReadDouble()
if err != nil {
return emptyValue, err
}
if !dc.Truncate && math.Floor(f64) != f64 {
return emptyValue, errCannotTruncate
}
if f64 > float64(math.MaxInt64) {
return emptyValue, fmt.Errorf("%g overflows int64", f64)
}
i64 = int64(f64)
case bsontype.Boolean:
b, err := vr.ReadBoolean()
if err != nil {
return emptyValue, err
}
if b {
i64 = 1
}
case bsontype.Null:
if err = vr.ReadNull(); err != nil {
return emptyValue, err
}
case bsontype.Undefined:
if err = vr.ReadUndefined(); err != nil {
return emptyValue, err
}
default:
return emptyValue, fmt.Errorf("cannot decode %v into an integer type", vrType)
}
switch t.Kind() {
case reflect.Int8:
if i64 < math.MinInt8 || i64 > math.MaxInt8 {
return emptyValue, fmt.Errorf("%d overflows int8", i64)
}
return reflect.ValueOf(int8(i64)), nil
case reflect.Int16:
if i64 < math.MinInt16 || i64 > math.MaxInt16 {
return emptyValue, fmt.Errorf("%d overflows int16", i64)
}
return reflect.ValueOf(int16(i64)), nil
case reflect.Int32:
if i64 < math.MinInt32 || i64 > math.MaxInt32 {
return emptyValue, fmt.Errorf("%d overflows int32", i64)
}
return reflect.ValueOf(int32(i64)), nil
case reflect.Int64:
return reflect.ValueOf(i64), nil
case reflect.Int:
if i64 > math.MaxInt { // Can we fit this inside of an int
return emptyValue, fmt.Errorf("%d overflows int", i64)
}
return reflect.ValueOf(int(i64)), nil
default:
return emptyValue, ValueDecoderError{
Name: "IntDecodeValue",
Kinds: []reflect.Kind{reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int},
Received: reflect.Zero(t),
}
}
}
// IntDecodeValue is the ValueDecoderFunc for int types.
//
// Deprecated: Use [go.mongodb.org/mongo-driver/bson.NewRegistry] to get a registry with all default
// value decoders registered.
func (dvd DefaultValueDecoders) IntDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
if !val.CanSet() {
return ValueDecoderError{
Name: "IntDecodeValue",
Kinds: []reflect.Kind{reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int},
Received: val,
}
}
elem, err := dvd.intDecodeType(dc, vr, val.Type())
if err != nil {
return err
}
val.SetInt(elem.Int())
return nil
}
// UintDecodeValue is the ValueDecoderFunc for uint types.
//
// Deprecated: UintDecodeValue is not registered by default. Use UintCodec.DecodeValue instead.
func (dvd DefaultValueDecoders) UintDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
var i64 int64
var err error
switch vr.Type() {
case bsontype.Int32:
i32, err := vr.ReadInt32()
if err != nil {
return err
}
i64 = int64(i32)
case bsontype.Int64:
i64, err = vr.ReadInt64()
if err != nil {
return err
}
case bsontype.Double:
f64, err := vr.ReadDouble()
if err != nil {
return err
}
if !dc.Truncate && math.Floor(f64) != f64 {
return errors.New("UintDecodeValue can only truncate float64 to an integer type when truncation is enabled")
}
if f64 > float64(math.MaxInt64) {
return fmt.Errorf("%g overflows int64", f64)
}
i64 = int64(f64)
case bsontype.Boolean:
b, err := vr.ReadBoolean()
if err != nil {
return err
}
if b {
i64 = 1
}
default:
return fmt.Errorf("cannot decode %v into an integer type", vr.Type())
}
if !val.CanSet() {
return ValueDecoderError{
Name: "UintDecodeValue",
Kinds: []reflect.Kind{reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint},
Received: val,
}
}
switch val.Kind() {
case reflect.Uint8:
if i64 < 0 || i64 > math.MaxUint8 {
return fmt.Errorf("%d overflows uint8", i64)
}
case reflect.Uint16:
if i64 < 0 || i64 > math.MaxUint16 {
return fmt.Errorf("%d overflows uint16", i64)
}
case reflect.Uint32:
if i64 < 0 || i64 > math.MaxUint32 {
return fmt.Errorf("%d overflows uint32", i64)
}
case reflect.Uint64:
if i64 < 0 {
return fmt.Errorf("%d overflows uint64", i64)
}
case reflect.Uint:
if i64 < 0 || uint64(i64) > uint64(math.MaxUint) { // Can we fit this inside of an uint
return fmt.Errorf("%d overflows uint", i64)
}
default:
return ValueDecoderError{
Name: "UintDecodeValue",
Kinds: []reflect.Kind{reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint},
Received: val,
}
}
val.SetUint(uint64(i64))
return nil
}
func (dvd DefaultValueDecoders) floatDecodeType(dc DecodeContext, vr bsonrw.ValueReader, t reflect.Type) (reflect.Value, error) {
var f float64
var err error
switch vrType := vr.Type(); vrType {
case bsontype.Int32:
i32, err := vr.ReadInt32()
if err != nil {
return emptyValue, err
}
f = float64(i32)
case bsontype.Int64:
i64, err := vr.ReadInt64()
if err != nil {
return emptyValue, err
}
f = float64(i64)
case bsontype.Double:
f, err = vr.ReadDouble()
if err != nil {
return emptyValue, err
}
case bsontype.Boolean:
b, err := vr.ReadBoolean()
if err != nil {
return emptyValue, err
}
if b {
f = 1
}
case bsontype.Null:
if err = vr.ReadNull(); err != nil {
return emptyValue, err
}
case bsontype.Undefined:
if err = vr.ReadUndefined(); err != nil {
return emptyValue, err
}
default:
return emptyValue, fmt.Errorf("cannot decode %v into a float32 or float64 type", vrType)
}
switch t.Kind() {
case reflect.Float32:
if !dc.Truncate && float64(float32(f)) != f {
return emptyValue, errCannotTruncate
}
return reflect.ValueOf(float32(f)), nil
case reflect.Float64:
return reflect.ValueOf(f), nil
default:
return emptyValue, ValueDecoderError{
Name: "FloatDecodeValue",
Kinds: []reflect.Kind{reflect.Float32, reflect.Float64},
Received: reflect.Zero(t),
}
}
}
// FloatDecodeValue is the ValueDecoderFunc for float types.
//
// Deprecated: Use [go.mongodb.org/mongo-driver/bson.NewRegistry] to get a registry with all default
// value decoders registered.
func (dvd DefaultValueDecoders) FloatDecodeValue(ec DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
if !val.CanSet() {
return ValueDecoderError{
Name: "FloatDecodeValue",
Kinds: []reflect.Kind{reflect.Float32, reflect.Float64},
Received: val,
}
}
elem, err := dvd.floatDecodeType(ec, vr, val.Type())
if err != nil {
return err
}
val.SetFloat(elem.Float())
return nil
}
// StringDecodeValue is the ValueDecoderFunc for string types.
//
// Deprecated: StringDecodeValue is not registered by default. Use StringCodec.DecodeValue instead.
func (dvd DefaultValueDecoders) StringDecodeValue(_ DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
var str string
var err error
switch vr.Type() {
// TODO(GODRIVER-577): Handle JavaScript and Symbol BSON types when allowed.
case bsontype.String:
str, err = vr.ReadString()
if err != nil {
return err
}
default:
return fmt.Errorf("cannot decode %v into a string type", vr.Type())
}
if !val.CanSet() || val.Kind() != reflect.String {
return ValueDecoderError{Name: "StringDecodeValue", Kinds: []reflect.Kind{reflect.String}, Received: val}
}
val.SetString(str)
return nil
}
func (DefaultValueDecoders) javaScriptDecodeType(_ DecodeContext, vr bsonrw.ValueReader, t reflect.Type) (reflect.Value, error) {
if t != tJavaScript {
return emptyValue, ValueDecoderError{
Name: "JavaScriptDecodeValue",
Types: []reflect.Type{tJavaScript},
Received: reflect.Zero(t),
}
}
var js string
var err error
switch vrType := vr.Type(); vrType {
case bsontype.JavaScript:
js, err = vr.ReadJavascript()
case bsontype.Null:
err = vr.ReadNull()
case bsontype.Undefined:
err = vr.ReadUndefined()
default:
return emptyValue, fmt.Errorf("cannot decode %v into a primitive.JavaScript", vrType)
}
if err != nil {
return emptyValue, err
}
return reflect.ValueOf(primitive.JavaScript(js)), nil
}
// JavaScriptDecodeValue is the ValueDecoderFunc for the primitive.JavaScript type.
//
// Deprecated: Use [go.mongodb.org/mongo-driver/bson.NewRegistry] to get a registry with all default
// value decoders registered.
func (dvd DefaultValueDecoders) JavaScriptDecodeValue(dctx DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
if !val.CanSet() || val.Type() != tJavaScript {
return ValueDecoderError{Name: "JavaScriptDecodeValue", Types: []reflect.Type{tJavaScript}, Received: val}
}
elem, err := dvd.javaScriptDecodeType(dctx, vr, tJavaScript)
if err != nil {
return err
}
val.SetString(elem.String())
return nil
}
func (DefaultValueDecoders) symbolDecodeType(_ DecodeContext, vr bsonrw.ValueReader, t reflect.Type) (reflect.Value, error) {
if t != tSymbol {
return emptyValue, ValueDecoderError{
Name: "SymbolDecodeValue",
Types: []reflect.Type{tSymbol},
Received: reflect.Zero(t),
}
}
var symbol string
var err error
switch vrType := vr.Type(); vrType {
case bsontype.String:
symbol, err = vr.ReadString()
case bsontype.Symbol:
symbol, err = vr.ReadSymbol()
case bsontype.Binary:
data, subtype, err := vr.ReadBinary()
if err != nil {
return emptyValue, err
}
if subtype != bsontype.BinaryGeneric && subtype != bsontype.BinaryBinaryOld {
return emptyValue, decodeBinaryError{subtype: subtype, typeName: "primitive.Symbol"}
}
symbol = string(data)
case bsontype.Null:
err = vr.ReadNull()
case bsontype.Undefined:
err = vr.ReadUndefined()
default:
return emptyValue, fmt.Errorf("cannot decode %v into a primitive.Symbol", vrType)
}
if err != nil {
return emptyValue, err
}
return reflect.ValueOf(primitive.Symbol(symbol)), nil
}
// SymbolDecodeValue is the ValueDecoderFunc for the primitive.Symbol type.
//
// Deprecated: Use [go.mongodb.org/mongo-driver/bson.NewRegistry] to get a registry with all default
// value decoders registered.
func (dvd DefaultValueDecoders) SymbolDecodeValue(dctx DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
if !val.CanSet() || val.Type() != tSymbol {
return ValueDecoderError{Name: "SymbolDecodeValue", Types: []reflect.Type{tSymbol}, Received: val}
}
elem, err := dvd.symbolDecodeType(dctx, vr, tSymbol)
if err != nil {
return err
}
val.SetString(elem.String())
return nil
}
func (DefaultValueDecoders) binaryDecodeType(_ DecodeContext, vr bsonrw.ValueReader, t reflect.Type) (reflect.Value, error) {
if t != tBinary {
return emptyValue, ValueDecoderError{
Name: "BinaryDecodeValue",
Types: []reflect.Type{tBinary},
Received: reflect.Zero(t),
}
}
var data []byte
var subtype byte
var err error
switch vrType := vr.Type(); vrType {
case bsontype.Binary:
data, subtype, err = vr.ReadBinary()
case bsontype.Null:
err = vr.ReadNull()
case bsontype.Undefined:
err = vr.ReadUndefined()
default:
return emptyValue, fmt.Errorf("cannot decode %v into a Binary", vrType)
}
if err != nil {
return emptyValue, err
}
return reflect.ValueOf(primitive.Binary{Subtype: subtype, Data: data}), nil
}
// BinaryDecodeValue is the ValueDecoderFunc for Binary.
//
// Deprecated: Use [go.mongodb.org/mongo-driver/bson.NewRegistry] to get a registry with all default
// value decoders registered.
func (dvd DefaultValueDecoders) BinaryDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
if !val.CanSet() || val.Type() != tBinary {
return ValueDecoderError{Name: "BinaryDecodeValue", Types: []reflect.Type{tBinary}, Received: val}
}
elem, err := dvd.binaryDecodeType(dc, vr, tBinary)
if err != nil {
return err
}
val.Set(elem)
return nil
}
func (DefaultValueDecoders) undefinedDecodeType(_ DecodeContext, vr bsonrw.ValueReader, t reflect.Type) (reflect.Value, error) {
if t != tUndefined {
return emptyValue, ValueDecoderError{
Name: "UndefinedDecodeValue",
Types: []reflect.Type{tUndefined},
Received: reflect.Zero(t),
}
}
var err error
switch vrType := vr.Type(); vrType {
case bsontype.Undefined:
err = vr.ReadUndefined()
case bsontype.Null:
err = vr.ReadNull()
default:
return emptyValue, fmt.Errorf("cannot decode %v into an Undefined", vr.Type())
}
if err != nil {
return emptyValue, err
}
return reflect.ValueOf(primitive.Undefined{}), nil
}
// UndefinedDecodeValue is the ValueDecoderFunc for Undefined.
//
// Deprecated: Use [go.mongodb.org/mongo-driver/bson.NewRegistry] to get a registry with all default
// value decoders registered.
func (dvd DefaultValueDecoders) UndefinedDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
if !val.CanSet() || val.Type() != tUndefined {
return ValueDecoderError{Name: "UndefinedDecodeValue", Types: []reflect.Type{tUndefined}, Received: val}
}
elem, err := dvd.undefinedDecodeType(dc, vr, tUndefined)
if err != nil {
return err
}
val.Set(elem)
return nil
}
// Accept both 12-byte string and pretty-printed 24-byte hex string formats.
func (dvd DefaultValueDecoders) objectIDDecodeType(_ DecodeContext, vr bsonrw.ValueReader, t reflect.Type) (reflect.Value, error) {
if t != tOID {
return emptyValue, ValueDecoderError{
Name: "ObjectIDDecodeValue",
Types: []reflect.Type{tOID},
Received: reflect.Zero(t),
}
}
var oid primitive.ObjectID
var err error
switch vrType := vr.Type(); vrType {
case bsontype.ObjectID:
oid, err = vr.ReadObjectID()
if err != nil {
return emptyValue, err
}
case bsontype.String:
str, err := vr.ReadString()
if err != nil {
return emptyValue, err
}
if oid, err = primitive.ObjectIDFromHex(str); err == nil {
break
}
if len(str) != 12 {
return emptyValue, fmt.Errorf("an ObjectID string must be exactly 12 bytes long (got %v)", len(str))
}
byteArr := []byte(str)
copy(oid[:], byteArr)
case bsontype.Null:
if err = vr.ReadNull(); err != nil {
return emptyValue, err
}
case bsontype.Undefined:
if err = vr.ReadUndefined(); err != nil {
return emptyValue, err
}
default:
return emptyValue, fmt.Errorf("cannot decode %v into an ObjectID", vrType)
}
return reflect.ValueOf(oid), nil
}
// ObjectIDDecodeValue is the ValueDecoderFunc for primitive.ObjectID.
//
// Deprecated: Use [go.mongodb.org/mongo-driver/bson.NewRegistry] to get a registry with all default
// value decoders registered.
func (dvd DefaultValueDecoders) ObjectIDDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
if !val.CanSet() || val.Type() != tOID {
return ValueDecoderError{Name: "ObjectIDDecodeValue", Types: []reflect.Type{tOID}, Received: val}
}
elem, err := dvd.objectIDDecodeType(dc, vr, tOID)
if err != nil {
return err
}
val.Set(elem)
return nil
}
func (DefaultValueDecoders) dateTimeDecodeType(_ DecodeContext, vr bsonrw.ValueReader, t reflect.Type) (reflect.Value, error) {
if t != tDateTime {
return emptyValue, ValueDecoderError{
Name: "DateTimeDecodeValue",
Types: []reflect.Type{tDateTime},
Received: reflect.Zero(t),
}
}
var dt int64
var err error
switch vrType := vr.Type(); vrType {
case bsontype.DateTime:
dt, err = vr.ReadDateTime()
case bsontype.Null:
err = vr.ReadNull()
case bsontype.Undefined:
err = vr.ReadUndefined()
default:
return emptyValue, fmt.Errorf("cannot decode %v into a DateTime", vrType)
}
if err != nil {
return emptyValue, err
}
return reflect.ValueOf(primitive.DateTime(dt)), nil
}
// DateTimeDecodeValue is the ValueDecoderFunc for DateTime.
//
// Deprecated: Use [go.mongodb.org/mongo-driver/bson.NewRegistry] to get a registry with all default
// value decoders registered.
func (dvd DefaultValueDecoders) DateTimeDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
if !val.CanSet() || val.Type() != tDateTime {
return ValueDecoderError{Name: "DateTimeDecodeValue", Types: []reflect.Type{tDateTime}, Received: val}
}
elem, err := dvd.dateTimeDecodeType(dc, vr, tDateTime)
if err != nil {
return err
}
val.Set(elem)
return nil
}
func (DefaultValueDecoders) nullDecodeType(_ DecodeContext, vr bsonrw.ValueReader, t reflect.Type) (reflect.Value, error) {
if t != tNull {
return emptyValue, ValueDecoderError{
Name: "NullDecodeValue",
Types: []reflect.Type{tNull},
Received: reflect.Zero(t),
}
}
var err error
switch vrType := vr.Type(); vrType {
case bsontype.Undefined:
err = vr.ReadUndefined()
case bsontype.Null:
err = vr.ReadNull()
default:
return emptyValue, fmt.Errorf("cannot decode %v into a Null", vr.Type())
}
if err != nil {
return emptyValue, err
}
return reflect.ValueOf(primitive.Null{}), nil
}
// NullDecodeValue is the ValueDecoderFunc for Null.
//
// Deprecated: Use [go.mongodb.org/mongo-driver/bson.NewRegistry] to get a registry with all default
// value decoders registered.
func (dvd DefaultValueDecoders) NullDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
if !val.CanSet() || val.Type() != tNull {
return ValueDecoderError{Name: "NullDecodeValue", Types: []reflect.Type{tNull}, Received: val}
}
elem, err := dvd.nullDecodeType(dc, vr, tNull)
if err != nil {
return err
}
val.Set(elem)
return nil
}
func (DefaultValueDecoders) regexDecodeType(_ DecodeContext, vr bsonrw.ValueReader, t reflect.Type) (reflect.Value, error) {
if t != tRegex {
return emptyValue, ValueDecoderError{
Name: "RegexDecodeValue",
Types: []reflect.Type{tRegex},
Received: reflect.Zero(t),
}
}
var pattern, options string
var err error
switch vrType := vr.Type(); vrType {
case bsontype.Regex:
pattern, options, err = vr.ReadRegex()
case bsontype.Null:
err = vr.ReadNull()
case bsontype.Undefined:
err = vr.ReadUndefined()
default:
return emptyValue, fmt.Errorf("cannot decode %v into a Regex", vrType)
}
if err != nil {
return emptyValue, err
}
return reflect.ValueOf(primitive.Regex{Pattern: pattern, Options: options}), nil
}
// RegexDecodeValue is the ValueDecoderFunc for Regex.
//
// Deprecated: Use [go.mongodb.org/mongo-driver/bson.NewRegistry] to get a registry with all default
// value decoders registered.
func (dvd DefaultValueDecoders) RegexDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
if !val.CanSet() || val.Type() != tRegex {
return ValueDecoderError{Name: "RegexDecodeValue", Types: []reflect.Type{tRegex}, Received: val}
}
elem, err := dvd.regexDecodeType(dc, vr, tRegex)
if err != nil {
return err
}
val.Set(elem)
return nil
}
func (DefaultValueDecoders) dBPointerDecodeType(_ DecodeContext, vr bsonrw.ValueReader, t reflect.Type) (reflect.Value, error) {
if t != tDBPointer {
return emptyValue, ValueDecoderError{
Name: "DBPointerDecodeValue",
Types: []reflect.Type{tDBPointer},
Received: reflect.Zero(t),
}
}
var ns string
var pointer primitive.ObjectID
var err error
switch vrType := vr.Type(); vrType {
case bsontype.DBPointer:
ns, pointer, err = vr.ReadDBPointer()
case bsontype.Null:
err = vr.ReadNull()
case bsontype.Undefined:
err = vr.ReadUndefined()
default:
return emptyValue, fmt.Errorf("cannot decode %v into a DBPointer", vrType)
}
if err != nil {
return emptyValue, err
}
return reflect.ValueOf(primitive.DBPointer{DB: ns, Pointer: pointer}), nil
}
// DBPointerDecodeValue is the ValueDecoderFunc for DBPointer.
//
// Deprecated: Use [go.mongodb.org/mongo-driver/bson.NewRegistry] to get a registry with all default
// value decoders registered.
func (dvd DefaultValueDecoders) DBPointerDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
if !val.CanSet() || val.Type() != tDBPointer {
return ValueDecoderError{Name: "DBPointerDecodeValue", Types: []reflect.Type{tDBPointer}, Received: val}
}
elem, err := dvd.dBPointerDecodeType(dc, vr, tDBPointer)
if err != nil {
return err
}
val.Set(elem)
return nil
}
func (DefaultValueDecoders) timestampDecodeType(_ DecodeContext, vr bsonrw.ValueReader, reflectType reflect.Type) (reflect.Value, error) {
if reflectType != tTimestamp {
return emptyValue, ValueDecoderError{
Name: "TimestampDecodeValue",