From 6bd96d70b15ef7c76c98822dec14f661752a626c Mon Sep 17 00:00:00 2001 From: "Brad P. Crochet" Date: Thu, 12 Oct 2017 13:42:08 -0400 Subject: [PATCH] Add yaml and json parsing functions Sometimes you'll get a string back from some action (like swift get_object) and it will be in either a yaml or json format. These functions will allow you to parse those into a useful object. Change-Id: I375219f4b019319e1b3d756dca512f7f90cd097f --- mistral/utils/expression_utils.py | 9 +++++++++ releasenotes/notes/yaml-json-parse-53217627a647dc1d.yaml | 5 +++++ setup.cfg | 2 ++ 3 files changed, 16 insertions(+) create mode 100644 releasenotes/notes/yaml-json-parse-53217627a647dc1d.yaml diff --git a/mistral/utils/expression_utils.py b/mistral/utils/expression_utils.py index a0dd5ca18..e5aefd573 100644 --- a/mistral/utils/expression_utils.py +++ b/mistral/utils/expression_utils.py @@ -17,6 +17,7 @@ from functools import partial from oslo_serialization import jsonutils from stevedore import extension +import yaml import yaql from mistral.db.v2 import api as db_api @@ -271,3 +272,11 @@ def global_(context, var_name): wf_ex = db_api.get_workflow_execution(context['__execution']['id']) return wf_ex.context.get(var_name) + + +def json_parse_(context, data): + return jsonutils.loads(data) + + +def yaml_parse_(context, data): + return yaml.safe_load(data) diff --git a/releasenotes/notes/yaml-json-parse-53217627a647dc1d.yaml b/releasenotes/notes/yaml-json-parse-53217627a647dc1d.yaml new file mode 100644 index 000000000..05db48cfc --- /dev/null +++ b/releasenotes/notes/yaml-json-parse-53217627a647dc1d.yaml @@ -0,0 +1,5 @@ +--- +features: + - | + Add yaml_parse and json_parse expression functions. Each accepts a string + and will parse as either json or yaml, and return an object. diff --git a/setup.cfg b/setup.cfg index 6615b5da2..fff40150c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -83,6 +83,8 @@ mistral.expression.functions = 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 = yaql = mistral.expressions.yaql_expression:InlineYAQLEvaluator