-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstructor.go
723 lines (593 loc) · 19.4 KB
/
constructor.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
// 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 birch
import (
"fmt"
"math"
"time"
"github.com/tychoish/birch/bsontype"
"github.com/tychoish/birch/elements"
"github.com/tychoish/birch/jsonx"
"github.com/tychoish/birch/types"
)
// EC is a convenience variable provided for access to the ElementConstructor methods.
var EC ElementConstructor
var ECE ElementConstructorError
// VC is a convenience variable provided for access to the ValueConstructor methods.
var VC ValueConstructor
var VCE ValueConstructorError
// ElementConstructor is used as a namespace for document element constructor functions.
type ElementConstructor struct{}
type ElementConstructorError struct{}
// ValueConstructor is used as a namespace for value constructor functions.
type ValueConstructor struct{}
type ValueConstructorError struct{}
// Interface will attempt to turn the provided key and value into an Element.
// For common types, type casting is used, for all slices and all
// other complex types, this relies on the Marshaler interface.
//
// If the value cannot be converted to bson, a null Element is constructed with the
// key. This method will never return a nil *Element. If an error turning the
// value into an Element is desired, use the InterfaceErr method.
func (ElementConstructor) Interface(key string, value any) *Element {
var (
elem *Element
err error
)
switch t := value.(type) {
case bool:
elem = EC.Boolean(key, t)
case int8:
elem = EC.Int32(key, int32(t))
case int16:
elem = EC.Int32(key, int32(t))
case int32:
elem = EC.Int32(key, t)
case int64:
elem = EC.Int64(key, t)
case int:
elem = EC.Int(key, t)
case uint8:
elem = EC.Int32(key, int32(t))
case uint16:
elem = EC.Int32(key, int32(t))
case uint:
switch {
case t < math.MaxInt32:
elem = EC.Int32(key, int32(t))
case uint64(t) > math.MaxInt64:
elem = EC.Null(key)
default:
elem = EC.Int64(key, int64(t))
}
case uint32:
if t < math.MaxInt32 {
elem = EC.Int32(key, int32(t))
} else {
elem = EC.Int64(key, int64(t))
}
case uint64:
switch {
case t < math.MaxInt32:
elem = EC.Int32(key, int32(t))
case t > math.MaxInt64:
elem = EC.Null(key)
default:
elem = EC.Int64(key, int64(t))
}
case float32:
elem = EC.Double(key, float64(t))
case float64:
elem = EC.Double(key, t)
case string:
elem = EC.String(key, t)
case time.Time:
elem = EC.Time(key, t)
case types.Timestamp:
elem = EC.Timestamp(key, t.T, t.I)
case map[string]any:
elem = EC.SubDocument(key, DC.MapInterface(t))
case map[any]any:
elem = EC.SubDocument(key, DC.Interface(t))
case map[string]string:
elem = EC.SubDocument(key, DC.MapString(t))
case []any:
elem = EC.SliceInterface(key, t)
case []string:
elem = EC.SliceString(key, t)
case []int64:
elem = EC.SliceInt64(key, t)
case []int32:
elem = EC.SliceInt32(key, t)
case []float64:
elem = EC.SliceFloat64(key, t)
case []float32:
elem = EC.SliceFloat32(key, t)
case []int:
elem = EC.SliceInt(key, t)
case []time.Time:
elem = EC.SliceTime(key, t)
case []time.Duration:
elem = EC.SliceDuration(key, t)
case []*Element:
elem = EC.SubDocumentFromElements(key, t...)
case *Element:
elem = t
case *Value:
elem = EC.Value(key, t)
case []*Value:
elem = EC.Array(key, MakeArray(len(t)).Append(t...))
case *jsonx.Document:
elem = EC.SubDocument(key, DC.JSONX(t))
case *Document:
if t != nil {
elem = EC.SubDocument(key, t)
}
case Reader:
var doc *Document
doc, err = DCE.Reader(t)
if err == nil {
elem = EC.SubDocument(key, doc)
}
case DocumentMarshaler:
elem, err = ECE.DocumentMarshaler(key, t)
case Marshaler:
elem, err = ECE.Marshaler(key, t)
default:
elem = EC.Null(key)
}
if err != nil || elem == nil {
elem = EC.Null(key)
}
return elem
}
// InterfaceErr does what Interface does, but returns an error when it cannot
// properly convert a value into an *Element. See Interface for details.
func (ElementConstructorError) Interface(key string, value any) (*Element, error) {
switch t := value.(type) {
case uint:
switch {
case t < math.MaxInt32:
return EC.Int32(key, int32(t)), nil
case uint64(t) > math.MaxInt64:
return nil, fmt.Errorf("BSON only has signed integer types and %d overflows an int64", t)
default:
return EC.Int64(key, int64(t)), nil
}
case uint64:
switch {
case t < math.MaxInt32:
return EC.Int32(key, int32(t)), nil
case t > math.MaxInt64:
return nil, fmt.Errorf("BSON only has signed integer types and %d overflows an int64", t)
default:
return EC.Int64(key, int64(t)), nil
}
case DocumentMarshaler:
return ECE.DocumentMarshaler(key, t)
case Marshaler:
return ECE.Marshaler(key, t)
default:
if t == nil {
return EC.Null(key), nil
}
elem := EC.Interface(key, t)
if elem.Value().Type() == bsontype.Null {
return nil, fmt.Errorf("Cannot create element for type %T, try using bsoncodec.ConstructElementErr", value)
}
return elem, nil
}
}
// Double creates a double element with the given key and value.
func (ElementConstructor) Double(key string, f float64) *Element {
b := make([]byte, 1+len(key)+1+8)
elem := newElement(0, 1+uint32(len(key))+1)
if _, err := elements.Double.Element(0, b, key, f); err != nil {
panic(err)
}
elem.value.data = b
return elem
}
// String creates a string element with the given key and value.
func (ElementConstructor) String(key string, val string) *Element {
size := uint32(1 + len(key) + 1 + 4 + len(val) + 1)
b := make([]byte, size)
elem := newElement(0, 1+uint32(len(key))+1)
if _, err := elements.String.Element(0, b, key, val); err != nil {
panic(err)
}
elem.value.data = b
return elem
}
// SubDocument creates a subdocument element with the given key and value.
func (ElementConstructor) SubDocument(key string, d *Document) *Element {
size := uint32(1 + len(key) + 1)
b := make([]byte, size)
elem := newElement(0, size)
if _, err := elements.Byte.Encode(0, b, '\x03'); err != nil {
panic(err)
}
if _, err := elements.CString.Encode(1, b, key); err != nil {
panic(err)
}
elem.value.data = b
elem.value.d = d
return elem
}
func (ElementConstructor) SubDocumentFromReader(key string, r Reader) *Element {
size := uint32(1 + len(key) + 1 + len(r))
b := make([]byte, size)
elem := newElement(0, uint32(1+len(key)+1))
if _, err := elements.Byte.Encode(0, b, '\x03'); err != nil {
panic(err)
}
if _, err := elements.CString.Encode(1, b, key); err != nil {
panic(err)
}
// NOTE: We don't validate the Reader here since we don't validate the
// Document when provided to SubDocument.
copy(b[1+len(key)+1:], r)
elem.value.data = b
return elem
}
// SubDocumentFromElements creates a subdocument element with the given key. The elements passed as
// arguments will be used to create a new document as the value.
func (ElementConstructor) SubDocumentFromElements(key string, elems ...*Element) *Element {
return EC.SubDocument(key, DC.Elements(elems...))
}
// Array creates an array element with the given key and value.
func (ElementConstructor) Array(key string, a *Array) *Element {
size := uint32(1 + len(key) + 1)
b := make([]byte, size)
elem := newElement(0, size)
if _, err := elements.Byte.Encode(0, b, '\x04'); err != nil {
panic(err)
}
if _, err := elements.CString.Encode(1, b, key); err != nil {
panic(err)
}
elem.value.data = b
elem.value.d = a.doc
return elem
}
// ArrayFromElements creates an element with the given key. The elements passed as
// arguments will be used to create a new array as the value.
func (ElementConstructor) ArrayFromElements(key string, values ...*Value) *Element {
return EC.Array(key, NewArray(values...))
}
// Binary creates a binary element with the given key and value.
func (ElementConstructor) Binary(key string, b []byte) *Element {
return EC.BinaryWithSubtype(key, b, 0)
}
// BinaryWithSubtype creates a binary element with the given key. It will create a new BSON binary value
// with the given data and subtype.
func (ElementConstructor) BinaryWithSubtype(key string, b []byte, btype byte) *Element {
size := uint32(1 + len(key) + 1 + 4 + 1 + len(b))
if btype == 2 {
size += 4
}
buf := make([]byte, size)
elem := newElement(0, 1+uint32(len(key))+1)
if _, err := elements.Binary.Element(0, buf, key, b, btype); err != nil {
panic(err)
}
elem.value.data = buf
return elem
}
// Undefined creates a undefined element with the given key.
func (ElementConstructor) Undefined(key string) *Element {
size := 1 + uint32(len(key)) + 1
b := make([]byte, size)
elem := newElement(0, size)
if _, err := elements.Byte.Encode(0, b, '\x06'); err != nil {
panic(err)
}
if _, err := elements.CString.Encode(1, b, key); err != nil {
panic(err)
}
elem.value.data = b
return elem
}
// ObjectID creates a objectid element with the given key and value.
func (ElementConstructor) ObjectID(key string, oid types.ObjectID) *Element {
size := uint32(1 + len(key) + 1 + 12)
elem := newElement(0, 1+uint32(len(key))+1)
elem.value.data = make([]byte, size)
if _, err := elements.ObjectID.Element(0, elem.value.data, key, oid); err != nil {
panic(err)
}
return elem
}
// Boolean creates a boolean element with the given key and value.
func (ElementConstructor) Boolean(key string, b bool) *Element {
size := uint32(1 + len(key) + 1 + 1)
elem := newElement(0, 1+uint32(len(key))+1)
elem.value.data = make([]byte, size)
if _, err := elements.Boolean.Element(0, elem.value.data, key, b); err != nil {
panic(err)
}
return elem
}
// DateTime creates a datetime element with the given key and value.
// dt represents milliseconds since the Unix epoch
func (ElementConstructor) DateTime(key string, dt int64) *Element {
size := uint32(1 + len(key) + 1 + 8)
elem := newElement(0, 1+uint32(len(key))+1)
elem.value.data = make([]byte, size)
if _, err := elements.DateTime.Element(0, elem.value.data, key, dt); err != nil {
panic(err)
}
return elem
}
// Time creates a datetime element with the given key and value.
func (ElementConstructor) Time(key string, t time.Time) *Element {
// Apply nanoseconds to milliseconds conversion
return EC.DateTime(key, t.Unix()*1000+int64(t.Nanosecond()/1e6))
}
// Null creates a null element with the given key.
func (ElementConstructor) Null(key string) *Element {
size := uint32(1 + len(key) + 1)
b := make([]byte, size)
elem := newElement(0, uint32(1+len(key)+1))
if _, err := elements.Byte.Encode(0, b, '\x0A'); err != nil {
panic(err)
}
if _, err := elements.CString.Encode(1, b, key); err != nil {
panic(err)
}
elem.value.data = b
return elem
}
// Regex creates a regex element with the given key and value.
func (ElementConstructor) Regex(key string, pattern, options string) *Element {
size := uint32(1 + len(key) + 1 + len(pattern) + 1 + len(options) + 1)
elem := newElement(0, uint32(1+len(key)+1))
elem.value.data = make([]byte, size)
_, err := elements.Regex.Element(0, elem.value.data, key, pattern, options)
if err != nil {
panic(err)
}
return elem
}
// DBPointer creates a dbpointer element with the given key and value.
func (ElementConstructor) DBPointer(key string, ns string, oid types.ObjectID) *Element {
size := uint32(1 + len(key) + 1 + 4 + len(ns) + 1 + 12)
elem := newElement(0, uint32(1+len(key)+1))
elem.value.data = make([]byte, size)
_, err := elements.DBPointer.Element(0, elem.value.data, key, ns, oid)
if err != nil {
panic(err)
}
return elem
}
// JavaScript creates a JavaScript code element with the given key and value.
func (ElementConstructor) JavaScript(key string, code string) *Element {
size := uint32(1 + len(key) + 1 + 4 + len(code) + 1)
elem := newElement(0, uint32(1+len(key)+1))
elem.value.data = make([]byte, size)
_, err := elements.JavaScript.Element(0, elem.value.data, key, code)
if err != nil {
panic(err)
}
return elem
}
// Symbol creates a symbol element with the given key and value.
func (ElementConstructor) Symbol(key string, symbol string) *Element {
size := uint32(1 + len(key) + 1 + 4 + len(symbol) + 1)
elem := newElement(0, uint32(1+len(key)+1))
elem.value.data = make([]byte, size)
_, err := elements.Symbol.Element(0, elem.value.data, key, symbol)
if err != nil {
panic(err)
}
return elem
}
// CodeWithScope creates a JavaScript code with scope element with the given key and value.
func (ElementConstructor) CodeWithScope(key string, code string, scope *Document) *Element {
size := uint32(1 + len(key) + 1 + 4 + 4 + len(code) + 1)
elem := newElement(0, uint32(1+len(key)+1))
elem.value.data = make([]byte, size)
elem.value.d = scope
_, err := elements.Byte.Encode(0, elem.value.data, '\x0F')
if err != nil {
panic(err)
}
_, err = elements.CString.Encode(1, elem.value.data, key)
if err != nil {
panic(err)
}
_, err = elements.Int32.Encode(1+uint(len(key))+1, elem.value.data, int32(size))
if err != nil {
panic(err)
}
_, err = elements.String.Encode(1+uint(len(key))+1+4, elem.value.data, code)
if err != nil {
panic(err)
}
return elem
}
// Int32 creates a int32 element with the given key and value.
func (ElementConstructor) Int32(key string, i int32) *Element {
size := uint32(1 + len(key) + 1 + 4)
elem := newElement(0, uint32(1+len(key)+1))
elem.value.data = make([]byte, size)
_, err := elements.Int32.Element(0, elem.value.data, key, i)
if err != nil {
panic(err)
}
return elem
}
// Timestamp creates a timestamp element with the given key and value.
func (ElementConstructor) Timestamp(key string, t uint32, i uint32) *Element {
size := uint32(1 + len(key) + 1 + 8)
elem := newElement(0, uint32(1+len(key)+1))
elem.value.data = make([]byte, size)
_, err := elements.Timestamp.Element(0, elem.value.data, key, t, i)
if err != nil {
panic(err)
}
return elem
}
// Int64 creates a int64 element with the given key and value.
func (ElementConstructor) Int64(key string, i int64) *Element {
size := uint32(1 + len(key) + 1 + 8)
elem := newElement(0, 1+uint32(len(key))+1)
elem.value.data = make([]byte, size)
_, err := elements.Int64.Element(0, elem.value.data, key, i)
if err != nil {
panic(err)
}
return elem
}
// Decimal128 creates a decimal element with the given key and value.
func (ElementConstructor) Decimal128(key string, d types.Decimal128) *Element {
size := uint32(1 + len(key) + 1 + 16)
elem := newElement(0, uint32(1+len(key)+1))
elem.value.data = make([]byte, size)
_, err := elements.Decimal128.Element(0, elem.value.data, key, d)
if err != nil {
panic(err)
}
return elem
}
// MinKey creates a minkey element with the given key and value.
func (ElementConstructor) MinKey(key string) *Element {
size := uint32(1 + len(key) + 1)
elem := newElement(0, uint32(1+len(key)+1))
elem.value.data = make([]byte, size)
_, err := elements.Byte.Encode(0, elem.value.data, '\xFF')
if err != nil {
panic(err)
}
_, err = elements.CString.Encode(1, elem.value.data, key)
if err != nil {
panic(err)
}
return elem
}
// MaxKey creates a maxkey element with the given key and value.
func (ElementConstructor) MaxKey(key string) *Element {
size := uint32(1 + len(key) + 1)
elem := newElement(0, uint32(1+len(key)+1))
elem.value.data = make([]byte, size)
_, err := elements.Byte.Encode(0, elem.value.data, '\x7F')
if err != nil {
panic(err)
}
_, err = elements.CString.Encode(1, elem.value.data, key)
if err != nil {
panic(err)
}
return elem
}
// Value constructs an element using the underlying value.
func (ElementConstructor) Value(key string, value *Value) *Element {
return convertValueToElem(key, value)
}
// Double creates a double element with the given value.
func (ValueConstructor) Double(f float64) *Value {
return EC.Double("", f).value
}
// String creates a string element with the given value.
func (ValueConstructor) String(val string) *Value {
return EC.String("", val).value
}
// Document creates a subdocument value from the argument.
func (ValueConstructor) Document(d *Document) *Value {
return EC.SubDocument("", d).value
}
// DocumentFromReader creates a subdocument element from the given value.
func (ValueConstructor) DocumentFromReader(r Reader) *Value {
return EC.SubDocumentFromReader("", r).value
}
// DocumentFromElements creates a subdocument element from the given elements.
func (ValueConstructor) DocumentFromElements(elems ...*Element) *Value {
return EC.SubDocumentFromElements("", elems...).value
}
// Array creates an array value from the argument.
func (ValueConstructor) Array(a *Array) *Value {
return EC.Array("", a).value
}
// ArrayFromValues creates an array element from the given the elements.
func (ValueConstructor) ArrayFromValues(values ...*Value) *Value {
return EC.ArrayFromElements("", values...).value
}
// Binary creates a binary value from the argument.
func (ValueConstructor) Binary(b []byte) *Value {
return VC.BinaryWithSubtype(b, 0)
}
// BinaryWithSubtype creates a new binary element with the given data and subtype.
func (ValueConstructor) BinaryWithSubtype(b []byte, btype byte) *Value {
return EC.BinaryWithSubtype("", b, btype).value
}
// Undefined creates a undefined element.
func (ValueConstructor) Undefined() *Value {
return EC.Undefined("").value
}
// ObjectID creates a objectid value from the argument.
func (ValueConstructor) ObjectID(oid types.ObjectID) *Value {
return EC.ObjectID("", oid).value
}
// Boolean creates a boolean value from the argument.
func (ValueConstructor) Boolean(b bool) *Value {
return EC.Boolean("", b).value
}
// Time creates a datetime value from the argument.
func (ValueConstructor) Time(t time.Time) *Value {
return EC.Time("", t).value
}
// DateTime creates a datetime value from the argument.
func (ValueConstructor) DateTime(dt int64) *Value {
return EC.DateTime("", dt).value
}
// Null creates a null value from the argument.
func (ValueConstructor) Null() *Value {
return EC.Null("").value
}
// Regex creates a regex value from the arguments.
func (ValueConstructor) Regex(pattern, options string) *Value {
return EC.Regex("", pattern, options).value
}
// DBPointer creates a dbpointer value from the arguments.
func (ValueConstructor) DBPointer(ns string, oid types.ObjectID) *Value {
return EC.DBPointer("", ns, oid).value
}
// JavaScript creates a JavaScript code value from the argument.
func (ValueConstructor) JavaScript(code string) *Value {
return EC.JavaScript("", code).value
}
// Symbol creates a symbol value from the argument.
func (ValueConstructor) Symbol(symbol string) *Value {
return EC.Symbol("", symbol).value
}
// CodeWithScope creates a JavaScript code with scope value from the arguments.
func (ValueConstructor) CodeWithScope(code string, scope *Document) *Value {
return EC.CodeWithScope("", code, scope).value
}
// Int32 creates a int32 value from the argument.
func (ValueConstructor) Int32(i int32) *Value {
return EC.Int32("", i).value
}
// Timestamp creates a timestamp value from the arguments.
func (ValueConstructor) Timestamp(t uint32, i uint32) *Value {
return EC.Timestamp("", t, i).value
}
// Int64 creates a int64 value from the argument.
func (ValueConstructor) Int64(i int64) *Value {
return EC.Int64("", i).value
}
// Decimal128 creates a decimal value from the argument.
func (ValueConstructor) Decimal128(d types.Decimal128) *Value {
return EC.Decimal128("", d).value
}
// MinKey creates a minkey value from the argument.
func (ValueConstructor) MinKey() *Value {
return EC.MinKey("").value
}
// MaxKey creates a maxkey value from the argument.
func (ValueConstructor) MaxKey() *Value {
return EC.MaxKey("").value
}