Fixing YAQL len() function in Mistral

Closes-Bug: #1451754

Change-Id: Ie6782d35fb3c856fe73da4936efdcd222314d2da
This commit is contained in:
Nikolay Mahotkin 2015-05-05 13:04:21 +03:00
parent 50ebc71b4c
commit 1d80a89632
2 changed files with 15 additions and 8 deletions

View File

@ -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')

View File

@ -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)