Skip to content

Commit

Permalink
added in operator for dicts
Browse files Browse the repository at this point in the history
fixes #673
  • Loading branch information
wouterdb committed Jun 26, 2018
1 parent 1efc7e9 commit b6fc6f6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/inmanta/ast/constraint/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ def _is_final(self, result):

class In(BinaryOperator):
"""
The in operator for iterable types
The in operator for iterable types and dicts
"""
__op = "in"

Expand All @@ -448,11 +448,13 @@ def _bin_op(self, arg1, arg2):
"""
@see Operator#_op
"""
if not (isinstance(arg2, list) or (hasattr(arg2, "type") and arg2.type() == list)):
raise Exception("Operand two of 'in' can only be a list (%s)" % arg2[0])

for arg in arg2:
if arg == arg1:
return True
if isinstance(arg2, dict):
return arg1 in arg2
elif isinstance(arg2, list):
for arg in arg2:
if arg == arg1:
return True
else:
raise Exception("Operand two of 'in' can only be a list or dict (%s)" % arg2[0])

return False
34 changes: 34 additions & 0 deletions tests/test_compilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2272,3 +2272,37 @@ def test_lnr_on_double_is_defined(snippetcompiler):
a.one = a
""")
compiler.do_compile()


def test_673_in_dict(snippetcompiler):
snippetcompiler.setup_for_snippet("""
entity Test:
dict attributes
end
implementation test for Test:
end
implement Test using test when "foo" in self.attributes
Test(attributes={"foo": 42})
""")
compiler.do_compile()


def test_673_in_list(snippetcompiler):
snippetcompiler.setup_for_snippet("""
entity Test:
string[] attributes
end
implementation test for Test:
end
implement Test using test when "foo" in self.attributes
Test(attributes=["blah", "foo"])
""")
compiler.do_compile()

0 comments on commit b6fc6f6

Please sign in to comment.