Adding 'json_pp' function in YAQL
* Added new function called 'json_pp' for pretty output json objects using YAQL. It can be used, for example, for sending email containing some dicts or lists. * This function also fixes escaped new-line symbols Change-Id: I8a7d432a957423b832935f46bb04ddd4505258c7
This commit is contained in:
parent
0420bbd86f
commit
b41e297728
@ -111,6 +111,24 @@ class YaqlEvaluatorTest(base.BaseTest):
|
|||||||
self._evaluator.validate,
|
self._evaluator.validate,
|
||||||
{'a': 1})
|
{'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):
|
class InlineYAQLEvaluatorTest(base.BaseTest):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
@ -12,6 +12,8 @@
|
|||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
|
import json
|
||||||
|
|
||||||
import yaql
|
import yaql
|
||||||
|
|
||||||
from mistral.db.v2 import api as db_api
|
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(env_)
|
||||||
yaql_ctx.register_function(execution_)
|
yaql_ctx.register_function(execution_)
|
||||||
yaql_ctx.register_function(task_)
|
yaql_ctx.register_function(task_)
|
||||||
|
yaql_ctx.register_function(json_pp_, name='json_pp')
|
||||||
|
|
||||||
|
|
||||||
# Additional YAQL functions needed by Mistral.
|
# Additional YAQL functions needed by Mistral.
|
||||||
@ -56,6 +59,10 @@ def execution_(context):
|
|||||||
return context['__execution']
|
return context['__execution']
|
||||||
|
|
||||||
|
|
||||||
|
def json_pp_(data):
|
||||||
|
return json.dumps(data, indent=4).replace("\\n", "\n")
|
||||||
|
|
||||||
|
|
||||||
def task_(context, task_name):
|
def task_(context, task_name):
|
||||||
# Importing data_flow in order to break cycle dependency between modules.
|
# Importing data_flow in order to break cycle dependency between modules.
|
||||||
from mistral.workflow import data_flow
|
from mistral.workflow import data_flow
|
||||||
|
Loading…
Reference in New Issue
Block a user