Skip to content

Commit d58870b

Browse files
committed
Adding splat[] short hand to explode,collect,flatten,groupby,path,pivot,select and more
1 parent f1964de commit d58870b

14 files changed

+146
-13
lines changed

pkg/yqlib/lexer_participle.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ func flattenWithDepth() yqAction {
328328

329329
prefs := flattenPreferences{depth: depth}
330330
op := &Operation{OperationType: flattenOpType, Value: flattenOpType.Type, StringValue: value, Preferences: prefs}
331-
return &token{TokenType: operationToken, Operation: op}, nil
331+
return &token{TokenType: operationToken, Operation: op, CheckForPostTraverse: flattenOpType.CheckForPostTraverse}, nil
332332
}
333333
}
334334

pkg/yqlib/lexer_participle_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ var participleLexerScenarios = []participleLexerScenario{
513513
StringValue: "flatten(3)",
514514
Preferences: flattenPreferences{depth: 3},
515515
},
516+
CheckForPostTraverse: flattenOpType.CheckForPostTraverse,
516517
},
517518
},
518519
},
@@ -527,6 +528,7 @@ var participleLexerScenarios = []participleLexerScenario{
527528
StringValue: "flatten",
528529
Preferences: flattenPreferences{depth: -1},
529530
},
531+
CheckForPostTraverse: flattenOpType.CheckForPostTraverse,
530532
},
531533
},
532534
},

pkg/yqlib/operation.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ var toEntriesOpType = &operationType{Type: "TO_ENTRIES", NumArgs: 0, Precedence:
117117
var fromEntriesOpType = &operationType{Type: "FROM_ENTRIES", NumArgs: 0, Precedence: 50, Handler: fromEntriesOperator}
118118
var withEntriesOpType = &operationType{Type: "WITH_ENTRIES", NumArgs: 1, Precedence: 50, Handler: withEntriesOperator}
119119

120-
var withOpType = &operationType{Type: "WITH", NumArgs: 1, Precedence: 50, Handler: withOperator}
120+
var withOpType = &operationType{Type: "WITH", NumArgs: 1, Precedence: 52, Handler: withOperator, CheckForPostTraverse: true}
121121

122-
var splitDocumentOpType = &operationType{Type: "SPLIT_DOC", NumArgs: 0, Precedence: 50, Handler: splitDocumentOperator}
122+
var splitDocumentOpType = &operationType{Type: "SPLIT_DOC", NumArgs: 0, Precedence: 52, Handler: splitDocumentOperator, CheckForPostTraverse: true}
123123
var getVariableOpType = &operationType{Type: "GET_VARIABLE", NumArgs: 0, Precedence: 55, Handler: getVariableOperator}
124124
var getStyleOpType = &operationType{Type: "GET_STYLE", NumArgs: 0, Precedence: 50, Handler: getStyleOperator}
125125
var getTagOpType = &operationType{Type: "GET_TAG", NumArgs: 0, Precedence: 50, Handler: getTagOperator}
@@ -138,10 +138,10 @@ var getFileIndexOpType = &operationType{Type: "GET_FILE_INDEX", NumArgs: 0, Prec
138138

139139
var getPathOpType = &operationType{Type: "GET_PATH", NumArgs: 0, Precedence: 52, Handler: getPathOperator, CheckForPostTraverse: true}
140140
var setPathOpType = &operationType{Type: "SET_PATH", NumArgs: 1, Precedence: 50, Handler: setPathOperator}
141-
var delPathsOpType = &operationType{Type: "DEL_PATHS", NumArgs: 1, Precedence: 50, Handler: delPathsOperator}
141+
var delPathsOpType = &operationType{Type: "DEL_PATHS", NumArgs: 1, Precedence: 52, Handler: delPathsOperator, CheckForPostTraverse: true}
142142

143-
var explodeOpType = &operationType{Type: "EXPLODE", NumArgs: 1, Precedence: 50, Handler: explodeOperator}
144-
var sortByOpType = &operationType{Type: "SORT_BY", NumArgs: 1, Precedence: 52, Handler: sortByOperator}
143+
var explodeOpType = &operationType{Type: "EXPLODE", NumArgs: 1, Precedence: 52, Handler: explodeOperator, CheckForPostTraverse: true}
144+
var sortByOpType = &operationType{Type: "SORT_BY", NumArgs: 1, Precedence: 52, Handler: sortByOperator, CheckForPostTraverse: true}
145145
var reverseOpType = &operationType{Type: "REVERSE", NumArgs: 0, Precedence: 52, Handler: reverseOperator, CheckForPostTraverse: true}
146146
var sortOpType = &operationType{Type: "SORT", NumArgs: 0, Precedence: 52, Handler: sortOperator, CheckForPostTraverse: true}
147147
var shuffleOpType = &operationType{Type: "SHUFFLE", NumArgs: 0, Precedence: 52, Handler: shuffleOperator, CheckForPostTraverse: true}
@@ -153,7 +153,7 @@ var subStringOpType = &operationType{Type: "SUBSTR", NumArgs: 1, Precedence: 50,
153153
var matchOpType = &operationType{Type: "MATCH", NumArgs: 1, Precedence: 50, Handler: matchOperator}
154154
var captureOpType = &operationType{Type: "CAPTURE", NumArgs: 1, Precedence: 50, Handler: captureOperator}
155155
var testOpType = &operationType{Type: "TEST", NumArgs: 1, Precedence: 50, Handler: testOperator}
156-
var splitStringOpType = &operationType{Type: "SPLIT", NumArgs: 1, Precedence: 50, Handler: splitStringOperator}
156+
var splitStringOpType = &operationType{Type: "SPLIT", NumArgs: 1, Precedence: 52, Handler: splitStringOperator, CheckForPostTraverse: true}
157157
var changeCaseOpType = &operationType{Type: "CHANGE_CASE", NumArgs: 0, Precedence: 50, Handler: changeCaseOperator}
158158
var trimOpType = &operationType{Type: "TRIM", NumArgs: 0, Precedence: 50, Handler: trimSpaceOperator}
159159
var toStringOpType = &operationType{Type: "TO_STRING", NumArgs: 0, Precedence: 50, Handler: toStringOperator}
@@ -185,15 +185,15 @@ var envsubstOpType = &operationType{Type: "ENVSUBST", NumArgs: 0, Precedence: 50
185185

186186
var recursiveDescentOpType = &operationType{Type: "RECURSIVE_DESCENT", NumArgs: 0, Precedence: 50, Handler: recursiveDescentOperator}
187187

188-
var selectOpType = &operationType{Type: "SELECT", NumArgs: 1, Precedence: 50, Handler: selectOperator}
188+
var selectOpType = &operationType{Type: "SELECT", NumArgs: 1, Precedence: 52, Handler: selectOperator, CheckForPostTraverse: true}
189189
var hasOpType = &operationType{Type: "HAS", NumArgs: 1, Precedence: 50, Handler: hasOperator}
190-
var uniqueOpType = &operationType{Type: "UNIQUE", NumArgs: 0, Precedence: 50, Handler: unique}
191-
var uniqueByOpType = &operationType{Type: "UNIQUE_BY", NumArgs: 1, Precedence: 50, Handler: uniqueBy}
192-
var groupByOpType = &operationType{Type: "GROUP_BY", NumArgs: 1, Precedence: 50, Handler: groupBy}
193-
var flattenOpType = &operationType{Type: "FLATTEN_BY", NumArgs: 0, Precedence: 50, Handler: flattenOp}
190+
var uniqueOpType = &operationType{Type: "UNIQUE", NumArgs: 0, Precedence: 52, Handler: unique, CheckForPostTraverse: true}
191+
var uniqueByOpType = &operationType{Type: "UNIQUE_BY", NumArgs: 1, Precedence: 52, Handler: uniqueBy, CheckForPostTraverse: true}
192+
var groupByOpType = &operationType{Type: "GROUP_BY", NumArgs: 1, Precedence: 52, Handler: groupBy, CheckForPostTraverse: true}
193+
var flattenOpType = &operationType{Type: "FLATTEN_BY", NumArgs: 0, Precedence: 52, Handler: flattenOp, CheckForPostTraverse: true}
194194
var deleteChildOpType = &operationType{Type: "DELETE", NumArgs: 1, Precedence: 40, Handler: deleteChildOperator}
195195

196-
var pivotOpType = &operationType{Type: "PIVOT", NumArgs: 0, Precedence: 50, Handler: pivotOperator}
196+
var pivotOpType = &operationType{Type: "PIVOT", NumArgs: 0, Precedence: 52, Handler: pivotOperator, CheckForPostTraverse: true}
197197

198198
// debugging purposes only
199199
func (p *Operation) toString() string {

pkg/yqlib/operator_anchors_aliases_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,16 @@ var anchorOperatorScenarios = []expressionScenario{
188188
"D0, P[b], (!!str)::cat\n",
189189
},
190190
},
191+
{
192+
description: "Explode splat",
193+
skipDoc: true,
194+
document: `{a: &a cat, b: *a}`,
195+
expression: `explode(.)[]`,
196+
expected: []string{
197+
"D0, P[a], (!!str)::cat\n",
198+
"D0, P[b], (!!str)::cat\n",
199+
},
200+
},
191201
{
192202
description: "Explode alias and anchor - check original parent",
193203
skipDoc: true,

pkg/yqlib/operator_collect_object_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ var collectObjectOperatorScenarios = []expressionScenario{
2828
"D0, P[], (!!map)::dog: great\n",
2929
},
3030
},
31+
{
32+
description: "collect splat",
33+
skipDoc: true,
34+
document: `[{name: cat}, {name: dog}]`,
35+
expression: `.[] | {.name: "great"}[]`,
36+
expected: []string{
37+
"D0, P[cat], (!!str)::great\n",
38+
"D0, P[dog], (!!str)::great\n",
39+
},
40+
},
3141
{
3242
skipDoc: true,
3343
expression: `({} + {}) | (.b = 3)`,

pkg/yqlib/operator_flatten_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ var flattenOperatorScenarios = []expressionScenario{
1414
"D0, P[], (!!seq)::[1, 2, 3]\n",
1515
},
1616
},
17+
{
18+
description: "Flatten splat",
19+
skipDoc: true,
20+
document: `[1, [2], [[3]]]`,
21+
expression: `flatten[]`,
22+
expected: []string{
23+
"D0, P[0], (!!int)::1\n",
24+
"D0, P[0], (!!int)::2\n",
25+
"D0, P[0], (!!int)::3\n",
26+
},
27+
},
1728
{
1829
description: "Flatten with depth of one",
1930
document: `[1, [2], [[3]]]`,
@@ -22,6 +33,17 @@ var flattenOperatorScenarios = []expressionScenario{
2233
"D0, P[], (!!seq)::[1, 2, [3]]\n",
2334
},
2435
},
36+
{
37+
description: "Flatten with depth and splat",
38+
skipDoc: true,
39+
document: `[1, [2], [[3]]]`,
40+
expression: `flatten(1)[]`,
41+
expected: []string{
42+
"D0, P[0], (!!int)::1\n",
43+
"D0, P[0], (!!int)::2\n",
44+
"D0, P[0], (!!seq)::[3]\n",
45+
},
46+
},
2547
{
2648
description: "Flatten empty array",
2749
document: `[[]]`,

pkg/yqlib/operator_group_by_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ var groupByOperatorScenarios = []expressionScenario{
1313
"D0, P[], (!!seq)::- - {foo: 1, bar: 10}\n - {foo: 1, bar: 1}\n- - {foo: 3, bar: 100}\n",
1414
},
1515
},
16+
{
17+
description: "Group splat",
18+
skipDoc: true,
19+
document: `[{foo: 1, bar: 10}, {foo: 3, bar: 100}, {foo: 1, bar: 1}]`,
20+
expression: `group_by(.foo)[]`,
21+
expected: []string{
22+
"D0, P[0], (!!seq)::- {foo: 1, bar: 10}\n- {foo: 1, bar: 1}\n",
23+
"D0, P[1], (!!seq)::- {foo: 3, bar: 100}\n",
24+
},
25+
},
1626
{
1727
description: "Group by field, with nulls",
1828
document: `[{cat: dog}, {foo: 1, bar: 10}, {foo: 3, bar: 100}, {no: foo for you}, {foo: 1, bar: 1}]`,

pkg/yqlib/operator_path_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,15 @@ var pathOperatorScenarios = []expressionScenario{
139139
"D0, P[], (!!map)::a: [frog]\n",
140140
},
141141
},
142+
{
143+
description: "Delete splat",
144+
skipDoc: true,
145+
document: `a: [cat, frog]`,
146+
expression: `delpaths([["a", 0]])[]`,
147+
expected: []string{
148+
"D0, P[a], (!!seq)::[frog]\n",
149+
},
150+
},
142151
{
143152
description: "Delete - wrong parameter",
144153
subdescription: "delpaths does not work with a single path array",

pkg/yqlib/operator_pivot_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ var pivotOperatorScenarios = []expressionScenario{
1111
"D0, P[], ()::- - foo\n - sis\n- - bar\n - boom\n- - baz\n - bah\n",
1212
},
1313
},
14+
{
15+
description: "Pivot splat",
16+
skipDoc: true,
17+
document: "[[foo, bar], [sis, boom]]\n",
18+
expression: `pivot[]`,
19+
expected: []string{
20+
"D0, P[0], ()::- foo\n- sis\n",
21+
"D0, P[1], ()::- bar\n- boom\n",
22+
},
23+
},
1424
{
1525
description: "Pivot sequence of heterogeneous sequences",
1626
subdescription: `Missing values are "padded" to null.`,

pkg/yqlib/operator_select_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ var selectOperatorScenarios = []expressionScenario{
7575
"D0, P[], (!!map)::b: world\n",
7676
},
7777
},
78+
{
79+
description: "select splat",
80+
skipDoc: true,
81+
document: "a: hello",
82+
document2: "b: world",
83+
expression: `select(.a == "hello" or .b == "world")[]`,
84+
expected: []string{
85+
"D0, P[a], (!!str)::hello\n",
86+
"D0, P[b], (!!str)::world\n",
87+
},
88+
},
7889
{
7990
description: "select does not update the map",
8091
skipDoc: true,

0 commit comments

Comments
 (0)