Skip to content

Commit

Permalink
Fixed array subtraction update bug #2159
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Sep 29, 2024
1 parent 5df7107 commit f654fbf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
8 changes: 8 additions & 0 deletions pkg/yqlib/operator_add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ var addOperatorScenarios = []expressionScenario{
"D0, P[1 a], (!!int)::3\n",
},
},
{
skipDoc: true,
description: "add sequence creates a new sequence",
expression: `["a"] as $f | {0:$f + ["b"], 1:$f}`,
expected: []string{
"D0, P[], (!!map)::0:\n - a\n - b\n1:\n - a\n",
},
},
{
skipDoc: true,
document: `a: key`,
Expand Down
8 changes: 3 additions & 5 deletions pkg/yqlib/operator_subtract.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func subtractOperator(d *dataTreeNavigator, context Context, expressionNode *Exp
return crossFunction(d, context.ReadOnlyClone(), expressionNode, subtract, false)
}

func subtractArray(lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
func subtractArray(lhs *CandidateNode, rhs *CandidateNode) []*CandidateNode {
newLHSArray := make([]*CandidateNode, 0)

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

func subtract(_ *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
Expand All @@ -56,7 +54,7 @@ func subtract(_ *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *Ca
if rhs.Kind != SequenceNode {
return nil, fmt.Errorf("%v (%v) cannot be subtracted from %v", rhs.Tag, rhs.GetNicePath(), lhs.Tag)
}
return subtractArray(lhs, rhs)
target.Content = subtractArray(lhs, rhs)
case ScalarNode:
if rhs.Kind != ScalarNode {
return nil, fmt.Errorf("%v (%v) cannot be subtracted from %v", rhs.Tag, rhs.GetNicePath(), lhs.Tag)
Expand Down
8 changes: 8 additions & 0 deletions pkg/yqlib/operator_subtract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ var subtractOperatorScenarios = []expressionScenario{
"D0, P[], (!!map)::{}\n",
},
},
{
skipDoc: true,
description: "subtract sequence creates a new sequence",
expression: `["a", "b"] as $f | {0:$f - ["a"], 1:$f}`,
expected: []string{
"D0, P[], (!!map)::0:\n - b\n1:\n - a\n - b\n",
},
},
{
description: "Array subtraction",
expression: `[1,2] - [2,3]`,
Expand Down

0 comments on commit f654fbf

Please sign in to comment.