Skip to content

Commit 98a9818

Browse files
committed
support for non array walk removals
1 parent 9fe7997 commit 98a9818

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

bsonutil.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,19 @@ func BSONWalk(doc bson.D, pathString string, visitor BSONWalkVisitor) (bson.D, e
148148
var DELETE_ME = fmt.Errorf("delete_me")
149149

150150
func BSONWalkHelp(doc bson.D, path []string, visitor BSONWalkVisitor, inArray bool) (bson.D, error) {
151+
prev := doc
151152
current := doc
152153

154+
docPath := []int{}
155+
153156
for pieceOffset, piece := range path {
154157
idx := BSONIndexOf(current, piece)
155158
//fmt.Printf("XX %d %s %d\n", pieceOffset, piece, idx)
156159

157160
if idx < 0 {
158161
return doc, nil
159162
}
163+
docPath = append(docPath, idx)
160164

161165
elem := &(current)[idx]
162166

@@ -171,7 +175,13 @@ func BSONWalkHelp(doc bson.D, path []string, visitor BSONWalkVisitor, inArray bo
171175
if inArray {
172176
return bson.D{}, DELETE_ME
173177
} else {
174-
panic("hi")
178+
fixed := append(current[0:idx], current[idx+1:]...)
179+
if pieceOffset == 0 {
180+
return fixed, nil
181+
}
182+
183+
prev[docPath[len(docPath)-2]].Value = fixed
184+
return doc, nil
175185
}
176186
}
177187

@@ -185,6 +195,7 @@ func BSONWalkHelp(doc bson.D, path []string, visitor BSONWalkVisitor, inArray bo
185195

186196
switch val := elem.Value.(type) {
187197
case bson.D:
198+
prev = current
188199
current = val
189200
case []bson.D:
190201
numDeleted := 0

bsonutil_test.go

+76
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,79 @@ func TestBSONWalk6(test *testing.T) {
164164
}
165165

166166
}
167+
168+
func TestBSONWalk7(test *testing.T) {
169+
doc := bson.D{{"a", 111}, {"b", 3}}
170+
walker := &testWalker{}
171+
doc, err := BSONWalk(doc, "a", walker)
172+
if err != nil {
173+
test.Errorf("why did we get an error %s", err)
174+
}
175+
if len(doc) != 1 {
176+
test.Errorf("didn't delete 1 %s", doc)
177+
}
178+
if doc[0].Name != "b" {
179+
test.Errorf("deleted wrong one? %s", doc)
180+
}
181+
182+
}
183+
184+
func TestBSONWalk8(test *testing.T) {
185+
doc := bson.D{{"b", 11}, {"a", 111}}
186+
walker := &testWalker{}
187+
doc, err := BSONWalk(doc, "a", walker)
188+
if err != nil {
189+
test.Errorf("why did we get an error %s", err)
190+
}
191+
if len(doc) != 1 {
192+
test.Errorf("didn't delete 1 %s", doc)
193+
}
194+
if doc[0].Name != "b" {
195+
test.Errorf("deleted wrong one? %s", doc)
196+
}
197+
198+
}
199+
200+
func TestBSONWalk9(test *testing.T) {
201+
doc := bson.D{{"b", 11}, {"a", 111}, {"c", 12}}
202+
walker := &testWalker{}
203+
doc, err := BSONWalk(doc, "a", walker)
204+
if err != nil {
205+
test.Errorf("why did we get an error %s", err)
206+
}
207+
if len(doc) != 2 {
208+
test.Errorf("didn't delete 1 %s", doc)
209+
}
210+
if doc[0].Name != "b" {
211+
test.Errorf("deleted wrong one? %s", doc)
212+
}
213+
if doc[1].Name != "c" {
214+
test.Errorf("deleted wrong one? %s", doc)
215+
}
216+
217+
}
218+
219+
func TestBSONWalk10(test *testing.T) {
220+
doc := bson.D{{"b", 11}, {"a", bson.D{{"x", 1}, {"y", 111}}}, {"c", 12}}
221+
walker := &testWalker{}
222+
doc, err := BSONWalk(doc, "a.y", walker)
223+
if err != nil {
224+
test.Errorf("why did we get an error %s", err)
225+
}
226+
if len(doc) != 3 {
227+
test.Errorf("what did i do! %s", doc)
228+
}
229+
230+
if doc[1].Name != "a" {
231+
test.Errorf("what did i do! %s", doc)
232+
}
233+
234+
sub := doc[1].Value.(bson.D)
235+
if len(sub) != 1 {
236+
test.Errorf("didn't delete %s", doc)
237+
}
238+
if sub[0].Name != "x" {
239+
test.Errorf("deleted wrong one? %s", doc)
240+
}
241+
242+
}

0 commit comments

Comments
 (0)