Skip to content

Commit

Permalink
fix delpaths against nested slicing
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Jun 6, 2020
1 parent fc472c4 commit 6dc7234
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cli/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3175,13 +3175,14 @@
- name: del function
args:
- -c
- 'del(.), del(empty), del((.foo,.bar,.baz) | .[2,3,0]), del(.foo[0], .bar[0], .foo, .baz.bar[0].x)'
- 'del(.), del(empty), del((.foo,.bar,.baz) | .[2,3,0]), del(.foo[0], .bar[0], .foo, .baz.bar[0].x), del(.foo[1:][:3][1:], .bar[:2][1:])'
input: '{"foo": [0,1,2,3,4], "bar": [0,1]}'
expected: |
null
{"bar":[0,1],"foo":[0,1,2,3,4]}
{"bar":[1],"foo":[1,4]}
{"bar":[1]}
{"bar":[0],"foo":[0,1,4]}
- name: del function with slice
args:
Expand Down
15 changes: 15 additions & 0 deletions func.go
Original file line number Diff line number Diff line change
Expand Up @@ -1271,6 +1271,21 @@ func updatePaths(v interface{}, path []interface{}, w interface{}, delpaths bool
if start >= end {
return uu, nil
}
if len(path) > 1 {
u, err := updatePaths(uu[start:end], path[1:], w, delpaths)
if err != nil {
return nil, err
}
switch us := u.(type) {
case []interface{}:
vs := make([]interface{}, len(uu))
copy(vs, uu)
copy(vs[start:end], us)
return vs, nil
default:
return nil, &expectedArrayError{u}
}
}
vs := make([]interface{}, len(uu))
copy(vs, uu)
for y := start; y < end; y++ {
Expand Down

0 comments on commit 6dc7234

Please sign in to comment.