-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
var separate syntax for variable (#348)
* Feat: support var * support variable templating and randomGenerations
- Loading branch information
Showing
24 changed files
with
592 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,67 @@ | ||
import json | ||
from typing import Dict, List, Union | ||
from typing import Dict, List, Optional, Union | ||
|
||
from ..utils.property_util import PropertyProvider, get_no_replace_property_provider | ||
from ..utils.common import triple_or_double_tostring | ||
|
||
|
||
def json_or_array_to_json(model, update_content_func) -> Union[Dict, List]: | ||
if isinstance(model, Dict) or isinstance(model, List): | ||
# TODO | ||
# this is bad | ||
# hooking here could lead to other issues | ||
return model | ||
if array := model.array: | ||
return [jsonmodel_to_json(value, update_content_func) for value in array.values] | ||
elif json_object := model.object: | ||
return { | ||
# TODO i'm confused about key weather it should be string or int or float (value has float, number, | ||
# boolean, null) but key is unsupported by requests | ||
get_key(member, update_content_func): jsonmodel_to_json( | ||
member.value, update_content_func | ||
) | ||
for member in json_object.members | ||
} | ||
class JsonParser: | ||
def __init__(self, property_util: PropertyProvider): | ||
self.property_util = property_util | ||
|
||
def json_or_array_to_json(self, model) -> Union[Dict, List]: | ||
if isinstance(model, Dict) or isinstance(model, List): | ||
return model | ||
if array := model.array: | ||
return [self.jsonmodel_to_json(value) for value in array.values] | ||
elif json_object := model.object: | ||
return { | ||
self.get_key(member): self.jsonmodel_to_json(member.value) | ||
for member in json_object.members | ||
} | ||
|
||
def get_key(member, update_content_func): | ||
if member.key: | ||
return triple_or_double_tostring(member.key, update_content_func) | ||
elif member.var: | ||
return update_content_func(member.var) | ||
def get_key(self, member): | ||
if member.key: | ||
return triple_or_double_tostring(member.key, self.property_util.get_updated_content) | ||
elif member.var: | ||
return self.property_util.get_updated_obj_content(member.var) | ||
|
||
def jsonmodel_to_json(self, model): | ||
if str_value := model.strs: | ||
return triple_or_double_tostring(str_value, self.property_util.get_updated_content) | ||
elif var_value := model.var: | ||
return self.property_util.get_updated_obj_content(var_value) | ||
elif int_val := model.int: | ||
return int_val.value | ||
elif flt := model.flt: | ||
return flt.value | ||
elif bl := model.bl: | ||
return bl.value | ||
elif json_object := model.object: | ||
return { | ||
self.get_key(member): self.jsonmodel_to_json(member.value) | ||
for member in json_object.members | ||
} | ||
elif array := model.array: | ||
return [self.jsonmodel_to_json(value) for value in array.values] | ||
elif model == "null": | ||
return None | ||
elif expr := model.expr: | ||
return eval(expr) | ||
|
||
def jsonmodel_to_json(model, update_content_func): | ||
# if length if array is 0, which means, its not string | ||
if str_value := model.strs: | ||
return triple_or_double_tostring(str_value, update_content_func) | ||
elif var_value := model.var: | ||
return get_json_data(var_value, update_content_func) | ||
elif int_val := model.int: | ||
return int_val.value | ||
elif flt := model.flt: | ||
return flt.value | ||
elif bl := model.bl: | ||
return bl.value | ||
elif json_object := model.object: | ||
return { | ||
get_key(member, update_content_func): jsonmodel_to_json( | ||
member.value, update_content_func | ||
) | ||
for member in json_object.members | ||
} | ||
elif array := model.array: | ||
return [jsonmodel_to_json(value, update_content_func) for value in array.values] | ||
elif model == "null": | ||
return None | ||
elif expr := model.expr: | ||
return eval(expr) | ||
|
||
# Supporting function | ||
def json_or_array_to_json(model, property_util: Optional[PropertyProvider]=None) -> Union[Dict, List]: | ||
if property_util is None: | ||
# This is a hack to ignore replacement of variables where it is not needed | ||
property_util = get_no_replace_property_provider() | ||
parser = JsonParser(property_util) | ||
return parser.json_or_array_to_json(model) | ||
|
||
def get_json_data(var_value, update_content_func): | ||
content: str = update_content_func(var_value) | ||
if content == var_value: | ||
return var_value | ||
try: | ||
return json.loads(content) | ||
except ValueError: | ||
return content | ||
|
||
def jsonmodel_to_json(model, property_util: Optional[PropertyProvider]=None) -> Union[Dict, List]: | ||
if property_util is None: | ||
# This is a hack to ignore replacement of variables where it is not needed | ||
property_util = get_no_replace_property_provider() | ||
parser = JsonParser(property_util) | ||
return parser.jsonmodel_to_json(model) |
Oops, something went wrong.