Skip to content

Commit

Permalink
Merge 3db9c72 into 6043909
Browse files Browse the repository at this point in the history
  • Loading branch information
snopoke committed Jul 15, 2021
2 parents 6043909 + 3db9c72 commit 64c42ea
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
10 changes: 9 additions & 1 deletion commcare_export/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,13 @@ def substr(val, start, end):
return val[start:end]


@unwrap('val')
def unique(val):
if isinstance(val, list):
return list(set(val))
return val


class BuiltInEnv(DictEnv):
"""
A built-in environment of operators and functions
Expand Down Expand Up @@ -519,7 +526,8 @@ def __init__(self, d=None):
'or': _or,
'sha1': sha1,
'substr': substr,
'_or_raw': _or_raw, # for internal use
'_or_raw': _or_raw, # for internal use,
'unique': unique
})
return super(BuiltInEnv, self).__init__(d)

Expand Down
5 changes: 5 additions & 0 deletions tests/test_minilinq.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,11 @@ def test_case_url(self):
expected = 'https://www.commcarehq.org/a/d1/reports/case_data/123/'
assert Apply(Reference('case_url'), Reference('id')).eval(env) == expected

def test_unique(self):
env = BuiltInEnv() | JsonPathEnv(
{"list": [{"a": 1}, {"a": 2}, {"a": 3}, {"a": 2}]})
assert Apply(Reference('unique'), Reference('list[*].a')).eval(env) == [1, 2, 3]

def test_template(self):
env = BuiltInEnv() | JsonPathEnv({'a': '1', 'b': '2'})
assert Apply(Reference('template'), Literal('{}.{}'), Reference('a'), Reference('b')).eval(env) == '1.2'
Expand Down

0 comments on commit 64c42ea

Please sign in to comment.