From fa0a5fdfaace7af868c5c7daada24091437ff602 Mon Sep 17 00:00:00 2001 From: Thomas Neidhart Date: Mon, 16 Sep 2024 23:09:29 +0200 Subject: [PATCH] fix: restore support for JFunction and add test case --- src/jsonata/functions.py | 2 +- tests/custom_function_test.py | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/jsonata/functions.py b/src/jsonata/functions.py index b093f4a..951e1a1 100644 --- a/src/jsonata/functions.py +++ b/src/jsonata/functions.py @@ -1575,7 +1575,7 @@ def get_function_arity(func: Any) -> int: return len(signature(func.function).parameters) else: - raise RuntimeError(f"unexpected function '{type(func)}'") + return len(func.arguments) # # Helper function to build the arguments to be supplied to the function arg of the diff --git a/tests/custom_function_test.py b/tests/custom_function_test.py index 6d0ccdd..ef2426a 100644 --- a/tests/custom_function_test.py +++ b/tests/custom_function_test.py @@ -23,7 +23,11 @@ def test_ternary(self): expression.register_lambda("abc", lambda x, y, z: str(x) + str(y) + str(z)) assert expression.evaluate({"a": "a", "b": "b", "c": "c"}) == "abc" - def test_map(self): + def test_map_with_lambda(self): expression = jsonata.Jsonata("$map([1, 2, 3], $square)") expression.register_lambda("square", lambda x: x * x) assert expression.evaluate(None) == [1, 4, 9] + + def test_map_with_function(self): + expression = jsonata.Jsonata("$map([1, 2, 3], function($v) { $v * $v })") + assert expression.evaluate(None) == [1, 4, 9]