diff --git a/caluma/caluma_core/jexl.py b/caluma/caluma_core/jexl.py index d0af1d82b..27c797bc9 100644 --- a/caluma/caluma_core/jexl.py +++ b/caluma/caluma_core/jexl.py @@ -75,6 +75,7 @@ def __init__(self, *args, **kwargs): self.add_transform("mapby", self._mapby_transform) self.add_transform("stringify", lambda obj: json.dumps(obj)) self.add_transform("flatten", self._flatten_transform) + self.add_transform("length", self._length_transform) self.add_binary_operator( "intersects", 20, lambda left, right: any(x in right for x in left) ) @@ -159,6 +160,12 @@ def _flatten_transform(self, arr, *options): return list(chain(*arr)) + def _length_transform(self, value, *options): + try: + return len(value) + except TypeError: + return None + def evaluate(self, expression, context=None): self._expr_stack.append(expression) try: diff --git a/caluma/caluma_core/tests/test_jexl.py b/caluma/caluma_core/tests/test_jexl.py index d924b759e..13ca69300 100644 --- a/caluma/caluma_core/tests/test_jexl.py +++ b/caluma/caluma_core/tests/test_jexl.py @@ -194,6 +194,13 @@ def test_intersects_operator(expression, result): ["some-value", "some-other-value"], ), ("null|flatten", None), + # count + ("['test1', 'test2']|length", 2), + ("{key: 1}|length", 1), + ("'foobar'|length", 6), + ("1|length", None), + ("1.1|length", None), + ("null|length", None), ], ) def test_simple_transforms(expression, result):