Added support for shorthand splat[] for filter,reverse,sort,shuffle

This commit is contained in:
Mike Farah 2024-06-15 22:13:58 +10:00
parent d9af2dd976
commit 45eda8dbae
6 changed files with 67 additions and 6 deletions

View File

@ -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 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: 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 errorOpType = &operationType{Type: "ERROR", NumArgs: 1, Precedence: 50, Handler: errorOperator}
var pickOpType = &operationType{Type: "PICK", NumArgs: 1, Precedence: 50, Handler: pickOperator} var pickOpType = &operationType{Type: "PICK", NumArgs: 1, Precedence: 50, Handler: pickOperator}
var omitOpType = &operationType{Type: "OMIT", NumArgs: 1, Precedence: 50, Handler: omitOperator} var omitOpType = &operationType{Type: "OMIT", NumArgs: 1, Precedence: 50, Handler: omitOperator}
@ -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 delPathsOpType = &operationType{Type: "DEL_PATHS", NumArgs: 1, Precedence: 50, Handler: delPathsOperator}
var explodeOpType = &operationType{Type: "EXPLODE", NumArgs: 1, Precedence: 50, Handler: explodeOperator} var explodeOpType = &operationType{Type: "EXPLODE", NumArgs: 1, Precedence: 50, Handler: explodeOperator}
var sortByOpType = &operationType{Type: "SORT_BY", NumArgs: 1, Precedence: 50, Handler: sortByOperator} var sortByOpType = &operationType{Type: "SORT_BY", NumArgs: 1, Precedence: 52, Handler: sortByOperator, CheckForPostTraverse: true}
var reverseOpType = &operationType{Type: "REVERSE", NumArgs: 0, Precedence: 50, Handler: reverseOperator} var reverseOpType = &operationType{Type: "REVERSE", NumArgs: 0, Precedence: 52, Handler: reverseOperator, CheckForPostTraverse: true}
var sortOpType = &operationType{Type: "SORT", NumArgs: 0, Precedence: 50, Handler: sortOperator} var sortOpType = &operationType{Type: "SORT", NumArgs: 0, Precedence: 52, Handler: sortOperator, CheckForPostTraverse: true}
var shuffleOpType = &operationType{Type: "SHUFFLE", NumArgs: 0, Precedence: 50, Handler: shuffleOperator} 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 joinStringOpType = &operationType{Type: "JOIN", NumArgs: 1, Precedence: 50, Handler: joinStringOperator}
var subStringOpType = &operationType{Type: "SUBSTR", NumArgs: 1, Precedence: 50, Handler: substituteStringOperator} var subStringOpType = &operationType{Type: "SUBSTR", NumArgs: 1, Precedence: 50, Handler: substituteStringOperator}

View File

@ -13,6 +13,16 @@ var filterOperatorScenarios = []expressionScenario{
"D0, P[], (!!seq)::[1, 2]\n", "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", description: "Filter map values",
document: `{c: {things: cool, frog: yes}, d: {things: hot, frog: false}}`, document: `{c: {things: cool, frog: yes}, d: {things: hot, frog: false}}`,

View File

@ -11,6 +11,16 @@ var reverseOperatorScenarios = []expressionScenario{
"D0, P[], (!!seq)::[3, 2, 1]\n", "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, skipDoc: true,
document: "[]", document: "[]",

View File

@ -11,6 +11,17 @@ var shuffleOperatorScenarios = []expressionScenario{
"D0, P[], (!!seq)::[5, 2, 4, 1, 3]\n", "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", description: "Shuffle array in place",

View File

@ -13,6 +13,16 @@ var sortKeysOperatorScenarios = []expressionScenario{
"D0, P[], (!!map)::{a: blah, b: bing, c: frog}\n", "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, skipDoc: true,
document: `{c: frog}`, document: `{c: frog}`,

View File

@ -11,6 +11,16 @@ var sortByOperatorScenarios = []expressionScenario{
"D0, P[], (!!seq)::[{a: apple}, {a: banana}, {a: cat}]\n", "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", description: "Sort by multiple fields",
document: "[{a: dog},{a: cat, b: banana},{a: cat, b: apple}]", document: "[{a: dog},{a: cat, b: banana},{a: cat, b: apple}]",
@ -104,6 +114,16 @@ var sortByOperatorScenarios = []expressionScenario{
"D0, P[], (!!seq)::[null, false, true, 3, 6, 8, cat]\n", "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, skipDoc: true,
description: "false before true", description: "false before true",