Skip to content

Commit

Permalink
Added support for shorthand splat[] for filter,reverse,sort,shuffle
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Jun 15, 2024
1 parent d9af2dd commit 45eda8d
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 6 deletions.
12 changes: 6 additions & 6 deletions pkg/yqlib/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ var expressionOpType = &operationType{Type: "EXP", NumArgs: 0, Precedence: 50, H

var collectOpType = &operationType{Type: "COLLECT", NumArgs: 1, Precedence: 50, Handler: collectOperator}
var mapOpType = &operationType{Type: "MAP", NumArgs: 1, Precedence: 50, Handler: mapOperator}
var filterOpType = &operationType{Type: "FILTER", NumArgs: 1, Precedence: 50, Handler: filterOperator}
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}
Expand Down Expand Up @@ -140,12 +140,12 @@ var setPathOpType = &operationType{Type: "SET_PATH", NumArgs: 1, Precedence: 50,
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: 50, Handler: sortByOperator}
var reverseOpType = &operationType{Type: "REVERSE", NumArgs: 0, Precedence: 50, Handler: reverseOperator}
var sortOpType = &operationType{Type: "SORT", NumArgs: 0, Precedence: 50, Handler: sortOperator}
var shuffleOpType = &operationType{Type: "SHUFFLE", NumArgs: 0, Precedence: 50, Handler: shuffleOperator}
var sortByOpType = &operationType{Type: "SORT_BY", NumArgs: 1, Precedence: 52, Handler: sortByOperator, CheckForPostTraverse: true}
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}

var sortKeysOpType = &operationType{Type: "SORT_KEYS", NumArgs: 1, Precedence: 50, Handler: sortKeysOperator}
var sortKeysOpType = &operationType{Type: "SORT_KEYS", NumArgs: 1, Precedence: 52, Handler: sortKeysOperator, CheckForPostTraverse: true}

var joinStringOpType = &operationType{Type: "JOIN", NumArgs: 1, Precedence: 50, Handler: joinStringOperator}
var subStringOpType = &operationType{Type: "SUBSTR", NumArgs: 1, Precedence: 50, Handler: substituteStringOperator}
Expand Down
10 changes: 10 additions & 0 deletions pkg/yqlib/operator_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ var filterOperatorScenarios = []expressionScenario{
"D0, P[], (!!seq)::[1, 2]\n",
},
},
{
description: "Filter array splat",
skipDoc: true,
document: `[1,2,3]`,
expression: `filter(. < 3)[]`,
expected: []string{
"D0, P[0], (!!int)::1\n",
"D0, P[1], (!!int)::2\n",
},
},
{
description: "Filter map values",
document: `{c: {things: cool, frog: yes}, d: {things: hot, frog: false}}`,
Expand Down
10 changes: 10 additions & 0 deletions pkg/yqlib/operator_reverse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ var reverseOperatorScenarios = []expressionScenario{
"D0, P[], (!!seq)::[3, 2, 1]\n",
},
},
{
description: "Reverse",
skipDoc: true,
document: "[1, 2]",
expression: `reverse[]`,
expected: []string{
"D0, P[1], (!!int)::2\n",
"D0, P[0], (!!int)::1\n",
},
},
{
skipDoc: true,
document: "[]",
Expand Down
11 changes: 11 additions & 0 deletions pkg/yqlib/operator_shuffle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ var shuffleOperatorScenarios = []expressionScenario{
"D0, P[], (!!seq)::[5, 2, 4, 1, 3]\n",
},
},
{
description: "Shuffle array",
skipDoc: true,
document: "[1, 2, 3]",
expression: `shuffle[]`,
expected: []string{
"D0, P[2], (!!int)::3\n",
"D0, P[0], (!!int)::1\n",
"D0, P[1], (!!int)::2\n",
},
},

{
description: "Shuffle array in place",
Expand Down
10 changes: 10 additions & 0 deletions pkg/yqlib/operator_sort_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ var sortKeysOperatorScenarios = []expressionScenario{
"D0, P[], (!!map)::{a: blah, b: bing, c: frog}\n",
},
},
{
description: "Sort keys of map",
skipDoc: true,
document: `{c: frog, a: zoo}`,
expression: `sort_keys(.)[]`,
expected: []string{
"D0, P[a], (!!str)::zoo\n",
"D0, P[c], (!!str)::frog\n",
},
},
{
skipDoc: true,
document: `{c: frog}`,
Expand Down
20 changes: 20 additions & 0 deletions pkg/yqlib/operator_sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ var sortByOperatorScenarios = []expressionScenario{
"D0, P[], (!!seq)::[{a: apple}, {a: banana}, {a: cat}]\n",
},
},
{
description: "Sort by string field",
skipDoc: true,
document: "[{a: banana},{a: apple}]",
expression: `sort_by(.a)[]`,
expected: []string{
"D0, P[1], (!!map)::{a: apple}\n",
"D0, P[0], (!!map)::{a: banana}\n",
},
},
{
description: "Sort by multiple fields",
document: "[{a: dog},{a: cat, b: banana},{a: cat, b: apple}]",
Expand Down Expand Up @@ -104,6 +114,16 @@ var sortByOperatorScenarios = []expressionScenario{
"D0, P[], (!!seq)::[null, false, true, 3, 6, 8, cat]\n",
},
},
{
description: "Sort, nulls come first",
skipDoc: true,
document: "[8,null]",
expression: `sort[]`,
expected: []string{
"D0, P[1], (!!null)::null\n",
"D0, P[0], (!!int)::8\n",
},
},
{
skipDoc: true,
description: "false before true",
Expand Down

0 comments on commit 45eda8d

Please sign in to comment.