Skip to content

Commit 88a2cf1

Browse files
Merge pull request #3 from unknowncoder05/dev
constructor basics
2 parents 9a5a932 + 06533c4 commit 88a2cf1

File tree

13 files changed

+265
-62
lines changed

13 files changed

+265
-62
lines changed

blueprints/global/communication/chat/feed/models/feed.model.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@
1010
},
1111
"create":{
1212
"__extends":{
13-
"from":"attributes",
13+
"__from":"attributes",
1414
"__excludes":["id"]
1515
}
1616
},
1717
"list":{
1818
"__extends":{
19-
"from":"attributes"
19+
"__from":"attributes"
2020
}
2121
},
2222
"get":{
2323
"__extends":{
24-
"from":"create",
24+
"__from":"create",
2525
"__excludes":[ "id"]
2626
}
2727
}

blueprints/global/communication/chat/feed/models/message.model.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
"feed":{"type":"feed", "required":true, "user":"__user"}
99
},
1010
"__extends":{
11-
"from":"global.communication.chat.models.message"
11+
"__from":"global.communication.chat.models.message"
1212
}
1313
}

blueprints/global/communication/chat/feed/models/reaction.model.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@
1111
},
1212
"create":{
1313
"__extends":{
14-
"from":"attributes",
14+
"__from":"attributes",
1515
"__excludes":["id"]
1616
}
1717
},
1818
"list":{
1919
"__extends":{
20-
"from":"attributes"
20+
"__from":"attributes"
2121
}
2222
},
2323
"get":{
2424
"__extends":{
25-
"from":"create",
25+
"__from":"create",
2626
"__excludes":[ "id"]
2727
}
2828
}

blueprints/global/communication/chat/models/conversation.model.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@
1212
},
1313
"create":{
1414
"__extends":{
15-
"from":"attributes",
15+
"__from":"attributes",
1616
"__excludes":["id"]
1717
}
1818
},
1919
"list":{
2020
"__extends":{
21-
"from":"attributes"
21+
"__from":"attributes"
2222
}
2323
},
2424
"get":{
2525
"__extends":{
26-
"from":"create",
26+
"__from":"create",
2727
"__excludes":[ "id"]
2828
}
2929
}

blueprints/global/communication/chat/models/message.model.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"__constructor":{
33
"user":{
4-
"description":"user to be used"
4+
"type":"str",
5+
"description":"route to user to be used"
56
}
67
},
78
"attributes":{
@@ -13,18 +14,18 @@
1314
},
1415
"create":{
1516
"__extends":{
16-
"from":"attributes",
17+
"__from":"attributes",
1718
"__excludes":["id"]
1819
}
1920
},
2021
"list":{
2122
"__extends":{
22-
"from":"attributes"
23+
"__from":"attributes"
2324
}
2425
},
2526
"get":{
2627
"__extends":{
27-
"from":"create",
28+
"__from":"create",
2829
"__excludes":[ "id"]
2930
}
3031
}

blueprints/global/communication/chat/services/conversation.service.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
"list user conversations":{
2020
"request":{
2121
"__extends":{
22-
"from":"global.utils.models.pagination.request",
22+
"__from":"global.utils.models.pagination.request",
2323
"include":["page", "page_size"]
2424
},
2525
"user" : {"type": "__conversation.__user.attributes.id"}
2626
},
2727
"response":{
2828
"__extends":{
29-
"from":"global.utils.models.pagination.response",
29+
"__from":"global.utils.models.pagination.response",
3030
"include":["total_count", "page_count"]
3131
},
3232
"conversations" : {"many": true, "type": "__conversation.list"}

blueprints/global/utils/auth/user/models/user.model.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
"create":{
99
"request":{
1010
"__extends":{
11-
"from":"attributes",
11+
"__from":"attributes",
1212
"__excludes":["id"]
1313
}
1414
},
1515
"response":{
1616
"__extends":{
17-
"from":"attributes",
17+
"__from":"attributes",
1818
"__includes":["id"]
1919
}
2020
}

blueprints/global/utils/models/pagination.model.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
},
77
"response":{
88
"__extends":{
9-
"from":"request"
9+
"__from":"request"
1010
},
1111
"total_count":{ "type":"int", "min":0},
1212
"page_count":{ "type":"int", "min":0},

compiler/compiler.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ def main():
1919

2020
main()
2121
# python .\compiler.py c "json.v1" ../blueprints/examples/facebook/main.app.json
22-
# python .\compiler.py c "json.v1" ./sample_blueprints/samples/import.app.json
22+
# python .\compiler.py c "json.v1" ./sample_blueprints/samples/import.app.json
23+
# python .\compiler.py c "json.v1" ./sample_blueprints/samples/construct.app.json

compiler/json_compiler/Compiler.py

Lines changed: 91 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,53 +9,100 @@
99
SPECIAL_FIELD_FLAG = "__"
1010
EXTENDS_FIELD = SPECIAL_FIELD_FLAG+"extends"
1111

12-
12+
def set_type(object, to_type):
13+
# TODO: implement type serialization
14+
return object
1315
def load_json_as_dict(file_name):
1416
with open(file_name, "r") as f:
1517
return json.load(f)
16-
def special_flags_processing(json_dict, args = {}, *, base_folder=None, base_dict={}, object_route=""):
17-
if SPECIAL_FIELD_FLAG+"constructor" in json_dict:
18-
json_dict.pop(SPECIAL_FIELD_FLAG+"constructor")
19-
if SPECIAL_FIELD_FLAG+"extends" in json_dict:
20-
extends_from = json_dict[SPECIAL_FIELD_FLAG+"extends"]["from"].split(".")
21-
attr_build = {}
22-
if len(extends_from) == 1:
23-
if extends_from[0] in base_dict:
24-
attr_build = special_flags_processing(base_dict[extends_from[0]], base_dict=base_dict, object_route = object_route+"."+extends_from[0])
18+
def extends(json_dict, *, base_folder=None, base_dict={}, object_route=""):
19+
extends_from = json_dict[SPECIAL_FIELD_FLAG+"extends"][SPECIAL_FIELD_FLAG+"from"].split(".")
20+
attr_build = {}
21+
if len(extends_from) == 1:
22+
new_route = ".".join(object_route.split(".")+[extends_from[0]])
23+
if extends_from[0] in base_dict:
24+
attr_build = special_flags_processing(base_dict[extends_from[0]], base_dict=base_dict, object_route = new_route)
25+
else:
26+
CustomLogging.error(f"Attribute {extends_from[0]} not found \n{base_dict}")
27+
else:
28+
attr_file_name = search_json(json_dict[SPECIAL_FIELD_FLAG+"extends"][SPECIAL_FIELD_FLAG+"from"], base_folder=base_folder)
29+
if not attr_file_name:
30+
CustomLogging.error(f"{json_dict[SPECIAL_FIELD_FLAG+'extends'][SPECIAL_FIELD_FLAG+'from']} path does not exists in")
31+
attr_json = load_json_as_dict(attr_file_name)
32+
attr_build = json_global_compile(
33+
attr_json,
34+
args = json_dict[SPECIAL_FIELD_FLAG+"extends"],
35+
base_folder = os.path.dirname(attr_file_name),
36+
object_route=json_dict[SPECIAL_FIELD_FLAG+"extends"][SPECIAL_FIELD_FLAG+"from"]
37+
)
38+
is_excluding = SPECIAL_FIELD_FLAG+"excludes" in json_dict[SPECIAL_FIELD_FLAG+"extends"]
39+
is_including = SPECIAL_FIELD_FLAG+"includes" in json_dict[SPECIAL_FIELD_FLAG+"extends"]
40+
if is_including and is_excluding:
41+
CustomLogging.error("can not use excludes and includes in a same block")
42+
if is_including:
43+
new_attr_build = {}
44+
for include in json_dict[SPECIAL_FIELD_FLAG+"extends"][SPECIAL_FIELD_FLAG+"includes"]:
45+
if include not in attr_build:
46+
CustomLogging.error(f"{object_route} include error: attribute {include} not in {json_dict[SPECIAL_FIELD_FLAG+'extends'][SPECIAL_FIELD_FLAG+'from']}")
47+
new_attr_build[include] = attr_build[include]
48+
attr_build = new_attr_build
49+
if is_excluding:
50+
for exclude in json_dict[SPECIAL_FIELD_FLAG+"extends"][SPECIAL_FIELD_FLAG+"excludes"]:
51+
if exclude in attr_build:
52+
attr_build.pop(exclude)
2553
else:
26-
CustomLogging.error(f"Attribute not found {object_route}.{extends_from[0]}\n{base_dict}")
54+
CustomLogging.warning(f"exclude error: attribute {exclude} not in {json_dict[SPECIAL_FIELD_FLAG+'extends'][SPECIAL_FIELD_FLAG+'from']}")
55+
json_dict.update(attr_build)
56+
json_dict.pop(SPECIAL_FIELD_FLAG+"extends")
57+
return json_dict
58+
def cosntruct_replace(main_object, arg_replace, value):
59+
if type(main_object) == str:
60+
return main_object.replace(arg_replace, value)
61+
response_json = {}
62+
for attribute in main_object:
63+
attribute_new_name = attribute.replace(arg_replace, value)
64+
attribute_new_value = main_object[attribute]
65+
if type(attribute_new_value) == dict:
66+
attribute_new_value = cosntruct_replace(attribute_new_value, arg_replace, value)
67+
elif type(attribute_new_value) == list:
68+
attribute_new_value = [ cosntruct_replace(element, arg_replace, value) for element in attribute_new_value ]
69+
response_json[attribute_new_name] = attribute_new_value
70+
return response_json
71+
def cosntructor(json_dict, *, args = {}, object_route=""):
72+
constructor_dict = json_dict.pop(SPECIAL_FIELD_FLAG+"constructor")
73+
response_json = copy.deepcopy(json_dict)
74+
args_to_check = copy.deepcopy(args)
75+
args_to_check.pop(SPECIAL_FIELD_FLAG+"from")
76+
if SPECIAL_FIELD_FLAG+"excludes" in args_to_check:
77+
args_to_check.pop(SPECIAL_FIELD_FLAG+"excludes")
78+
if SPECIAL_FIELD_FLAG+"includes" in args_to_check:
79+
args_to_check.pop(SPECIAL_FIELD_FLAG+"includes")
80+
for arg_to_check in args_to_check:
81+
if arg_to_check not in constructor_dict:
82+
CustomLogging.warning(f"error in constructor: invalid parameter {arg_to_check}")
2783
else:
28-
attr_file_name = search_json(json_dict[SPECIAL_FIELD_FLAG+"extends"]["from"], base_folder=base_folder)
29-
if not attr_file_name:
30-
CustomLogging.error(f"{json_dict[SPECIAL_FIELD_FLAG+'extends']['from']} path does not exists in")
31-
attr_json = load_json_as_dict(attr_file_name)
32-
attr_build = json_global_compile(attr_json, base_folder = os.path.dirname(attr_file_name),object_route=json_dict[SPECIAL_FIELD_FLAG+"extends"]["from"])
33-
is_excluding = SPECIAL_FIELD_FLAG+"excludes" in json_dict[SPECIAL_FIELD_FLAG+"extends"]
34-
is_including = SPECIAL_FIELD_FLAG+"includes" in json_dict[SPECIAL_FIELD_FLAG+"extends"]
35-
if is_including and is_excluding:
36-
CustomLogging.error("can not use excludes and includes in a same block")
37-
if is_including:
38-
new_attr_build = {}
39-
for include in json_dict[SPECIAL_FIELD_FLAG+"extends"][SPECIAL_FIELD_FLAG+"includes"]:
40-
if include not in attr_build:
41-
CustomLogging.error(f"include error: attribute {include} not in {json_dict[SPECIAL_FIELD_FLAG+'extends']['from']}")
42-
new_attr_build[include] = attr_build[include]
43-
attr_build = new_attr_build
44-
if is_excluding:
45-
for exclude in json_dict[SPECIAL_FIELD_FLAG+"extends"][SPECIAL_FIELD_FLAG+"excludes"]:
46-
if exclude in attr_build:
47-
attr_build.pop(exclude)
48-
else:
49-
CustomLogging.warning(f"exclude error: attribute {exclude} not in {json_dict[SPECIAL_FIELD_FLAG+'extends']['from']}")
50-
json_dict.update(attr_build)
51-
json_dict.pop(SPECIAL_FIELD_FLAG+"extends")
84+
constructor_dict.pop(arg_to_check) # remove argument so we know it already was defined
85+
for default_attribute in constructor_dict:
86+
if default_attribute.startswith(SPECIAL_FIELD_FLAG):
87+
continue
88+
args_to_check[default_attribute] = set_type(constructor_dict[default_attribute]["default"], constructor_dict[default_attribute]["type"])
89+
for arg in args_to_check:
90+
new_value = cosntruct_replace(response_json, SPECIAL_FIELD_FLAG+arg, args_to_check[arg])
91+
response_json = new_value
92+
return response_json
93+
def special_flags_processing(json_dict, *, args = {}, base_folder=None, base_dict={}, object_route=""):
94+
if SPECIAL_FIELD_FLAG+"constructor" in json_dict:
95+
json_dict = cosntructor(json_dict, args=args)
96+
base_dict = copy.deepcopy(json_dict)
97+
if SPECIAL_FIELD_FLAG+"extends" in json_dict:
98+
json_dict = extends(json_dict, base_folder=base_folder, base_dict=base_dict, object_route=object_route)
5299
for attribute in json_dict:
53100
if type(json_dict[attribute]) == dict:
54-
json_dict[attribute] = special_flags_processing(json_dict[attribute], args, base_folder=base_folder, base_dict=base_dict, object_route=f"{object_route}.{attribute}")
101+
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}")
55102
return copy.deepcopy(json_dict)
56103

57-
def json_global_compile(json_dict, args = {}, *, base_folder=None, base_dict={}, object_route= ""):
58-
data = special_flags_processing(json_dict, args, base_folder=base_folder, base_dict=json_dict, object_route=object_route)
104+
def json_global_compile(json_dict, *, args = {}, base_folder=None, base_dict={}, object_route= ""):
105+
data = special_flags_processing(json_dict, args=args, base_folder=base_folder, base_dict=json_dict, object_route=object_route)
59106
return data
60107
class Compiler:
61108
blueprint: dict = {}
@@ -68,18 +115,20 @@ def __init__(self, main_file) -> None:
68115
def compile_models(self):
69116
if MODELS_FIELD not in self.blueprint and EXTENDS_FIELD not in self.blueprint:
70117
CustomLogging.error("models is not defined")
71-
build = json_global_compile(self.blueprint)
118+
build = json_global_compile(self.blueprint, base_folder=self.main_folder)
72119
for model in build["models"].copy():
73120
model_file_name = self.main_file
74-
if type(model) == str:
121+
if type(build["models"][model]) == str:
75122
model_file_name = search_json(
76123
build["models"][model], base_folder=self.main_folder)
77124
if not model_file_name:
78125
CustomLogging.error(build["models"][model], "path does not exists in")
79126
continue
80127
model_json = load_json_as_dict(model_file_name)
81-
elif type(model) == dict:
82-
model_json = model
128+
elif type(build["models"][model]) == dict:
129+
model_json = build["models"][model]
130+
else:
131+
CustomLogging.error(f"invalid model {model}")
83132
model_build = json_global_compile(model_json, base_folder = os.path.dirname(model_file_name), object_route=model)
84133
build["models"][model] = model_build
85134
pp = pprint.PrettyPrinter(indent=2)

0 commit comments

Comments
 (0)