Skip to content

constructor basics #3

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

Merged
merged 2 commits into from
Aug 12, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@
},
"create":{
"__extends":{
"from":"attributes",
"__from":"attributes",
"__excludes":["id"]
}
},
"list":{
"__extends":{
"from":"attributes"
"__from":"attributes"
}
},
"get":{
"__extends":{
"from":"create",
"__from":"create",
"__excludes":[ "id"]
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"feed":{"type":"feed", "required":true, "user":"__user"}
},
"__extends":{
"from":"global.communication.chat.models.message"
"__from":"global.communication.chat.models.message"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@
},
"create":{
"__extends":{
"from":"attributes",
"__from":"attributes",
"__excludes":["id"]
}
},
"list":{
"__extends":{
"from":"attributes"
"__from":"attributes"
}
},
"get":{
"__extends":{
"from":"create",
"__from":"create",
"__excludes":[ "id"]
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@
},
"create":{
"__extends":{
"from":"attributes",
"__from":"attributes",
"__excludes":["id"]
}
},
"list":{
"__extends":{
"from":"attributes"
"__from":"attributes"
}
},
"get":{
"__extends":{
"from":"create",
"__from":"create",
"__excludes":[ "id"]
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"__constructor":{
"user":{
"description":"user to be used"
"type":"str",
"description":"route to user to be used"
}
},
"attributes":{
Expand All @@ -13,18 +14,18 @@
},
"create":{
"__extends":{
"from":"attributes",
"__from":"attributes",
"__excludes":["id"]
}
},
"list":{
"__extends":{
"from":"attributes"
"__from":"attributes"
}
},
"get":{
"__extends":{
"from":"create",
"__from":"create",
"__excludes":[ "id"]
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
"list user conversations":{
"request":{
"__extends":{
"from":"global.utils.models.pagination.request",
"__from":"global.utils.models.pagination.request",
"include":["page", "page_size"]
},
"user" : {"type": "__conversation.__user.attributes.id"}
},
"response":{
"__extends":{
"from":"global.utils.models.pagination.response",
"__from":"global.utils.models.pagination.response",
"include":["total_count", "page_count"]
},
"conversations" : {"many": true, "type": "__conversation.list"}
Expand Down
4 changes: 2 additions & 2 deletions blueprints/global/utils/auth/user/models/user.model.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
"create":{
"request":{
"__extends":{
"from":"attributes",
"__from":"attributes",
"__excludes":["id"]
}
},
"response":{
"__extends":{
"from":"attributes",
"__from":"attributes",
"__includes":["id"]
}
}
Expand Down
2 changes: 1 addition & 1 deletion blueprints/global/utils/models/pagination.model.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"response":{
"__extends":{
"from":"request"
"__from":"request"
},
"total_count":{ "type":"int", "min":0},
"page_count":{ "type":"int", "min":0},
Expand Down
3 changes: 2 additions & 1 deletion compiler/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ def main():

main()
# python .\compiler.py c "json.v1" ../blueprints/examples/facebook/main.app.json
# python .\compiler.py c "json.v1" ./sample_blueprints/samples/import.app.json
# python .\compiler.py c "json.v1" ./sample_blueprints/samples/import.app.json
# python .\compiler.py c "json.v1" ./sample_blueprints/samples/construct.app.json
133 changes: 91 additions & 42 deletions compiler/json_compiler/Compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,100 @@
SPECIAL_FIELD_FLAG = "__"
EXTENDS_FIELD = SPECIAL_FIELD_FLAG+"extends"


def set_type(object, to_type):
# TODO: implement type serialization
return object
def load_json_as_dict(file_name):
with open(file_name, "r") as f:
return json.load(f)
def special_flags_processing(json_dict, args = {}, *, base_folder=None, base_dict={}, object_route=""):
if SPECIAL_FIELD_FLAG+"constructor" in json_dict:
json_dict.pop(SPECIAL_FIELD_FLAG+"constructor")
if SPECIAL_FIELD_FLAG+"extends" in json_dict:
extends_from = json_dict[SPECIAL_FIELD_FLAG+"extends"]["from"].split(".")
attr_build = {}
if len(extends_from) == 1:
if extends_from[0] in base_dict:
attr_build = special_flags_processing(base_dict[extends_from[0]], base_dict=base_dict, object_route = object_route+"."+extends_from[0])
def extends(json_dict, *, base_folder=None, base_dict={}, object_route=""):
extends_from = json_dict[SPECIAL_FIELD_FLAG+"extends"][SPECIAL_FIELD_FLAG+"from"].split(".")
attr_build = {}
if len(extends_from) == 1:
new_route = ".".join(object_route.split(".")+[extends_from[0]])
if extends_from[0] in base_dict:
attr_build = special_flags_processing(base_dict[extends_from[0]], base_dict=base_dict, object_route = new_route)
else:
CustomLogging.error(f"Attribute {extends_from[0]} not found \n{base_dict}")
else:
attr_file_name = search_json(json_dict[SPECIAL_FIELD_FLAG+"extends"][SPECIAL_FIELD_FLAG+"from"], base_folder=base_folder)
if not attr_file_name:
CustomLogging.error(f"{json_dict[SPECIAL_FIELD_FLAG+'extends'][SPECIAL_FIELD_FLAG+'from']} path does not exists in")
attr_json = load_json_as_dict(attr_file_name)
attr_build = json_global_compile(
attr_json,
args = json_dict[SPECIAL_FIELD_FLAG+"extends"],
base_folder = os.path.dirname(attr_file_name),
object_route=json_dict[SPECIAL_FIELD_FLAG+"extends"][SPECIAL_FIELD_FLAG+"from"]
)
is_excluding = SPECIAL_FIELD_FLAG+"excludes" in json_dict[SPECIAL_FIELD_FLAG+"extends"]
is_including = SPECIAL_FIELD_FLAG+"includes" in json_dict[SPECIAL_FIELD_FLAG+"extends"]
if is_including and is_excluding:
CustomLogging.error("can not use excludes and includes in a same block")
if is_including:
new_attr_build = {}
for include in json_dict[SPECIAL_FIELD_FLAG+"extends"][SPECIAL_FIELD_FLAG+"includes"]:
if include not in attr_build:
CustomLogging.error(f"{object_route} include error: attribute {include} not in {json_dict[SPECIAL_FIELD_FLAG+'extends'][SPECIAL_FIELD_FLAG+'from']}")
new_attr_build[include] = attr_build[include]
attr_build = new_attr_build
if is_excluding:
for exclude in json_dict[SPECIAL_FIELD_FLAG+"extends"][SPECIAL_FIELD_FLAG+"excludes"]:
if exclude in attr_build:
attr_build.pop(exclude)
else:
CustomLogging.error(f"Attribute not found {object_route}.{extends_from[0]}\n{base_dict}")
CustomLogging.warning(f"exclude error: attribute {exclude} not in {json_dict[SPECIAL_FIELD_FLAG+'extends'][SPECIAL_FIELD_FLAG+'from']}")
json_dict.update(attr_build)
json_dict.pop(SPECIAL_FIELD_FLAG+"extends")
return json_dict
def cosntruct_replace(main_object, arg_replace, value):
if type(main_object) == str:
return main_object.replace(arg_replace, value)
response_json = {}
for attribute in main_object:
attribute_new_name = attribute.replace(arg_replace, value)
attribute_new_value = main_object[attribute]
if type(attribute_new_value) == dict:
attribute_new_value = cosntruct_replace(attribute_new_value, arg_replace, value)
elif type(attribute_new_value) == list:
attribute_new_value = [ cosntruct_replace(element, arg_replace, value) for element in attribute_new_value ]
response_json[attribute_new_name] = attribute_new_value
return response_json
def cosntructor(json_dict, *, args = {}, object_route=""):
constructor_dict = json_dict.pop(SPECIAL_FIELD_FLAG+"constructor")
response_json = copy.deepcopy(json_dict)
args_to_check = copy.deepcopy(args)
args_to_check.pop(SPECIAL_FIELD_FLAG+"from")
if SPECIAL_FIELD_FLAG+"excludes" in args_to_check:
args_to_check.pop(SPECIAL_FIELD_FLAG+"excludes")
if SPECIAL_FIELD_FLAG+"includes" in args_to_check:
args_to_check.pop(SPECIAL_FIELD_FLAG+"includes")
for arg_to_check in args_to_check:
if arg_to_check not in constructor_dict:
CustomLogging.warning(f"error in constructor: invalid parameter {arg_to_check}")
else:
attr_file_name = search_json(json_dict[SPECIAL_FIELD_FLAG+"extends"]["from"], base_folder=base_folder)
if not attr_file_name:
CustomLogging.error(f"{json_dict[SPECIAL_FIELD_FLAG+'extends']['from']} path does not exists in")
attr_json = load_json_as_dict(attr_file_name)
attr_build = json_global_compile(attr_json, base_folder = os.path.dirname(attr_file_name),object_route=json_dict[SPECIAL_FIELD_FLAG+"extends"]["from"])
is_excluding = SPECIAL_FIELD_FLAG+"excludes" in json_dict[SPECIAL_FIELD_FLAG+"extends"]
is_including = SPECIAL_FIELD_FLAG+"includes" in json_dict[SPECIAL_FIELD_FLAG+"extends"]
if is_including and is_excluding:
CustomLogging.error("can not use excludes and includes in a same block")
if is_including:
new_attr_build = {}
for include in json_dict[SPECIAL_FIELD_FLAG+"extends"][SPECIAL_FIELD_FLAG+"includes"]:
if include not in attr_build:
CustomLogging.error(f"include error: attribute {include} not in {json_dict[SPECIAL_FIELD_FLAG+'extends']['from']}")
new_attr_build[include] = attr_build[include]
attr_build = new_attr_build
if is_excluding:
for exclude in json_dict[SPECIAL_FIELD_FLAG+"extends"][SPECIAL_FIELD_FLAG+"excludes"]:
if exclude in attr_build:
attr_build.pop(exclude)
else:
CustomLogging.warning(f"exclude error: attribute {exclude} not in {json_dict[SPECIAL_FIELD_FLAG+'extends']['from']}")
json_dict.update(attr_build)
json_dict.pop(SPECIAL_FIELD_FLAG+"extends")
constructor_dict.pop(arg_to_check) # remove argument so we know it already was defined
for default_attribute in constructor_dict:
if default_attribute.startswith(SPECIAL_FIELD_FLAG):
continue
args_to_check[default_attribute] = set_type(constructor_dict[default_attribute]["default"], constructor_dict[default_attribute]["type"])
for arg in args_to_check:
new_value = cosntruct_replace(response_json, SPECIAL_FIELD_FLAG+arg, args_to_check[arg])
response_json = new_value
return response_json
def special_flags_processing(json_dict, *, args = {}, base_folder=None, base_dict={}, object_route=""):
if SPECIAL_FIELD_FLAG+"constructor" in json_dict:
json_dict = cosntructor(json_dict, args=args)
base_dict = copy.deepcopy(json_dict)
if SPECIAL_FIELD_FLAG+"extends" in json_dict:
json_dict = extends(json_dict, base_folder=base_folder, base_dict=base_dict, object_route=object_route)
for attribute in json_dict:
if type(json_dict[attribute]) == dict:
json_dict[attribute] = special_flags_processing(json_dict[attribute], args, base_folder=base_folder, base_dict=base_dict, object_route=f"{object_route}.{attribute}")
json_dict[attribute] = special_flags_processing(json_dict[attribute], args=args, base_folder=base_folder, base_dict=base_dict, object_route=f"{object_route}.{attribute}")
return copy.deepcopy(json_dict)

def json_global_compile(json_dict, args = {}, *, base_folder=None, base_dict={}, object_route= ""):
data = special_flags_processing(json_dict, args, base_folder=base_folder, base_dict=json_dict, object_route=object_route)
def json_global_compile(json_dict, *, args = {}, base_folder=None, base_dict={}, object_route= ""):
data = special_flags_processing(json_dict, args=args, base_folder=base_folder, base_dict=json_dict, object_route=object_route)
return data
class Compiler:
blueprint: dict = {}
Expand All @@ -68,18 +115,20 @@ def __init__(self, main_file) -> None:
def compile_models(self):
if MODELS_FIELD not in self.blueprint and EXTENDS_FIELD not in self.blueprint:
CustomLogging.error("models is not defined")
build = json_global_compile(self.blueprint)
build = json_global_compile(self.blueprint, base_folder=self.main_folder)
for model in build["models"].copy():
model_file_name = self.main_file
if type(model) == str:
if type(build["models"][model]) == str:
model_file_name = search_json(
build["models"][model], base_folder=self.main_folder)
if not model_file_name:
CustomLogging.error(build["models"][model], "path does not exists in")
continue
model_json = load_json_as_dict(model_file_name)
elif type(model) == dict:
model_json = model
elif type(build["models"][model]) == dict:
model_json = build["models"][model]
else:
CustomLogging.error(f"invalid model {model}")
model_build = json_global_compile(model_json, base_folder = os.path.dirname(model_file_name), object_route=model)
build["models"][model] = model_build
pp = pprint.PrettyPrinter(indent=2)
Expand Down
Loading