forked from tolsen/mongonet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bsonutil.go
412 lines (366 loc) · 9.04 KB
/
bsonutil.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
package mongonet
import (
"fmt"
"reflect"
"strings"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/x/bsonx"
)
var (
bsonRegistry = bson.NewRegistryBuilder().
RegisterCodec(reflect.TypeOf(primitive.D{}), bsonx.ReflectionFreeDCodec).
Build()
)
type SimpleBSON struct {
Size int32
BSON []byte
}
func SimpleBSONConvert(d interface{}) (SimpleBSON, error) {
raw, err := bson.MarshalWithRegistry(bsonRegistry, d)
if err != nil {
return SimpleBSON{}, err
}
return SimpleBSON{int32(len(raw)), raw}, nil
}
func SimpleBSONConvertOrPanic(d interface{}) SimpleBSON {
raw, err := bson.MarshalWithRegistry(bsonRegistry, d)
if err != nil {
panic(err)
}
return SimpleBSON{int32(len(raw)), raw}
}
func (sb SimpleBSON) ToBSOND() (bson.D, error) {
t := bson.D{}
err := bson.UnmarshalWithRegistry(bsonRegistry, sb.BSON, &t)
return t, err
}
func (sb SimpleBSON) Copy(loc *int, buf []byte) {
copy(buf[*loc:], sb.BSON)
*loc = *loc + int(sb.Size)
}
func parseSimpleBSON(b []byte) (SimpleBSON, error) {
if len(b) < 4 {
return SimpleBSON{}, NewStackErrorf("invalid bson -- length of bytes must be at least 4, not %v", len(b))
}
size := readInt32(b)
if int(size) == 0 {
// shortcut in wire protocol
return SimpleBSON{4, b}, nil
}
if int(size) > (128*1024*1024) || int(size) < 0 {
return SimpleBSON{}, NewStackErrorf("bson size invalid %d", size)
}
if int(size) > len(b) {
return SimpleBSON{}, NewStackErrorf("invalid bson -- size = %v is greater than length of bytes = %v", size, len(b))
}
return SimpleBSON{size, b[0:int(size)]}, nil
}
func SimpleBSONEmpty() SimpleBSON {
return SimpleBSON{int32(5), []byte{5, 0, 0, 0, 0}}
}
// ---------
func BSONIndexOf(doc bson.D, name string) int {
for i, elem := range doc {
if elem.Key == name {
return i
}
}
return -1
}
func GetAsString(elem bson.E) (string, string, error) {
tipe := fmt.Sprintf("%T", elem.Value)
switch val := elem.Value.(type) {
case string:
return val, tipe, nil
default:
return "", tipe, NewStackErrorf("not a string %T %s", val, val)
}
}
func GetAsInt(elem bson.E) (int, string, error) {
tipe := fmt.Sprintf("%T", elem.Value)
switch val := elem.Value.(type) {
case int:
return val, tipe, nil
case int32:
return int(val), tipe, nil
case int64:
return int(val), tipe, nil
case float64:
return int(val), tipe, nil
default:
return 0, tipe, NewStackErrorf("not a number %T %s", val, val)
}
}
func GetAsBool(elem bson.E) (bool, string, error) {
tipe := fmt.Sprintf("%T", elem.Value)
switch val := elem.Value.(type) {
case bool:
return val, tipe, nil
case int:
return val != 0, tipe, nil
case int32:
return int(val) != 0, tipe, nil
case int64:
return int(val) != 0, tipe, nil
case float64:
return val != 0.0, tipe, nil
default:
return false, tipe, NewStackErrorf("not a bool %T %s", val, val)
}
}
func GetAsBSON(elem bson.E) (bson.D, string, error) {
tipe := fmt.Sprintf("%T", elem.Value)
switch val := elem.Value.(type) {
case bson.D:
return val, tipe, nil
default:
return bson.D{}, tipe, NewStackErrorf("not bson %T %s", val, val)
}
}
func GetAsStringArray(elem bson.E) ([]string, string, error) {
tipe := fmt.Sprintf("%T", elem.Value)
switch val := elem.Value.(type) {
case primitive.A:
res := make([]string, len(val))
for num, raw := range []interface{}(val) {
switch fixed := raw.(type) {
case string:
res[num] = fixed
default:
return nil, tipe, NewStackErrorf("not string %T %s", raw, raw)
}
}
return res, tipe, nil
default:
return nil, tipe, NewStackErrorf("not an array %T", elem.Value)
}
}
func getAsBsonDocsArray(val []interface{}, tipe string) ([]bson.D, string, error) {
a := make([]bson.D, len(val))
for num, raw := range val {
switch fixed := raw.(type) {
case bson.D:
a[num] = fixed
default:
return []bson.D{}, tipe, NewStackErrorf("not bson.D %T %s", raw, raw)
}
}
return a, tipe, nil
}
func GetAsBSONDocs(elem bson.E) ([]bson.D, string, error) {
tipe := fmt.Sprintf("%T", elem.Value)
switch val := elem.Value.(type) {
case []bson.D:
return val, tipe, nil
case primitive.A:
return getAsBsonDocsArray([]interface{}(val), tipe)
case []interface{}:
return getAsBsonDocsArray(val, tipe)
default:
return []bson.D{}, tipe, NewStackErrorf("not an array %T", elem.Value)
}
}
// ---
var DELETE_ME = fmt.Errorf("delete_me")
var REMOVE_FIELD = fmt.Errorf("remove_field")
type BSONWalkVisitor interface {
/**
change value
set Name = "" to delete
*/
Visit(elem *bson.E) error
}
// BSONWalkAll - recursively walks the BSON doc and applies the visitor when encountering the fieldName
// If delete_me is encountered, it'll return an empty document for that element
func BSONWalkAll(doc bson.D, fieldName string, visitor BSONWalkVisitor) (bson.D, error) {
current := doc
for i, elem := range current {
elemDoc := &(current)[i]
if elem.Key == fieldName {
err := visitor.Visit(elemDoc)
if err != nil {
if err == DELETE_ME {
return nil, nil
}
if err == REMOVE_FIELD {
return append(current[:i], current[i+1:]...), nil
}
return nil, err
}
}
var valToUse []interface{}
switch val := elem.Value.(type) {
case bson.D:
newDoc, err := BSONWalkAll(val, fieldName, visitor)
if err != nil {
return nil, err
}
elem.Value = newDoc
case []bson.D:
for arrayOffset, sub := range val {
newDoc, err := BSONWalkAll(sub, fieldName, visitor)
if err != nil {
return nil, err
}
val[arrayOffset] = newDoc
}
case primitive.A:
valToUse = []interface{}(val)
case []interface{}:
valToUse = val
}
if len(valToUse) == 0 {
continue
}
for arrayOffset, subRaw := range valToUse {
switch sub := subRaw.(type) {
case bson.D:
newDoc, err := BSONWalkAll(sub, fieldName, visitor)
if err != nil {
return nil, err
}
valToUse[arrayOffset] = newDoc
default:
// won't alter nested arrays (e.g. [[1,2,3],[4,5,6]]) - will set them as-is
valToUse[arrayOffset] = sub
}
}
}
return doc, nil
}
// BSONWalk - applies the visitor on the select path
func BSONWalk(doc bson.D, pathString string, visitor BSONWalkVisitor) (bson.D, error) {
path := strings.Split(pathString, ".")
return BSONWalkHelp(doc, path, visitor, false)
}
func BSONWalkHelp(doc bson.D, path []string, visitor BSONWalkVisitor, inArray bool) (bson.D, error) {
prev := doc
current := doc
docPath := []int{}
for pieceOffset, piece := range path {
idx := BSONIndexOf(current, piece)
if idx < 0 {
return doc, nil
}
docPath = append(docPath, idx)
elem := &(current)[idx]
if pieceOffset == len(path)-1 {
// this is the end
if len(elem.Key) == 0 {
panic("this is not ok right now")
}
err := visitor.Visit(elem)
if err != nil {
if err == DELETE_ME {
if inArray {
return bson.D{}, DELETE_ME
}
fixed := append(current[0:idx], current[idx+1:]...)
if pieceOffset == 0 {
return fixed, nil
}
prev[docPath[len(docPath)-2]].Value = fixed
return doc, nil
}
return nil, err
}
return doc, nil
}
// more to walk down
var valToUse []interface{}
switch val := elem.Value.(type) {
case bson.D:
prev = current
current = val
continue
case []bson.D:
numDeleted := 0
for arrayOffset, sub := range val {
newDoc, err := BSONWalkHelp(sub, path[pieceOffset+1:], visitor, true)
if err == DELETE_ME {
newDoc = nil
numDeleted++
} else if err != nil {
return nil, err
}
val[arrayOffset] = newDoc
}
if numDeleted > 0 {
newArr := make([]bson.D, len(val)-numDeleted)
pos := 0
for _, sub := range val {
if sub != nil {
newArr[pos] = sub
pos++
}
}
current[idx].Value = newArr
}
return doc, nil
case primitive.A:
valToUse = []interface{}(val)
case []interface{}:
valToUse = val
default:
return doc, nil
}
numDeleted := 0
for arrayOffset, subRaw := range valToUse {
switch sub := subRaw.(type) {
case bson.D:
newDoc, err := BSONWalkHelp(sub, path[pieceOffset+1:], visitor, true)
if err == DELETE_ME {
newDoc = nil
numDeleted++
} else if err != nil {
return nil, err
}
valToUse[arrayOffset] = newDoc
default:
valToUse[arrayOffset] = sub
}
}
if numDeleted > 0 {
newArr := make([]interface{}, len(valToUse)-numDeleted)
pos := 0
for _, sub := range valToUse {
if sub != nil && len(sub.(bson.D)) > 0 {
newArr[pos] = sub
pos++
}
}
current[idx].Value = newArr
}
return doc, nil
}
return doc, nil
}
func BSONGetValueByNestedPathForTests(doc bson.D, nestedPath string, arrIndex int) interface{} {
tempDoc := doc
parts := strings.Split(nestedPath, ".")
var ix int
for _, p := range parts {
ix = BSONIndexOf(tempDoc, p)
if ix < 0 {
return nil
}
switch v := tempDoc[ix].Value.(type) {
case bson.D:
tempDoc = v
case primitive.A:
if arrIndex < 0 || len(v) <= arrIndex {
return v
}
switch v2 := v[arrIndex].(type) {
case bson.D:
tempDoc = v2
default:
return v2
}
default:
return tempDoc[ix].Value
}
}
return tempDoc
}