From 96603bced345e9cce8c79f514d1cc7d8a0121170 Mon Sep 17 00:00:00 2001 From: Stan Lagun Date: Fri, 21 Nov 2014 12:58:37 +0300 Subject: [PATCH] Adds aggregate function to MuranoPL (YAQL) Aggregate is similar to python's reduce() built-in Change-Id: If1e3ed20172c40344b771627ce20739348b80163 --- murano/engine/system/yaql_functions.py | 12 ++++++++++++ .../unit/dsl/meta/TestEngineFunctions.yaml | 17 +++++++++++++++++ .../unit/dsl/test_engine_yaql_functions.py | 12 ++++++++++++ 3 files changed, 41 insertions(+) diff --git a/murano/engine/system/yaql_functions.py b/murano/engine/system/yaql_functions.py index 1d0d3eaf..9a292e48 100644 --- a/murano/engine/system/yaql_functions.py +++ b/murano/engine/system/yaql_functions.py @@ -312,6 +312,16 @@ def _take(collection, count): return itertools.islice(collection, count) +@yaql.context.EvalArg('collection', collections.Iterable) +def _aggregate(collection, selector): + return reduce(selector, collection) + + +@yaql.context.EvalArg('collection', collections.Iterable) +def _aggregate_with_seed(collection, selector, seed): + return reduce(selector, collection, seed()) + + def register(context): context.register_function( lambda json, mappings: _transform_json(json(), mappings()), 'bind') @@ -347,3 +357,5 @@ def register(context): context.register_function(_merge_with, 'mergeWith') context.register_function(_skip, 'skip') context.register_function(_take, 'take') + context.register_function(_aggregate, 'aggregate') + context.register_function(_aggregate_with_seed, 'aggregate') diff --git a/murano/tests/unit/dsl/meta/TestEngineFunctions.yaml b/murano/tests/unit/dsl/meta/TestEngineFunctions.yaml index 650c363c..78bec46b 100644 --- a/murano/tests/unit/dsl/meta/TestEngineFunctions.yaml +++ b/murano/tests/unit/dsl/meta/TestEngineFunctions.yaml @@ -253,3 +253,20 @@ Methods: Contract: $.int() Body: - Return: $list.skip($start).take($count) + + + testAggregate: + Arguments: + - list: + Contract: [$.int()] + Body: + - Return: $list.aggregate($1 + $2) + + testAggregateWithInitializer: + Arguments: + - list: + Contract: [$.int()] + - initializer: + Contract: $.int() + Body: + - Return: $list.aggregate($1 + $2, $initializer) diff --git a/murano/tests/unit/dsl/test_engine_yaql_functions.py b/murano/tests/unit/dsl/test_engine_yaql_functions.py index e071222b..8e20c00d 100644 --- a/murano/tests/unit/dsl/test_engine_yaql_functions.py +++ b/murano/tests/unit/dsl/test_engine_yaql_functions.py @@ -224,3 +224,15 @@ class TestEngineYaqlFunctions(test_case.DslTestCase): [1, 2, 3, 4, 5, 6, 7, 8], 2, 3) ) + + def test_aggregate(self): + self.assertEqual( + 10, + self._runner.testAggregate([1, 2, 3, 4]) + ) + + def test_aggregate_with_initializer(self): + self.assertEqual( + 21, + self._runner.testAggregateWithInitializer([1, 2, 3, 4], 11) + )