diff --git a/mistral/tests/unit/test_expressions.py b/mistral/tests/unit/test_expressions.py index 5149222b8..2d0a51733 100644 --- a/mistral/tests/unit/test_expressions.py +++ b/mistral/tests/unit/test_expressions.py @@ -77,6 +77,15 @@ class YaqlEvaluatorTest(base.BaseTest): self.assertEqual('3', self._evaluator.evaluate('str($)', '3')) self.assertEqual('3', self._evaluator.evaluate('str($)', 3)) + def test_function_len(self): + self.assertEqual(3, self._evaluator.evaluate('$.len()', 'hey')) + data = [{'some': 'thing'}] + + self.assertEqual( + 1, + self._evaluator.evaluate('$[$.some = thing].len()', data) + ) + def test_validate(self): self._evaluator.validate('abc') self._evaluator.validate('1') diff --git a/mistral/yaql_utils.py b/mistral/yaql_utils.py index 46593edfe..e89ab3c48 100644 --- a/mistral/yaql_utils.py +++ b/mistral/yaql_utils.py @@ -13,8 +13,6 @@ # limitations under the License. import collections -import six -import types import yaql from yaql import context @@ -29,21 +27,21 @@ def create_yaql_context(): def _register_functions(yaql_ctx): - yaql_ctx.register_function(_string_and_iterable_length, 'len') - yaql_ctx.register_function(_generator_length, 'len') + yaql_ctx.register_function(_sized_length, 'len') + yaql_ctx.register_function(_iterable_length, 'len') yaql_ctx.register_function(to_str, 'str') # Additional convenience YAQL functions. -@context.EvalArg('a', arg_type=(six.string_types, collections.Iterable)) -def _string_and_iterable_length(a): +@context.EvalArg('a', arg_type=collections.Sized) +def _sized_length(a): return len(a) -@context.EvalArg('a', arg_type=types.GeneratorType) -def _generator_length(a): +@context.EvalArg('a', arg_type=collections.Iterable) +def _iterable_length(a): return sum(1 for i in a)