Skip to content

Commit 1a8efcd

Browse files
committed
Add a merge() function
This allows you to apply a base hash and merge overrides on top of each successive hash.
1 parent 3196d58 commit 1a8efcd

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
@@ -259,6 +259,13 @@ def _func_max(self, arg):
259259
else:
260260
return None
261261

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+
262269
@builtin_function({"types": ['array-number', 'array-string']})
263270
def _func_min(self, arg):
264271
if arg:

tests/compliance/functions.json

Lines changed: 20 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

0 commit comments

Comments
 (0)