-
Notifications
You must be signed in to change notification settings - Fork 0
/
x_constructor.go
468 lines (368 loc) · 11.2 KB
/
x_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
package birch
import (
"fmt"
"io"
"math"
"time"
"github.com/tychoish/fun/ft"
)
// DC is a convenience variable provided for access to the DocumentConstructor methods.
var DC DocumentConstructor
// DCE is a convenience variable provided for access to the DocumentConstructorError methods.
var DCE DocumentConstructorError
// DocumentConstructor is used as a namespace for document constructor
// functions. Constructor methods may panic in cases when invalid
// input would cause them to error when using the DocumentConstructorError
type DocumentConstructor struct{}
// DocumentConstructor is used as a namespace for document constructor
// functions. These methods return errors rather than panicing in the
// case of invalid input
type DocumentConstructorError struct{}
// New returns an empty document.
func (DocumentConstructor) New() *Document { return DC.Make(0) }
// Make returns a document with the underlying storage
// allocated as specified. Provides some efficiency when building
// larger documents iteratively.
func (DocumentConstructor) Make(n int) *Document { return &Document{elems: make([]*Element, 0, n)} }
// Elements returns a document initialized with the elements passed as
// arguments.
func (DocumentConstructor) Elements(elems ...*Element) *Document {
return DC.Make(len(elems)).Append(elems...)
}
// ElementsOmitEmpty crates a document with all non-empty values.
func (DocumentConstructor) ElementsOmitEmpty(elems ...*Element) *Document {
return DC.Make(len(elems)).AppendOmitEmpty(elems...)
}
// Reader constructs a document from a bson reader, which is a wrapper
// around a byte slice representation of a bson document. Reader
// panics if there is a problem reading the document.
func (DocumentConstructor) Reader(r Reader) *Document {
return ft.Must(DCE.Reader(r))
}
// ReaderErr constructs a document from a bson reader, which is a wrapper
// around a byte slice representation of a bson document. Reader
// returns an error if there is a problem reading the document.
func (DocumentConstructorError) Reader(r Reader) (*Document, error) {
return ReadDocument(r)
}
// ReadFrom builds a document reading a bytes sequence from an
// io.Reader, panicing if there's a problem reading from the reader.
func (DocumentConstructor) ReadFrom(in io.Reader) *Document {
doc, err := DCE.ReadFrom(in)
if err == io.EOF {
return nil
}
if err != nil {
panic(err)
}
return doc
}
// ReadFromErr builds a document reading a bytes sequence from an
// io.Reader, returning an error if there's a problem reading from the
// reader.
func (DocumentConstructorError) ReadFrom(in io.Reader) (*Document, error) {
doc := DC.New()
_, err := doc.ReadFrom(in)
if err == io.EOF {
return nil, err
}
if err != nil {
return nil, err
}
return doc, nil
}
func (DocumentConstructor) Marshaler(in Marshaler) *Document {
return ft.Must(DCE.Marshaler(in))
}
func (DocumentConstructorError) Marshaler(in Marshaler) (*Document, error) {
data, err := in.MarshalBSON()
if err != nil {
return nil, err
}
return DCE.Reader(data)
}
func (DocumentConstructor) MapString(in map[string]string) *Document {
out := DC.Make(len(in))
for k, v := range in {
out.Append(EC.String(k, v))
}
return out
}
func (DocumentConstructor) MapInterface(in map[string]any) *Document {
out := DC.Make(len(in))
for k, v := range in {
out.Append(EC.Interface(k, v))
}
return out
}
func (DocumentConstructorError) MapInterface(in map[string]any) (*Document, error) {
out := DC.Make(len(in))
for k, v := range in {
elem, err := ECE.Interface(k, v)
if err != nil {
return nil, err
}
if elem != nil {
out.Append(elem)
}
}
return out, nil
}
func (DocumentConstructorError) MapInterfaceInterface(in map[any]any) (*Document, error) {
out := DC.Make(len(in))
for k, v := range in {
elem, err := ECE.Interface(bestStringAttempt(k), v)
if err != nil {
return nil, err
}
if elem != nil {
out.Append(elem)
}
}
return out, nil
}
func (DocumentConstructor) MapInterfaceInterface(in map[any]any) *Document {
out := DC.Make(len(in))
for k, v := range in {
out.Append(EC.Interface(bestStringAttempt(k), v))
}
return out
}
func (DocumentConstructor) Interface(value any) *Document {
var (
doc *Document
err error
)
switch t := value.(type) {
case map[string]string:
doc = DC.MapString(t)
case map[string]any:
doc = DC.MapInterface(t)
case map[any]any:
doc = DC.Make(len(t))
for k, v := range t {
doc.Append(EC.Interface(bestStringAttempt(k), v))
}
case *Element:
doc = DC.Elements(t)
case *Document:
doc = t
case Reader:
doc, err = DCE.Reader(t)
case DocumentMarshaler:
doc, err = t.MarshalDocument()
case Marshaler:
doc, err = DCE.Marshaler(t)
case []*Element:
doc = DC.Elements(t...)
}
if err != nil || doc == nil {
return DC.New()
}
return doc
}
func (DocumentConstructorError) Interface(value any) (*Document, error) {
switch t := value.(type) {
case map[string]string:
return DC.MapString(t), nil
case map[string]any:
return DCE.MapInterface(t)
case map[any]any:
return DCE.MapInterfaceInterface(t)
case Reader:
return DCE.Reader(t)
case *Element:
return DC.Elements(t), nil
case []*Element:
return DC.Elements(t...), nil
case *Document:
return t, nil
case DocumentMarshaler:
return t.MarshalDocument()
case Marshaler:
return DCE.Marshaler(t)
default:
return nil, fmt.Errorf("value '%s' is of type '%T' which is not convertable to a document.", t, t)
}
}
func (ElementConstructor) Marshaler(key string, val Marshaler) *Element {
elem, err := ECE.Marshaler(key, val)
if err != nil {
panic(err)
}
return elem
}
func (ElementConstructorError) Marshaler(key string, val Marshaler) (*Element, error) {
doc, err := val.MarshalBSON()
if err != nil {
return nil, err
}
return EC.SubDocumentFromReader(key, doc), nil
}
func (ElementConstructor) DocumentMarshaler(key string, val DocumentMarshaler) *Element {
return EC.SubDocument(key, ft.Must(val.MarshalDocument()))
}
func (ElementConstructorError) DocumentMarshaler(key string, val DocumentMarshaler) (*Element, error) {
doc, err := val.MarshalDocument()
if err != nil {
return nil, err
}
return EC.SubDocument(key, doc), nil
}
func (ElementConstructor) Int(key string, i int) *Element {
if i < math.MaxInt32 {
return EC.Int32(key, int32(i))
}
return EC.Int64(key, int64(i))
}
func (ElementConstructor) SliceString(key string, in []string) *Element {
vals := make([]*Value, len(in))
for idx := range in {
vals[idx] = VC.String(in[idx])
}
return EC.Array(key, NewArray(vals...))
}
func (ElementConstructor) SliceInterface(key string, in []any) *Element {
vals := make([]*Value, len(in))
for idx := range in {
vals[idx] = VC.Interface(in[idx])
}
return EC.Array(key, NewArray(vals...))
}
func (ElementConstructorError) SliceInterface(key string, in []any) (*Element, error) {
vals := make([]*Value, 0, len(in))
for idx := range in {
elem, err := VCE.Interface(in[idx])
if err != nil {
return nil, err
}
if elem != nil {
vals = append(vals, elem)
}
}
return EC.Array(key, NewArray(vals...)), nil
}
func (ElementConstructor) SliceInt64(key string, in []int64) *Element {
vals := make([]*Value, len(in))
for idx := range in {
vals[idx] = VC.Int64(in[idx])
}
return EC.Array(key, NewArray(vals...))
}
func (ElementConstructor) SliceInt32(key string, in []int32) *Element {
vals := make([]*Value, len(in))
for idx := range in {
vals[idx] = VC.Int32(in[idx])
}
return EC.Array(key, NewArray(vals...))
}
func (ElementConstructor) SliceFloat64(key string, in []float64) *Element {
vals := make([]*Value, len(in))
for idx := range in {
vals[idx] = VC.Double(in[idx])
}
return EC.Array(key, NewArray(vals...))
}
func (ElementConstructor) SliceFloat32(key string, in []float32) *Element {
vals := make([]*Value, len(in))
for idx := range in {
vals[idx] = VC.Double(float64(in[idx]))
}
return EC.Array(key, NewArray(vals...))
}
func (ElementConstructor) SliceInt(key string, in []int) *Element {
vals := make([]*Value, len(in))
for idx := range in {
vals[idx] = VC.Int(in[idx])
}
return EC.Array(key, NewArray(vals...))
}
func (ElementConstructor) SliceTime(key string, in []time.Time) *Element {
vals := make([]*Value, len(in))
for idx := range in {
vals[idx] = VC.Time(in[idx])
}
return EC.Array(key, NewArray(vals...))
}
func (ElementConstructor) SliceDuration(key string, in []time.Duration) *Element {
vals := make([]*Value, len(in))
for idx := range in {
vals[idx] = VC.Int64(int64(in[idx]))
}
return EC.Array(key, NewArray(vals...))
}
func (ElementConstructor) Duration(key string, t time.Duration) *Element {
return EC.Int64(key, int64(t))
}
func (ValueConstructor) Int(in int) *Value {
return EC.Int("", in).value
}
func (ValueConstructor) Interface(in any) *Value {
return EC.Interface("", in).value
}
func (ValueConstructorError) Interface(in any) (*Value, error) {
elem, err := ECE.Interface("", in)
if err != nil {
return nil, err
}
return elem.value, nil
}
func (ValueConstructor) Marshaler(in Marshaler) *Value {
return EC.Marshaler("", in).value
}
func (ValueConstructorError) Marshaler(in Marshaler) (*Value, error) {
elem, err := ECE.Marshaler("", in)
if err != nil {
return nil, err
}
return elem.value, nil
}
func (ValueConstructor) DocumentMarshaler(in DocumentMarshaler) *Value {
return EC.DocumentMarshaler("", in).value
}
func (ValueConstructorError) DocumentMarshaler(in DocumentMarshaler) (*Value, error) {
elem, err := ECE.DocumentMarshaler("", in)
if err != nil {
return nil, err
}
return elem.value, nil
}
func (ValueConstructor) Duration(t time.Duration) *Value {
return VC.Int64(int64(t))
}
func (ValueConstructor) MapString(in map[string]string) *Value {
return EC.SubDocument("", DC.MapString(in)).value
}
func (ValueConstructor) MapInterface(in map[string]any) *Value {
return EC.SubDocument("", DC.MapInterface(in)).value
}
func (ValueConstructor) MapInterfaceInterface(in map[any]any) *Value {
return EC.SubDocument("", DC.MapInterfaceInterface(in)).value
}
func (ValueConstructorError) MapInterface(in map[string]any) (*Value, error) {
doc, err := DCE.MapInterface(in)
if err != nil {
return nil, err
}
return EC.SubDocument("", doc).value, nil
}
func (ValueConstructor) SliceString(in []string) *Value { return EC.SliceString("", in).value }
func (ValueConstructor) SliceInt(in []int) *Value { return EC.SliceInt("", in).value }
func (ValueConstructor) SliceInt64(in []int64) *Value { return EC.SliceInt64("", in).value }
func (ValueConstructor) SliceInt32(in []int32) *Value { return EC.SliceInt32("", in).value }
func (ValueConstructor) SliceFloat64(in []float64) *Value { return EC.SliceFloat64("", in).value }
func (ValueConstructor) SliceFloat32(in []float32) *Value { return EC.SliceFloat32("", in).value }
func (ValueConstructor) SliceTime(in []time.Time) *Value { return EC.SliceTime("", in).value }
func (ValueConstructor) SliceDuration(in []time.Duration) *Value {
return EC.SliceDuration("", in).value
}
func (ValueConstructor) SliceInterface(in []any) *Value {
return EC.SliceInterface("", in).value
}
func (ValueConstructorError) SliceInterface(in []any) (*Value, error) {
elem, err := ECE.SliceInterface("", in)
if err != nil {
return nil, err
}
return elem.value, nil
}