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
This commit is contained in:
Dougal Matthews 2017-10-24 11:00:24 +01:00
parent dffe200b2a
commit 6fc541a44c
4 changed files with 60 additions and 5 deletions

View File

@ -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()', {})

View File

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

View File

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

View File

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