mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-01 09:51:40 +00:00
Refining add op
This commit is contained in:
parent
fd7e750040
commit
176873e93a
@ -38,9 +38,9 @@ func toNodes(candidate *CandidateNode, lhs *CandidateNode) []*CandidateNode {
|
||||
|
||||
func addOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
||||
log.Debugf("Add operator")
|
||||
// if we don't have any matching nodes, but we were piped into (have a parent exp?) then we
|
||||
// shouldn't calcWhenEmpt
|
||||
calcWhenEmpty := expressionNode.Parent == nil
|
||||
// only calculate when empty IF we are the root expression; OR
|
||||
// calcWhenEmpty := expressionNode.Parent == nil || expressionNode.Parent.LHS == expressionNode
|
||||
calcWhenEmpty := context.MatchingNodes.Len() > 0
|
||||
|
||||
return crossFunction(d, context.ReadOnlyClone(), expressionNode, add, calcWhenEmpty)
|
||||
}
|
||||
|
||||
@ -50,6 +50,62 @@ var addOperatorScenarios = []expressionScenario{
|
||||
expression: `select(.) | "cat" + .`,
|
||||
expected: []string{},
|
||||
},
|
||||
{
|
||||
skipDoc: true,
|
||||
document: `[]`,
|
||||
expression: `.[] | (.a + "|" + .b)`,
|
||||
expected: []string{},
|
||||
},
|
||||
{
|
||||
skipDoc: true,
|
||||
document: `[]`,
|
||||
expression: `.[] | (.a + "|")`,
|
||||
expected: []string{},
|
||||
},
|
||||
{
|
||||
skipDoc: true,
|
||||
document: `[]`,
|
||||
expression: `.[] | ("|" + .a)`,
|
||||
expected: []string{},
|
||||
},
|
||||
{
|
||||
skipDoc: true,
|
||||
document: `resources: [foo, bar, baz]`,
|
||||
expression: `.missing + .resources | .[]`,
|
||||
expected: []string{
|
||||
"D0, P[resources 0], (!!str)::foo\n",
|
||||
"D0, P[resources 1], (!!str)::bar\n",
|
||||
"D0, P[resources 2], (!!str)::baz\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
skipDoc: true,
|
||||
document: `resources: [foo, bar, baz]`,
|
||||
expression: `. | .missing + .resources | .[]`,
|
||||
expected: []string{
|
||||
"D0, P[resources 0], (!!str)::foo\n",
|
||||
"D0, P[resources 1], (!!str)::bar\n",
|
||||
"D0, P[resources 2], (!!str)::baz\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
skipDoc: true,
|
||||
document: `resources: [foo, bar, baz]`,
|
||||
expression: `. | .missing + .resources`,
|
||||
expected: []string{
|
||||
"D0, P[resources], (!!seq)::[foo, bar, baz]\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
skipDoc: true,
|
||||
document: `resources: [foo, bar, baz]`,
|
||||
expression: `. | .missing + .resources | .[]`,
|
||||
expected: []string{
|
||||
"D0, P[resources 0], (!!str)::foo\n",
|
||||
"D0, P[resources 1], (!!str)::bar\n",
|
||||
"D0, P[resources 2], (!!str)::baz\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
skipDoc: true,
|
||||
document: `[{a: foo, b: bar}, {a: 1, b: 2}]`,
|
||||
|
||||
19
scripts/compare-jq.sh
Executable file
19
scripts/compare-jq.sh
Executable file
@ -0,0 +1,19 @@
|
||||
#!/usr/bin/env bash
|
||||
set -e
|
||||
|
||||
exp=$1
|
||||
file=$2
|
||||
|
||||
if [ "$2" == "" ]; then
|
||||
echo "yq"
|
||||
./yq -oj -n "$1"
|
||||
echo "jq"
|
||||
jq -n "$1"
|
||||
|
||||
else
|
||||
|
||||
echo "yq"
|
||||
./yq -oj "$1" $2
|
||||
echo "jq"
|
||||
./yq $2 -oj | jq "$1"
|
||||
fi
|
||||
15
scripts/compare-versions-output.sh
Normal file
15
scripts/compare-versions-output.sh
Normal file
@ -0,0 +1,15 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
test_data='
|
||||
- foo: false
|
||||
'
|
||||
|
||||
for version in 4.45.1 4.45.2 4.45.3; do
|
||||
for command in '.[] | (select(.foo) | {"foo": .foo} // {})' '.[] | (select(.foo) | {.foo} // {})'; do
|
||||
echo ${version} "${command}"
|
||||
echo -------
|
||||
echo "${test_data}" | podman run -i --rm mikefarah/yq:${version} -o json "${command}"
|
||||
echo -------
|
||||
echo
|
||||
done
|
||||
done
|
||||
Loading…
Reference in New Issue
Block a user