-
Notifications
You must be signed in to change notification settings - Fork 19
/
bsonutil.go
277 lines (232 loc) · 5.52 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
package mongonet
import "fmt"
import "strings"
import "gopkg.in/mgo.v2/bson"
type SimpleBSON struct {
Size int32
BSON []byte
}
func SimpleBSONConvert(d interface{}) (SimpleBSON, error) {
raw, err := bson.Marshal(d)
if err != nil {
return SimpleBSON{}, err
}
return SimpleBSON{int32(len(raw)), raw}, nil
}
func SimpleBSONConvertOrPanic(d interface{}) SimpleBSON {
raw, err := bson.Marshal(d)
if err != nil {
panic(err)
}
return SimpleBSON{int32(len(raw)), raw}
}
func (sb SimpleBSON) ToBSOND() (bson.D, error) {
t := bson.D{}
err := bson.Unmarshal(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) {
size := readInt32(b)
if int(size) == 0 {
// shortcut in wire protocol
return SimpleBSON{4, b}, nil
}
if int(size) > (128 * 1024 * 1024) {
return SimpleBSON{}, NewStackErrorf("bson size invalid %d", size)
}
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.Name == name {
return i
}
}
return -1
}
func GetAsString(elem bson.DocElem) (string, error) {
switch val := elem.Value.(type) {
case string:
return val, nil
default:
return "", NewStackErrorf("not a string %T %s", val, val)
}
}
func GetAsInt(elem bson.DocElem) (int, error) {
switch val := elem.Value.(type) {
case int:
return val, nil
case int32:
return int(val), nil
case int64:
return int(val), nil
case float64:
return int(val), nil
default:
return 0, NewStackErrorf("not a number %T %s", val, val)
}
}
func GetAsBool(elem bson.DocElem) (bool, error) {
switch val := elem.Value.(type) {
case bool:
return val, nil
case int:
return val != 0, nil
case int32:
return int(val) != 0, nil
case int64:
return int(val) != 0, nil
case float64:
return val != 0.0, nil
default:
return false, NewStackErrorf("not a bool %T %s", val, val)
}
}
func GetAsBSON(elem bson.DocElem) (bson.D, error) {
switch val := elem.Value.(type) {
case bson.D:
return val, nil
default:
return bson.D{}, NewStackErrorf("not bson %T %s", val, val)
}
}
func GetAsBSONDocs(elem bson.DocElem) ([]bson.D, error) {
switch val := elem.Value.(type) {
case []bson.D:
return val, nil
case []interface{}:
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{}, NewStackErrorf("not bson.D %T %s", raw, raw)
}
}
return a, nil
default:
return []bson.D{}, NewStackErrorf("not an array %T", elem.Value)
}
}
// ---
type BSONWalkVisitor interface {
/**
change value
set Name = "" to delete
*/
Visit(elem *bson.DocElem) error
}
func BSONWalk(doc bson.D, pathString string, visitor BSONWalkVisitor) (bson.D, error) {
path := strings.Split(pathString, ".")
return BSONWalkHelp(doc, path, visitor, false)
}
var DELETE_ME = fmt.Errorf("delete_me")
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)
//fmt.Printf("XX %d %s %d\n", pieceOffset, piece, idx)
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.Name) == 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
} else {
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, fmt.Errorf("error visiting node %s", err)
}
return doc, nil
}
// more to walk down
switch val := elem.Value.(type) {
case bson.D:
prev = current
current = val
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, fmt.Errorf("error going deeper into array %s", 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 []interface{}:
numDeleted := 0
for arrayOffset, subRaw := range val {
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, fmt.Errorf("error going deeper into array %s", err)
}
val[arrayOffset] = newDoc
default:
return nil, fmt.Errorf("bad type going deeper into array %s", sub)
}
}
if numDeleted > 0 {
newArr := make([]interface{}, len(val)-numDeleted)
pos := 0
for _, sub := range val {
if sub != nil && len(sub.(bson.D)) > 0 {
newArr[pos] = sub
pos++
}
}
current[idx].Value = newArr
}
return doc, nil
default:
//fmt.Printf("hi %#v\n", elem.Value)
return doc, nil
}
}
return doc, nil
}