diff --git a/pkg/yqlib/operation.go b/pkg/yqlib/operation.go index 3dcabd89..7afe11bd 100644 --- a/pkg/yqlib/operation.go +++ b/pkg/yqlib/operation.go @@ -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} @@ -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} diff --git a/pkg/yqlib/operator_filter_test.go b/pkg/yqlib/operator_filter_test.go index 0536d0c2..aff56c27 100644 --- a/pkg/yqlib/operator_filter_test.go +++ b/pkg/yqlib/operator_filter_test.go @@ -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}}`, diff --git a/pkg/yqlib/operator_reverse_test.go b/pkg/yqlib/operator_reverse_test.go index 3d677425..0ea0d672 100644 --- a/pkg/yqlib/operator_reverse_test.go +++ b/pkg/yqlib/operator_reverse_test.go @@ -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: "[]", diff --git a/pkg/yqlib/operator_shuffle_test.go b/pkg/yqlib/operator_shuffle_test.go index c6c30f5f..0c4e00ac 100644 --- a/pkg/yqlib/operator_shuffle_test.go +++ b/pkg/yqlib/operator_shuffle_test.go @@ -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", diff --git a/pkg/yqlib/operator_sort_keys_test.go b/pkg/yqlib/operator_sort_keys_test.go index 586a1ce8..c895b194 100644 --- a/pkg/yqlib/operator_sort_keys_test.go +++ b/pkg/yqlib/operator_sort_keys_test.go @@ -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}`, diff --git a/pkg/yqlib/operator_sort_test.go b/pkg/yqlib/operator_sort_test.go index 0b60545e..765317ed 100644 --- a/pkg/yqlib/operator_sort_test.go +++ b/pkg/yqlib/operator_sort_test.go @@ -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}]", @@ -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",