Skip to content

Commit 3196d58

Browse files
committed
Add to_array function
1 parent c75413e commit 3196d58

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

jmespath/functions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,13 @@ def _func_not_null(self, *arguments):
185185
if argument is not None:
186186
return argument
187187

188+
@builtin_function({'types': []})
189+
def _func_to_array(self, arg):
190+
if isinstance(arg, list):
191+
return arg
192+
else:
193+
return [arg]
194+
188195
@builtin_function({'types': []})
189196
def _func_to_string(self, arg):
190197
if isinstance(arg, STRING_TYPE):

tests/compliance/functions.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,26 @@
431431
"expression": "sum(`[]`)",
432432
"result": 0
433433
},
434+
{
435+
"expression": "to_array(`foo`)",
436+
"result": ["foo"]
437+
},
438+
{
439+
"expression": "to_array(`0`)",
440+
"result": [0]
441+
},
442+
{
443+
"expression": "to_array(objects)",
444+
"result": [{"foo": "bar", "bar": "baz"}]
445+
},
446+
{
447+
"expression": "to_array(`[1, 2, 3]`)",
448+
"result": [1, 2, 3]
449+
},
450+
{
451+
"expression": "to_array(false)",
452+
"result": [false]
453+
},
434454
{
435455
"expression": "to_string(`foo`)",
436456
"result": "foo"

0 commit comments

Comments
 (0)