Skip to content

Commit 176873e

Browse files
committed
Refining add op
1 parent fd7e750 commit 176873e

File tree

4 files changed

+93
-3
lines changed

4 files changed

+93
-3
lines changed

pkg/yqlib/operator_add.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ func toNodes(candidate *CandidateNode, lhs *CandidateNode) []*CandidateNode {
3838

3939
func addOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
4040
log.Debugf("Add operator")
41-
// if we don't have any matching nodes, but we were piped into (have a parent exp?) then we
42-
// shouldn't calcWhenEmpt
43-
calcWhenEmpty := expressionNode.Parent == nil
41+
// only calculate when empty IF we are the root expression; OR
42+
// calcWhenEmpty := expressionNode.Parent == nil || expressionNode.Parent.LHS == expressionNode
43+
calcWhenEmpty := context.MatchingNodes.Len() > 0
4444

4545
return crossFunction(d, context.ReadOnlyClone(), expressionNode, add, calcWhenEmpty)
4646
}

pkg/yqlib/operator_add_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,62 @@ var addOperatorScenarios = []expressionScenario{
5050
expression: `select(.) | "cat" + .`,
5151
expected: []string{},
5252
},
53+
{
54+
skipDoc: true,
55+
document: `[]`,
56+
expression: `.[] | (.a + "|" + .b)`,
57+
expected: []string{},
58+
},
59+
{
60+
skipDoc: true,
61+
document: `[]`,
62+
expression: `.[] | (.a + "|")`,
63+
expected: []string{},
64+
},
65+
{
66+
skipDoc: true,
67+
document: `[]`,
68+
expression: `.[] | ("|" + .a)`,
69+
expected: []string{},
70+
},
71+
{
72+
skipDoc: true,
73+
document: `resources: [foo, bar, baz]`,
74+
expression: `.missing + .resources | .[]`,
75+
expected: []string{
76+
"D0, P[resources 0], (!!str)::foo\n",
77+
"D0, P[resources 1], (!!str)::bar\n",
78+
"D0, P[resources 2], (!!str)::baz\n",
79+
},
80+
},
81+
{
82+
skipDoc: true,
83+
document: `resources: [foo, bar, baz]`,
84+
expression: `. | .missing + .resources | .[]`,
85+
expected: []string{
86+
"D0, P[resources 0], (!!str)::foo\n",
87+
"D0, P[resources 1], (!!str)::bar\n",
88+
"D0, P[resources 2], (!!str)::baz\n",
89+
},
90+
},
91+
{
92+
skipDoc: true,
93+
document: `resources: [foo, bar, baz]`,
94+
expression: `. | .missing + .resources`,
95+
expected: []string{
96+
"D0, P[resources], (!!seq)::[foo, bar, baz]\n",
97+
},
98+
},
99+
{
100+
skipDoc: true,
101+
document: `resources: [foo, bar, baz]`,
102+
expression: `. | .missing + .resources | .[]`,
103+
expected: []string{
104+
"D0, P[resources 0], (!!str)::foo\n",
105+
"D0, P[resources 1], (!!str)::bar\n",
106+
"D0, P[resources 2], (!!str)::baz\n",
107+
},
108+
},
53109
{
54110
skipDoc: true,
55111
document: `[{a: foo, b: bar}, {a: 1, b: 2}]`,

scripts/compare-jq.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
exp=$1
5+
file=$2
6+
7+
if [ "$2" == "" ]; then
8+
echo "yq"
9+
./yq -oj -n "$1"
10+
echo "jq"
11+
jq -n "$1"
12+
13+
else
14+
15+
echo "yq"
16+
./yq -oj "$1" $2
17+
echo "jq"
18+
./yq $2 -oj | jq "$1"
19+
fi

scripts/compare-versions-output.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env bash
2+
3+
test_data='
4+
- foo: false
5+
'
6+
7+
for version in 4.45.1 4.45.2 4.45.3; do
8+
for command in '.[] | (select(.foo) | {"foo": .foo} // {})' '.[] | (select(.foo) | {.foo} // {})'; do
9+
echo ${version} "${command}"
10+
echo -------
11+
echo "${test_data}" | podman run -i --rm mikefarah/yq:${version} -o json "${command}"
12+
echo -------
13+
echo
14+
done
15+
done

0 commit comments

Comments
 (0)