Merge "Adding 'json_pp' function in YAQL"

This commit is contained in:
Jenkins 2015-10-20 17:05:10 +00:00 committed by Gerrit Code Review
commit 8d9252d104
2 changed files with 25 additions and 0 deletions

View File

@ -111,6 +111,24 @@ class YaqlEvaluatorTest(base.BaseTest):
self._evaluator.validate,
{'a': 1})
def test_json_pp(self):
self.assertEqual('"3"', self._evaluator.evaluate('json_pp($)', '3'))
self.assertEqual('3', self._evaluator.evaluate('json_pp($)', 3))
self.assertEqual(
'[\n 1, \n 2\n]',
self._evaluator.evaluate('json_pp($)', [1, 2])
)
self.assertEqual(
'{\n "a": "b"\n}',
self._evaluator.evaluate('json_pp($)', {'a': 'b'})
)
self.assertEqual(
'"Mistral\nis\nawesome"',
self._evaluator.evaluate(
'json_pp($)', '\n'.join(['Mistral', 'is', 'awesome'])
)
)
class InlineYAQLEvaluatorTest(base.BaseTest):
def setUp(self):

View File

@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import yaql
from mistral.db.v2 import api as db_api
@ -42,6 +44,7 @@ def _register_functions(yaql_ctx):
yaql_ctx.register_function(env_)
yaql_ctx.register_function(execution_)
yaql_ctx.register_function(task_)
yaql_ctx.register_function(json_pp_, name='json_pp')
# Additional YAQL functions needed by Mistral.
@ -56,6 +59,10 @@ def execution_(context):
return context['__execution']
def json_pp_(data):
return json.dumps(data, indent=4).replace("\\n", "\n")
def task_(context, task_name):
# Importing data_flow in order to break cycle dependency between modules.
from mistral.workflow import data_flow