Skip to content

Adding json type to task variables #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions camunda/variables/tests/test_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,11 @@ def test_to_dict_returns_variables_as_dict(self):
"var2": {"value": True},
"var3": {"value": "string"}})
self.assertDictEqual({"var1": 1, "var2": True, "var3": "string"}, variables.to_dict())

def test_json_returns_variables_as_json(self):
variables = {"list": ["a", "b", "c"], "obj": [{"z":2, "h":3}, {"t":5, "s":9}], "zobj": {"az": [0, 1, 2], "zot": [True, False, False]}}
formatted_vars = Variables.format(variables)
self.assertDictEqual({"list": {"type": "json", "value": "[\"a\", \"b\", \"c\"]"},
"obj": {"type": "json", "value": "[{\"z\": 2, \"h\": 3}, {\"t\": 5, \"s\": 9}]"},
"zobj": {"type": "json", "value": "{\"az\": [0, 1, 2], \"zot\": [true, false, false]}"}}, formatted_vars)

16 changes: 11 additions & 5 deletions camunda/variables/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ def format(cls, variables):
"""
formatted_vars = {}
if variables:
formatted_vars = {
k: v if isinstance(v, dict) else {"value": v}
for k, v in variables.items()
}
for i in variables.keys():
if type(variables[i]) in [bool, int, float, str]:
formatted_vars[i] = {"value": variables[i]}
elif type(variables[i]) == dict and "value" in variables[i] and type(variables[i]['value']) in [bool, int, float, str]:
formatted_vars[i] = variables[i]
else:
formatted_vars[i] = {"value": json.dumps(variables[i]), "type": "json"}
return formatted_vars

def to_dict(self):
Expand All @@ -41,5 +44,8 @@ def to_dict(self):
"""
result = {}
for k, v in self.variables.items():
result[k] = v["value"]
if 'type' in v and v['type'] == "Json":
result[k] = json.loads(v["value"])
else:
result[k] = v["value"]
return result