From 6fc541a44caba96ee2beb87ad5e5b8e7c33dfed9 Mon Sep 17 00:00:00 2001 From: Dougal Matthews Date: Tue, 24 Oct 2017 11:00:24 +0100 Subject: [PATCH] Add a json_dump expression function This is a new and simplified version of the json_pp function. It uses on the standard jsonutils formatting and wont output the context if not argument is provided. Change-Id: I37f69d14e7cf4f57b910b355d7ccd31c9cd73d10 --- .../unit/expressions/test_yaql_expression.py | 34 ++++++++++++++++++- mistral/utils/expression_utils.py | 10 ++++++ ...mp-deprecate-json-pp-252c6c495fd2dea1.yaml | 10 ++++++ setup.cfg | 11 +++--- 4 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 releasenotes/notes/add-json-dump-deprecate-json-pp-252c6c495fd2dea1.yaml diff --git a/mistral/tests/unit/expressions/test_yaql_expression.py b/mistral/tests/unit/expressions/test_yaql_expression.py index 1fde31e05..6e6d060ab 100644 --- a/mistral/tests/unit/expressions/test_yaql_expression.py +++ b/mistral/tests/unit/expressions/test_yaql_expression.py @@ -15,11 +15,15 @@ # limitations under the License. import datetime +import json +import warnings + +import mock + from mistral import exceptions as exc from mistral.expressions import yaql_expression as expr from mistral.tests.unit import base from mistral import utils -import mock DATA = { "server": { @@ -133,6 +137,34 @@ class YaqlEvaluatorTest(base.BaseTest): ) ) + def test_function_json_pp_deprecation(self): + with warnings.catch_warnings(record=True) as w: + result = self._evaluator.evaluate('json_pp($)', '3') + self.assertEqual('"3"', result) + self.assertEqual(len(w), 1) + self.assertTrue(issubclass(w[-1].category, DeprecationWarning)) + self.assertTrue(str(w[-1].message).startswith( + "json_pp was deprecated in Queens and will be removed in the S " + )) + + def test_function_json_dump(self): + self.assertEqual('"3"', self._evaluator.evaluate('json_dump($)', '3')) + self.assertEqual('3', self._evaluator.evaluate('json_dump($)', 3)) + self.assertEqual( + json.dumps([1, 2], indent=4), + self._evaluator.evaluate('json_dump($)', [1, 2]) + ) + self.assertEqual( + json.dumps({"a": "b"}, indent=4), + self._evaluator.evaluate('json_dump($)', {'a': 'b'}) + ) + self.assertEqual( + json.dumps('\n'.join(["Mistral", "is", "awesome"]), indent=4), + self._evaluator.evaluate( + 'json_dump($)', '\n'.join(['Mistral', 'is', 'awesome']) + ) + ) + def test_function_uuid(self): uuid = self._evaluator.evaluate('uuid()', {}) diff --git a/mistral/utils/expression_utils.py b/mistral/utils/expression_utils.py index 3bf3d107b..a97472cb6 100644 --- a/mistral/utils/expression_utils.py +++ b/mistral/utils/expression_utils.py @@ -14,6 +14,7 @@ # limitations under the License. from functools import partial +import warnings from oslo_serialization import jsonutils from stevedore import extension @@ -116,12 +117,21 @@ def execution_(context): def json_pp_(context, data=None): + warnings.warn( + "json_pp was deprecated in Queens and will be removed in the S cycle. " + "The json_dump expression function can be used for outputting JSON", + DeprecationWarning + ) return jsonutils.dumps( data or context, indent=4 ).replace("\\n", "\n").replace(" \n", "\n") +def json_dump_(context, data): + return jsonutils.dumps(data, indent=4) + + def yaml_dump_(context, data): return yaml.safe_dump(data, default_flow_style=False) diff --git a/releasenotes/notes/add-json-dump-deprecate-json-pp-252c6c495fd2dea1.yaml b/releasenotes/notes/add-json-dump-deprecate-json-pp-252c6c495fd2dea1.yaml new file mode 100644 index 000000000..ebcfe0626 --- /dev/null +++ b/releasenotes/notes/add-json-dump-deprecate-json-pp-252c6c495fd2dea1.yaml @@ -0,0 +1,10 @@ +--- +features: + - | + A new YAQL/jinja2 expression function has been added for outputting JSON. + It is json_dump and accepts one argument, which is the object to be + serialised to JSON. +deprecations: + - | + The YAQL/jinja2 expression function ``json_pp`` has been deprecated and + will be removed in the S cycle. ``json_dump`` should be used instead. diff --git a/setup.cfg b/setup.cfg index fff40150c..729e6f31f 100644 --- a/setup.cfg +++ b/setup.cfg @@ -76,14 +76,17 @@ mistral.executors = remote = mistral.executors.remote_executor:RemoteExecutor mistral.expression.functions = - global = mistral.utils.expression_utils:global_ + # json_pp was deprecated in Queens and will be removed in the S cycle json_pp = mistral.utils.expression_utils:json_pp_ + + env = mistral.utils.expression_utils:env_ + execution = mistral.utils.expression_utils:execution_ + global = mistral.utils.expression_utils:global_ + json_parse = mistral.utils.expression_utils:json_parse_ + json_dump = mistral.utils.expression_utils:json_dump_ task = mistral.utils.expression_utils:task_ tasks = mistral.utils.expression_utils:tasks_ - execution = mistral.utils.expression_utils:execution_ - env = mistral.utils.expression_utils:env_ uuid = mistral.utils.expression_utils:uuid_ - json_parse = mistral.utils.expression_utils:json_parse_ yaml_parse = mistral.utils.expression_utils:yaml_parse_ mistral.expression.evaluators =