Skip to content

Commit

Permalink
feat(jexl): improve mapby and add stringify transform
Browse files Browse the repository at this point in the history
This extends the mapby transform to accept multiple keys as argument
which then returns an array instead of the single value. This will
result in nested arrays which is why we added a stringify transform that
converts such a collection into a string to be used in comparisons.
  • Loading branch information
anehx committed Mar 16, 2021
1 parent 9cf11f3 commit 5fc1c73
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
20 changes: 15 additions & 5 deletions caluma/caluma_core/jexl.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import math
import numbers
from functools import partial
Expand Down Expand Up @@ -71,9 +72,8 @@ def __init__(self, *args, **kwargs):
self.add_transform("debug", self._debug_transform)
self._expr_stack = []

self.add_transform(
"mapby", lambda arr, key: [obj.get(key, None) for obj in arr]
)
self.add_transform("mapby", self._mapby_transform)
self.add_transform("stringify", lambda obj: json.dumps(obj))
self.add_binary_operator(
"intersects", 20, lambda left, right: any(x in right for x in left)
)
Expand Down Expand Up @@ -143,6 +143,15 @@ def _debug_transform(self, value, label=None):
)
return value

def _mapby_transform(self, arr, *keys):
if not isinstance(arr, list):
return None

return [
[obj.get(key) for key in keys] if len(keys) > 1 else obj.get(keys[0])
for obj in arr
]

def evaluate(self, expression, context=None):
self._expr_stack.append(expression)
try:
Expand Down Expand Up @@ -218,8 +227,9 @@ def visit_Transform(self, transform):
and transform.subject
and transform.subject.name == "answer"
and len(transform.args)
and isinstance(transform.args[0], Literal)
):
yield transform.args[0].value
for arg in transform.args:
if isinstance(arg, Literal):
yield arg.value

yield from self.generic_visit(transform)
13 changes: 13 additions & 0 deletions caluma/caluma_core/tests/test_jexl.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ def test_jexl_cache():
("[{ key: 1 }]|mapby('key')", [1]),
("[{ otherkey: 1 }]|mapby('key')", [None]),
("[]|mapby('key')", []),
("[{ a: 1, b: 2 }]|mapby('a', 'b', 'c')", [[1, 2, None]]),
("0|mapby('key')", None),
],
)
def test_mapby_operator(expression, result):
Expand Down Expand Up @@ -191,6 +193,17 @@ def test_math_transforms(expression, result):
assert JEXL().evaluate(expression) == result


@pytest.mark.parametrize(
"expression,result",
[
("[['test1', 'test2'], [1,2]]|stringify", '[["test1", "test2"], [1, 2]]'),
("1|stringify", "1"),
],
)
def test_stringify_transform(expression, result):
assert JEXL().evaluate(expression) == result


@pytest.mark.parametrize(
"expression,expected",
[
Expand Down
12 changes: 10 additions & 2 deletions caluma/caluma_form/tests/test_question.py
Original file line number Diff line number Diff line change
Expand Up @@ -961,9 +961,9 @@ def test_calculated_question(
("'table'|answer|mapby('column')[0]", 99.99, ["table", "column"]),
("'other'|answer", 3.0, ["other"]),
(
"'sub_question'|answer && 'table'|answer|mapby('column') ? 'other'|answer: -1",
"'sub_question'|answer && 'table'|answer|mapby('column', 'column2') ? 'other'|answer: -1",
3.0,
["sub_question", "table", "column", "other"],
["sub_question", "table", "column", "column2", "other"],
),
],
)
Expand All @@ -972,6 +972,7 @@ def test_nested_calculated_question(
schema_executor,
form_and_document,
form_question_factory,
question_factory,
expr,
expected,
calc_deps,
Expand All @@ -993,6 +994,13 @@ def test_nested_calculated_question(
form=form, question__slug="other", question__type=models.Question.TYPE_INTEGER
).question

questions["column2"] = question_factory(
slug="column2", is_required="false", is_hidden="false"
)
form_question_factory(
form=questions["table"].row_form, question=questions["column2"]
)

api.save_answer(question=questions["other"], document=document, value=3)

questions["calc"] = form_question_factory(
Expand Down

0 comments on commit 5fc1c73

Please sign in to comment.