Skip to content

Commit 62d167c

Browse files
authored
Variable loop - Fixes #1566 (#1577)
* Variable loop wip * Variable loop wip * Variable loop wip * Variable loop wip * Fixed variable operator to work like jq
1 parent cb27444 commit 62d167c

12 files changed

+112
-31
lines changed

pkg/yqlib/lib.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ var subtractAssignOpType = &operationType{Type: "SUBTRACT_ASSIGN", NumArgs: 2, P
5353

5454
var assignAttributesOpType = &operationType{Type: "ASSIGN_ATTRIBUTES", NumArgs: 2, Precedence: 40, Handler: assignAttributesOperator}
5555
var assignStyleOpType = &operationType{Type: "ASSIGN_STYLE", NumArgs: 2, Precedence: 40, Handler: assignStyleOperator}
56-
var assignVariableOpType = &operationType{Type: "ASSIGN_VARIABLE", NumArgs: 2, Precedence: 40, Handler: assignVariableOperator}
56+
var assignVariableOpType = &operationType{Type: "ASSIGN_VARIABLE", NumArgs: 2, Precedence: 40, Handler: useWithPipe}
5757
var assignTagOpType = &operationType{Type: "ASSIGN_TAG", NumArgs: 2, Precedence: 40, Handler: assignTagOperator}
5858
var assignCommentOpType = &operationType{Type: "ASSIGN_COMMENT", NumArgs: 2, Precedence: 40, Handler: assignCommentsOperator}
5959
var assignAnchorOpType = &operationType{Type: "ASSIGN_ANCHOR", NumArgs: 2, Precedence: 40, Handler: assignAnchorOperator}

pkg/yqlib/operator_add_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ var addOperatorScenarios = []expressionScenario{
3434
{
3535
skipDoc: true,
3636
document: `{}`,
37-
expression: "(.a + .b) as $x",
37+
expression: "(.a + .b) as $x | .",
3838
expected: []string{
3939
"D0, P[], (doc)::{}\n",
4040
},

pkg/yqlib/operator_alternative_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var alternativeOperatorScenarios = []expressionScenario{
1616
},
1717
{
1818
skipDoc: true,
19-
expression: `(.b // "hello") as $x`,
19+
expression: `(.b // "hello") as $x | .`,
2020
document: `a: bridge`,
2121
expected: []string{
2222
"D0, P[], (doc)::a: bridge\n",

pkg/yqlib/operator_booleans_test.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,25 @@ var booleanOperatorScenarios = []expressionScenario{
102102
{
103103
skipDoc: true,
104104
document: `[{pet: cat}]`,
105-
expression: `any_c(.name == "harry") as $c`,
105+
expression: `any_c(.name == "harry") as $c | .`,
106106
expected: []string{
107107
"D0, P[], (doc)::[{pet: cat}]\n",
108108
},
109109
},
110110
{
111111
skipDoc: true,
112112
document: `[{pet: cat}]`,
113-
expression: `all_c(.name == "harry") as $c`,
113+
expression: `any_c(.name == "harry") as $c | $c`,
114114
expected: []string{
115-
"D0, P[], (doc)::[{pet: cat}]\n",
115+
"D0, P[], (!!bool)::false\n",
116+
},
117+
},
118+
{
119+
skipDoc: true,
120+
document: `[{pet: cat}]`,
121+
expression: `all_c(.name == "harry") as $c | $c`,
122+
expected: []string{
123+
"D0, P[], (!!bool)::false\n",
116124
},
117125
},
118126
{
@@ -185,15 +193,15 @@ var booleanOperatorScenarios = []expressionScenario{
185193
{
186194
skipDoc: true,
187195
document: `{}`,
188-
expression: `(.a.b or .c) as $x`,
196+
expression: `(.a.b or .c) as $x | .`,
189197
expected: []string{
190198
"D0, P[], (doc)::{}\n",
191199
},
192200
},
193201
{
194202
skipDoc: true,
195203
document: `{}`,
196-
expression: `(.a.b and .c) as $x`,
204+
expression: `(.a.b and .c) as $x | .`,
197205
expected: []string{
198206
"D0, P[], (doc)::{}\n",
199207
},

pkg/yqlib/operator_equals_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ var equalsOperatorScenarios = []expressionScenario{
4747
{
4848
skipDoc: true,
4949
document: "{}",
50-
expression: "(.a == .b) as $x",
50+
expression: "(.a == .b) as $x | .",
5151
expected: []string{
5252
"D0, P[], (doc)::{}\n",
5353
},
@@ -63,7 +63,7 @@ var equalsOperatorScenarios = []expressionScenario{
6363
{
6464
skipDoc: true,
6565
document: "{}",
66-
expression: "(.a != .b) as $x",
66+
expression: "(.a != .b) as $x | .",
6767
expected: []string{
6868
"D0, P[], (doc)::{}\n",
6969
},

pkg/yqlib/operator_has_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var hasOperatorScenarios = []expressionScenario{
1616
{
1717
skipDoc: true,
1818
document: `a: hello`,
19-
expression: `has(.b) as $c`,
19+
expression: `has(.b) as $c | .`,
2020
expected: []string{
2121
"D0, P[], (doc)::a: hello\n",
2222
},

pkg/yqlib/operator_pipe.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package yqlib
22

33
func pipeOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
44

5-
//lhs may update the variable context, we should pass that into the RHS
6-
// BUT we still return the original context back (see jq)
7-
// https://stedolan.github.io/jq/manual/#Variable/SymbolicBindingOperator:...as$identifier|...
5+
if expressionNode.LHS.Operation.OperationType == assignVariableOpType {
6+
return variableLoop(d, context, expressionNode)
7+
}
88

99
lhs, err := d.GetMatchingNodes(context, expressionNode.LHS)
1010
if err != nil {

pkg/yqlib/operator_subtract_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var subtractOperatorScenarios = []expressionScenario{
88
{
99
skipDoc: true,
1010
document: `{}`,
11-
expression: "(.a - .b) as $x",
11+
expression: "(.a - .b) as $x | .",
1212
expected: []string{
1313
"D0, P[], (doc)::{}\n",
1414
},

pkg/yqlib/operator_traverse_path_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ var traversePathOperatorScenarios = []expressionScenario{
151151
{
152152
skipDoc: true,
153153
document: `c: dog`,
154-
expression: `.[.a.b] as $x`,
154+
expression: `.[.a.b] as $x | .`,
155155
expected: []string{
156156
"D0, P[], (doc)::c: dog\n",
157157
},

pkg/yqlib/operator_union_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var unionOperatorScenarios = []expressionScenario{
88
{
99
skipDoc: true,
1010
document: "{}",
11-
expression: `(.a, .b.c) as $x`,
11+
expression: `(.a, .b.c) as $x | .`,
1212
expected: []string{
1313
"D0, P[], (doc)::{}\n",
1414
},

0 commit comments

Comments
 (0)