Skip to content

Commit

Permalink
Adding splat[] short hand to collect,eval,map,omit,path,pick
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Jun 16, 2024
1 parent 45eda8d commit f1964de
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 8 deletions.
2 changes: 2 additions & 0 deletions pkg/yqlib/lexer_participle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ var participleLexerScenarios = []participleLexerScenario{
StringValue: "map_values",
Preferences: nil,
},
CheckForPostTraverse: mapValuesOpType.CheckForPostTraverse,
},
},
},
Expand All @@ -497,6 +498,7 @@ var participleLexerScenarios = []participleLexerScenario{
StringValue: "mapvalues",
Preferences: nil,
},
CheckForPostTraverse: mapValuesOpType.CheckForPostTraverse,
},
},
},
Expand Down
17 changes: 9 additions & 8 deletions pkg/yqlib/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,17 @@ var lengthOpType = &operationType{Type: "LENGTH", NumArgs: 0, Precedence: 50, Ha
var lineOpType = &operationType{Type: "LINE", NumArgs: 0, Precedence: 50, Handler: lineOperator}
var columnOpType = &operationType{Type: "LINE", NumArgs: 0, Precedence: 50, Handler: columnOperator}

// Use this expression to create alias/syntactic sugar expressions (in lexer_participle).
var expressionOpType = &operationType{Type: "EXP", NumArgs: 0, Precedence: 50, Handler: expressionOperator}

var collectOpType = &operationType{Type: "COLLECT", NumArgs: 1, Precedence: 50, Handler: collectOperator}
var mapOpType = &operationType{Type: "MAP", NumArgs: 1, Precedence: 50, Handler: mapOperator}
var mapOpType = &operationType{Type: "MAP", NumArgs: 1, Precedence: 52, Handler: mapOperator, CheckForPostTraverse: true}
var filterOpType = &operationType{Type: "FILTER", NumArgs: 1, Precedence: 52, Handler: filterOperator, CheckForPostTraverse: true}
var errorOpType = &operationType{Type: "ERROR", NumArgs: 1, Precedence: 50, Handler: errorOperator}
var pickOpType = &operationType{Type: "PICK", NumArgs: 1, Precedence: 50, Handler: pickOperator}
var omitOpType = &operationType{Type: "OMIT", NumArgs: 1, Precedence: 50, Handler: omitOperator}
var evalOpType = &operationType{Type: "EVAL", NumArgs: 1, Precedence: 50, Handler: evalOperator}
var mapValuesOpType = &operationType{Type: "MAP_VALUES", NumArgs: 1, Precedence: 50, Handler: mapValuesOperator}
var pickOpType = &operationType{Type: "PICK", NumArgs: 1, Precedence: 52, Handler: pickOperator, CheckForPostTraverse: true}
var omitOpType = &operationType{Type: "OMIT", NumArgs: 1, Precedence: 52, Handler: omitOperator, CheckForPostTraverse: true}
var evalOpType = &operationType{Type: "EVAL", NumArgs: 1, Precedence: 52, Handler: evalOperator, CheckForPostTraverse: true}
var mapValuesOpType = &operationType{Type: "MAP_VALUES", NumArgs: 1, Precedence: 52, Handler: mapValuesOperator, CheckForPostTraverse: true}

var formatDateTimeOpType = &operationType{Type: "FORMAT_DATE_TIME", NumArgs: 1, Precedence: 50, Handler: formatDateTime}
var withDtFormatOpType = &operationType{Type: "WITH_DATE_TIME_FORMAT", NumArgs: 1, Precedence: 50, Handler: withDateTimeFormat}
Expand Down Expand Up @@ -135,12 +136,12 @@ var getDocumentIndexOpType = &operationType{Type: "GET_DOCUMENT_INDEX", NumArgs:
var getFilenameOpType = &operationType{Type: "GET_FILENAME", NumArgs: 0, Precedence: 50, Handler: getFilenameOperator}
var getFileIndexOpType = &operationType{Type: "GET_FILE_INDEX", NumArgs: 0, Precedence: 50, Handler: getFileIndexOperator}

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

var explodeOpType = &operationType{Type: "EXPLODE", NumArgs: 1, Precedence: 50, Handler: explodeOperator}
var sortByOpType = &operationType{Type: "SORT_BY", NumArgs: 1, Precedence: 52, Handler: sortByOperator, CheckForPostTraverse: true}
var sortByOpType = &operationType{Type: "SORT_BY", NumArgs: 1, Precedence: 52, Handler: sortByOperator}
var reverseOpType = &operationType{Type: "REVERSE", NumArgs: 0, Precedence: 52, Handler: reverseOperator, CheckForPostTraverse: true}
var sortOpType = &operationType{Type: "SORT", NumArgs: 0, Precedence: 52, Handler: sortOperator, CheckForPostTraverse: true}
var shuffleOpType = &operationType{Type: "SHUFFLE", NumArgs: 0, Precedence: 52, Handler: shuffleOperator, CheckForPostTraverse: true}
Expand All @@ -158,7 +159,7 @@ var trimOpType = &operationType{Type: "TRIM", NumArgs: 0, Precedence: 50, Handle
var toStringOpType = &operationType{Type: "TO_STRING", NumArgs: 0, Precedence: 50, Handler: toStringOperator}
var stringInterpolationOpType = &operationType{Type: "STRING_INT", NumArgs: 0, Precedence: 50, Handler: stringInterpolationOperator, ToString: valueToStringFunc}

var loadOpType = &operationType{Type: "LOAD", NumArgs: 1, Precedence: 52, Handler: loadOperator}
var loadOpType = &operationType{Type: "LOAD", NumArgs: 1, Precedence: 52, Handler: loadOperator, CheckForPostTraverse: true}
var loadStringOpType = &operationType{Type: "LOAD_STRING", NumArgs: 1, Precedence: 52, Handler: loadStringOperator}

var keysOpType = &operationType{Type: "KEYS", NumArgs: 0, Precedence: 52, Handler: keysOperator, CheckForPostTraverse: true}
Expand Down
8 changes: 8 additions & 0 deletions pkg/yqlib/operator_collect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ var collectOperatorScenarios = []expressionScenario{
"D0, P[], (!!seq)::- 1\n- 2\n- 3\n",
},
},
{
skipDoc: true,
expression: `[1,2][]`,
expected: []string{
"D0, P[0], (!!int)::1\n",
"D0, P[1], (!!int)::2\n",
},
},
{
document: `a: {b: [1,2,3]}`,
expression: `[.a.b.[]]`,
Expand Down
9 changes: 9 additions & 0 deletions pkg/yqlib/operator_eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ var evalOperatorScenarios = []expressionScenario{
"D0, P[a b 1], (!!map)::{name: cat}\n",
},
},
{
description: "eval splat",
skipDoc: true,
document: `{pathExp: '.a.b[] | select(.name == "cat")', a: {b: [{name: dog}, {name: cat}]}}`,
expression: `eval(.pathExp)[]`,
expected: []string{
"D0, P[a b 1 name], (!!str)::cat\n",
},
},
{
description: "Dynamically update a path from an environment variable",
subdescription: "The env variable can be any valid yq expression.",
Expand Down
19 changes: 19 additions & 0 deletions pkg/yqlib/operator_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ var mapOperatorScenarios = []expressionScenario{
"D0, P[], (!!seq)::[6, 7, 8]\n",
},
},
{
skipDoc: true,
document: `[1,2]`,
expression: `map(. + 1)[]`,
expected: []string{
"D0, P[0], (!!int)::2\n",
"D0, P[1], (!!int)::3\n",
},
},
{
description: "Map array",
document: `[1,2,3]`,
Expand All @@ -33,6 +42,16 @@ var mapOperatorScenarios = []expressionScenario{
"D0, P[], (!!map)::{x: 11, y: 21, z: 31}\n",
},
},
{
description: "map values splat",
skipDoc: true,
document: `{a: 1, b: 2}`,
expression: `map_values(. + 1)[]`,
expected: []string{
"D0, P[a], (!!int)::2\n",
"D0, P[b], (!!int)::3\n",
},
},
{
description: "Map object values",
document: `{a: 1, b: 2, c: 3}`,
Expand Down
10 changes: 10 additions & 0 deletions pkg/yqlib/operator_omit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ var omitOperatorScenarios = []expressionScenario{
"D0, P[], (!!map)::myMap: {dog: bark, thing: hamster}\n",
},
},
{
description: "Omit splat",
skipDoc: true,
document: "{cat: meow, dog: bark, hamster: squeak}\n",
expression: `omit(["dog"])[]`,
expected: []string{
"D0, P[cat], (!!str)::meow\n",
"D0, P[hamster], (!!str)::squeak\n",
},
},
{
description: "Omit keys from map",
skipDoc: true,
Expand Down
10 changes: 10 additions & 0 deletions pkg/yqlib/operator_path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ var pathOperatorScenarios = []expressionScenario{
"D0, P[a b], (!!seq)::- a\n- b\n",
},
},
{
description: "Map path",
document: `{a: {b: cat}}`,
skipDoc: true,
expression: `.a.b | path[]`,
expected: []string{
"D0, P[a b 0], (!!str)::a\n",
"D0, P[a b 1], (!!str)::b\n",
},
},
{
skipDoc: true,
document: `a:
Expand Down
10 changes: 10 additions & 0 deletions pkg/yqlib/operator_pick_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ var pickOperatorScenarios = []expressionScenario{
"D0, P[], (!!map)::myMap: {hamster: squeak, cat: meow}\n",
},
},
{
description: "Pick splat",
skipDoc: true,
document: "{cat: meow, dog: bark, thing: hamster, hamster: squeak}\n",
expression: `pick(["hamster", "cat"])[]`,
expected: []string{
"D0, P[hamster], (!!str)::squeak\n",
"D0, P[cat], (!!str)::meow\n",
},
},
{
description: "Pick keys from map",
skipDoc: true,
Expand Down

0 comments on commit f1964de

Please sign in to comment.