Skip to content

Commit

Permalink
fix assignment operator against nested slicing
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Jun 6, 2020
1 parent cdc71f9 commit fc472c4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
11 changes: 10 additions & 1 deletion cli/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3210,7 +3210,9 @@
- name: assignment operator against array with slice
args:
- -c
- '.[2:4] = ([], [0], [4,3,2]), .[1:-1] = [3,2], .[2:0] = [.[]], .[10:] = [1], .[-20:-10] = [1]'
- '.[2:4] = ([], [0], [4,3,2]), .[1:-1] = [3,2], .[2:0] = [.[]], .[10:] = [1],
.[-20:-10] = [1], .[0:5] = [9], .[1:][1:] = [9], .[1:][:3] = [6,7,8],
.[:4][1:][:3] = [9], .[1:][1:-2] = [9], .[:4][-3:][:5] = [9], (null | .[:1][2:] = [9])'
input: '[1,2,3,4,5]'
expected: |
[1,2,5]
Expand All @@ -3220,6 +3222,13 @@
[1,2,1,2,3,4,5,3,4,5]
[1,2,3,4,5,1]
[1,1,2,3,4,5]
[9]
[1,2,9]
[1,6,7,8,5]
[1,9,5]
[1,2,9,4,5]
[1,9,5]
[9]
- name: assignment operator deeply
args:
Expand Down
21 changes: 10 additions & 11 deletions func.go
Original file line number Diff line number Diff line change
Expand Up @@ -1257,24 +1257,16 @@ func updatePaths(v interface{}, path []interface{}, w interface{}, delpaths bool
}
if x, ok := toInt(x["end"]); ok {
x := toIndex(uu, x)
if x < start {
if x == -1 {
end = len(uu)
} else if x < start {
end = start
} else {
end = x
}
} else {
end = len(uu)
}
if len(path) > 1 {
u, err := updatePaths(uu[start], path[1:], w, delpaths)
if err != nil {
return nil, err
}
vs := make([]interface{}, len(uu))
copy(vs, uu)
vs[start] = u
return vs, nil
}
if delpaths {
if start >= end {
return uu, nil
Expand All @@ -1286,6 +1278,13 @@ func updatePaths(v interface{}, path []interface{}, w interface{}, delpaths bool
}
return vs, nil
}
if len(path) > 1 {
u, err := updatePaths(uu[start:end], path[1:], w, delpaths)
if err != nil {
return nil, err
}
w = u
}
switch v := w.(type) {
case []interface{}:
vs := make([]interface{}, start+len(v)+len(uu)-end)
Expand Down

0 comments on commit fc472c4

Please sign in to comment.