Skip to content

Commit 9bbb848

Browse files
committed
Add map() function
Proposed in jmespath/jmespath.site#15
1 parent f90040c commit 9bbb848

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

jmespath/functions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,13 @@ def _func_floor(self, arg):
251251
def _func_join(self, separator, array):
252252
return separator.join(array)
253253

254+
@builtin_function({'types': ['expref']}, {'types': ['array']})
255+
def _func_map(self, expref, arg):
256+
result = []
257+
for element in arg:
258+
result.append(self.interpreter.visit(expref.expression, element))
259+
return result
260+
254261
@builtin_function({"types": ['array-number', 'array-string']})
255262
def _func_max(self, arg):
256263
if arg:

tests/compliance/functions.json

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,4 +746,61 @@
746746
]
747747
}
748748
]
749-
}]
749+
}, {
750+
"given":
751+
{
752+
"people": [
753+
{"a": 10, "b": 1, "c": "z"},
754+
{"a": 10, "b": 2, "c": null},
755+
{"a": 10, "b": 3},
756+
{"a": 10, "b": 4, "c": "z"},
757+
{"a": 10, "b": 5, "c": null},
758+
{"a": 10, "b": 6},
759+
{"a": 10, "b": 7, "c": "z"},
760+
{"a": 10, "b": 8, "c": null},
761+
{"a": 10, "b": 9}
762+
]
763+
},
764+
"cases": [
765+
{
766+
"expression": "map(&a, people)",
767+
"result": [10, 10, 10, 10, 10, 10, 10, 10, 10]
768+
},
769+
{
770+
"expression": "map(&c, people)",
771+
"result": ["z", null, null, "z", null, null, "z", null, null]
772+
},
773+
{
774+
"expression": "map(&a, badkey)",
775+
"error": "invalid-type"
776+
}
777+
]
778+
}, {
779+
"given": {
780+
"array": [
781+
{
782+
"foo": {"bar": "yes1"}
783+
},
784+
{
785+
"foo": {"bar": "yes2"}
786+
},
787+
{
788+
"foo1": {"bar": "no"}
789+
}
790+
]},
791+
"cases": [
792+
{
793+
"expression": "map(&foo.bar, array)",
794+
"result": ["yes1", "yes2", null]
795+
},
796+
{
797+
"expression": "map(&foo1.bar, array)",
798+
"result": [null, null, "no"]
799+
},
800+
{
801+
"expression": "map(&foo.bar.baz, array)",
802+
"result": [null, null, null]
803+
}
804+
]
805+
}
806+
]

0 commit comments

Comments
 (0)