Skip to content

Commit 04c57b2

Browse files
committed
Merge branch 'map-func' into develop
* map-func: Add more map tests Add map() function
2 parents 13bd21a + 618547c commit 04c57b2

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-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: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,4 +746,76 @@
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+
"empty": []
764+
},
765+
"cases": [
766+
{
767+
"expression": "map(&a, people)",
768+
"result": [10, 10, 10, 10, 10, 10, 10, 10, 10]
769+
},
770+
{
771+
"expression": "map(&c, people)",
772+
"result": ["z", null, null, "z", null, null, "z", null, null]
773+
},
774+
{
775+
"expression": "map(&a, badkey)",
776+
"error": "invalid-type"
777+
},
778+
{
779+
"expression": "map(&foo, empty)",
780+
"result": []
781+
}
782+
]
783+
}, {
784+
"given": {
785+
"array": [
786+
{
787+
"foo": {"bar": "yes1"}
788+
},
789+
{
790+
"foo": {"bar": "yes2"}
791+
},
792+
{
793+
"foo1": {"bar": "no"}
794+
}
795+
]},
796+
"cases": [
797+
{
798+
"expression": "map(&foo.bar, array)",
799+
"result": ["yes1", "yes2", null]
800+
},
801+
{
802+
"expression": "map(&foo1.bar, array)",
803+
"result": [null, null, "no"]
804+
},
805+
{
806+
"expression": "map(&foo.bar.baz, array)",
807+
"result": [null, null, null]
808+
}
809+
]
810+
}, {
811+
"given": {
812+
"array": [[1, 2, 3, [4]], [5, 6, 7, [8, 9]]]
813+
},
814+
"cases": [
815+
{
816+
"expression": "map(&[], array)",
817+
"result": [[1, 2, 3, 4], [5, 6, 7, 8, 9]]
818+
}
819+
]
820+
}
821+
]

0 commit comments

Comments
 (0)