Skip to content

Commit 5726355

Browse files
committed
Merge branch 'more-functions-r2' into develop
* more-functions-r2: Add a merge() function Add to_array function
2 parents 0c77ac8 + 1a8efcd commit 5726355

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

jmespath/functions.py

Lines changed: 14 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):
@@ -252,6 +259,13 @@ def _func_max(self, arg):
252259
else:
253260
return None
254261

262+
@builtin_function({"types": ["object"], "variadic": True})
263+
def _func_merge(self, *arguments):
264+
merged = {}
265+
for arg in arguments:
266+
merged.update(arg)
267+
return merged
268+
255269
@builtin_function({"types": ['array-number', 'array-string']})
256270
def _func_min(self, arg):
257271
if arg:

tests/compliance/functions.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,26 @@
239239
"expression": "max(empty_list)",
240240
"result": null
241241
},
242+
{
243+
"expression": "merge(`{}`)",
244+
"result": {}
245+
},
246+
{
247+
"expression": "merge(`{}`, `{}`)",
248+
"result": {}
249+
},
250+
{
251+
"expression": "merge(`{\"a\": 1}`, `{\"b\": 2}`)",
252+
"result": {"a": 1, "b": 2}
253+
},
254+
{
255+
"expression": "merge(`{\"a\": 1}`, `{\"a\": 2}`)",
256+
"result": {"a": 2}
257+
},
258+
{
259+
"expression": "merge(`{\"a\": 1, \"b\": 2}`, `{\"a\": 2, \"c\": 3}`, `{\"d\": 4}`)",
260+
"result": {"a": 2, "b": 2, "c": 3, "d": 4}
261+
},
242262
{
243263
"expression": "min(numbers)",
244264
"result": -1
@@ -431,6 +451,26 @@
431451
"expression": "sum(`[]`)",
432452
"result": 0
433453
},
454+
{
455+
"expression": "to_array(`foo`)",
456+
"result": ["foo"]
457+
},
458+
{
459+
"expression": "to_array(`0`)",
460+
"result": [0]
461+
},
462+
{
463+
"expression": "to_array(objects)",
464+
"result": [{"foo": "bar", "bar": "baz"}]
465+
},
466+
{
467+
"expression": "to_array(`[1, 2, 3]`)",
468+
"result": [1, 2, 3]
469+
},
470+
{
471+
"expression": "to_array(false)",
472+
"result": [false]
473+
},
434474
{
435475
"expression": "to_string(`foo`)",
436476
"result": "foo"

0 commit comments

Comments
 (0)