Skip to content

Commit f654fbf

Browse files
committed
Fixed array subtraction update bug #2159
1 parent 5df7107 commit f654fbf

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

pkg/yqlib/operator_add_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ var addOperatorScenarios = []expressionScenario{
1414
"D0, P[1 a], (!!int)::3\n",
1515
},
1616
},
17+
{
18+
skipDoc: true,
19+
description: "add sequence creates a new sequence",
20+
expression: `["a"] as $f | {0:$f + ["b"], 1:$f}`,
21+
expected: []string{
22+
"D0, P[], (!!map)::0:\n - a\n - b\n1:\n - a\n",
23+
},
24+
},
1725
{
1826
skipDoc: true,
1927
document: `a: key`,

pkg/yqlib/operator_subtract.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func subtractOperator(d *dataTreeNavigator, context Context, expressionNode *Exp
2323
return crossFunction(d, context.ReadOnlyClone(), expressionNode, subtract, false)
2424
}
2525

26-
func subtractArray(lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
26+
func subtractArray(lhs *CandidateNode, rhs *CandidateNode) []*CandidateNode {
2727
newLHSArray := make([]*CandidateNode, 0)
2828

2929
for lindex := 0; lindex < len(lhs.Content); lindex = lindex + 1 {
@@ -37,9 +37,7 @@ func subtractArray(lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, erro
3737
newLHSArray = append(newLHSArray, lhs.Content[lindex])
3838
}
3939
}
40-
// removing children from LHS, parent hasn't changed
41-
lhs.Content = newLHSArray
42-
return lhs, nil
40+
return newLHSArray
4341
}
4442

4543
func subtract(_ *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
@@ -56,7 +54,7 @@ func subtract(_ *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *Ca
5654
if rhs.Kind != SequenceNode {
5755
return nil, fmt.Errorf("%v (%v) cannot be subtracted from %v", rhs.Tag, rhs.GetNicePath(), lhs.Tag)
5856
}
59-
return subtractArray(lhs, rhs)
57+
target.Content = subtractArray(lhs, rhs)
6058
case ScalarNode:
6159
if rhs.Kind != ScalarNode {
6260
return nil, fmt.Errorf("%v (%v) cannot be subtracted from %v", rhs.Tag, rhs.GetNicePath(), lhs.Tag)

pkg/yqlib/operator_subtract_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ var subtractOperatorScenarios = []expressionScenario{
1313
"D0, P[], (!!map)::{}\n",
1414
},
1515
},
16+
{
17+
skipDoc: true,
18+
description: "subtract sequence creates a new sequence",
19+
expression: `["a", "b"] as $f | {0:$f - ["a"], 1:$f}`,
20+
expected: []string{
21+
"D0, P[], (!!map)::0:\n - b\n1:\n - a\n - b\n",
22+
},
23+
},
1624
{
1725
description: "Array subtraction",
1826
expression: `[1,2] - [2,3]`,

0 commit comments

Comments
 (0)