Skip to content

Commit 692e23b

Browse files
basic construct functionalities
1 parent 9c5fb52 commit 692e23b

File tree

13 files changed

+252
-60
lines changed

13 files changed

+252
-60
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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@
1313
},
1414
"create":{
1515
"__extends":{
16-
"from":"attributes",
16+
"__from":"attributes",
1717
"__excludes":["id"]
1818
}
1919
},
2020
"list":{
2121
"__extends":{
22-
"from":"attributes"
22+
"__from":"attributes"
2323
}
2424
},
2525
"get":{
2626
"__extends":{
27-
"from":"create",
27+
"__from":"create",
2828
"__excludes":[ "id"]
2929
}
3030
}

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: 79 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,49 +13,85 @@
1313
def load_json_as_dict(file_name):
1414
with open(file_name, "r") as f:
1515
return json.load(f)
16-
def special_flags_processing(json_dict, args = {}, *, base_folder=None, base_dict={}, object_route=""):
16+
def extends(json_dict, *, base_folder=None, base_dict={}, object_route=""):
17+
extends_from = json_dict[SPECIAL_FIELD_FLAG+"extends"][SPECIAL_FIELD_FLAG+"from"].split(".")
18+
attr_build = {}
19+
if len(extends_from) == 1:
20+
new_route = ".".join(object_route.split(".")+[extends_from[0]])
21+
if extends_from[0] in base_dict:
22+
attr_build = special_flags_processing(base_dict[extends_from[0]], base_dict=base_dict, object_route = new_route)
23+
else:
24+
CustomLogging.error(f"Attribute {extends_from[0]} not found \n{base_dict}")
25+
else:
26+
attr_file_name = search_json(json_dict[SPECIAL_FIELD_FLAG+"extends"][SPECIAL_FIELD_FLAG+"from"], base_folder=base_folder)
27+
if not attr_file_name:
28+
CustomLogging.error(f"{json_dict[SPECIAL_FIELD_FLAG+'extends'][SPECIAL_FIELD_FLAG+'from']} path does not exists in")
29+
attr_json = load_json_as_dict(attr_file_name)
30+
attr_build = json_global_compile(
31+
attr_json,
32+
args = json_dict[SPECIAL_FIELD_FLAG+"extends"],
33+
base_folder = os.path.dirname(attr_file_name),
34+
object_route=json_dict[SPECIAL_FIELD_FLAG+"extends"][SPECIAL_FIELD_FLAG+"from"]
35+
)
36+
is_excluding = SPECIAL_FIELD_FLAG+"excludes" in json_dict[SPECIAL_FIELD_FLAG+"extends"]
37+
is_including = SPECIAL_FIELD_FLAG+"includes" in json_dict[SPECIAL_FIELD_FLAG+"extends"]
38+
if is_including and is_excluding:
39+
CustomLogging.error("can not use excludes and includes in a same block")
40+
if is_including:
41+
new_attr_build = {}
42+
for include in json_dict[SPECIAL_FIELD_FLAG+"extends"][SPECIAL_FIELD_FLAG+"includes"]:
43+
if include not in attr_build:
44+
CustomLogging.error(f"{object_route} include error: attribute {include} not in {json_dict[SPECIAL_FIELD_FLAG+'extends'][SPECIAL_FIELD_FLAG+'from']}")
45+
new_attr_build[include] = attr_build[include]
46+
attr_build = new_attr_build
47+
if is_excluding:
48+
for exclude in json_dict[SPECIAL_FIELD_FLAG+"extends"][SPECIAL_FIELD_FLAG+"excludes"]:
49+
if exclude in attr_build:
50+
attr_build.pop(exclude)
51+
else:
52+
CustomLogging.warning(f"exclude error: attribute {exclude} not in {json_dict[SPECIAL_FIELD_FLAG+'extends'][SPECIAL_FIELD_FLAG+'from']}")
53+
json_dict.update(attr_build)
54+
json_dict.pop(SPECIAL_FIELD_FLAG+"extends")
55+
return json_dict
56+
def cosntruct_replace(main_object, arg_replace, value):
57+
if type(main_object) == str:
58+
return main_object.replace(arg_replace, value)
59+
response_json = {}
60+
for attribute in main_object:
61+
attribute_new_name = attribute.replace(arg_replace, value)
62+
attribute_new_value = main_object[attribute]
63+
if type(attribute_new_value) == dict:
64+
attribute_new_value = cosntruct_replace(attribute_new_value, arg_replace, value)
65+
elif type(attribute_new_value) == list:
66+
attribute_new_value = [ cosntruct_replace(element, arg_replace, value) for element in attribute_new_value ]
67+
response_json[attribute_new_name] = attribute_new_value
68+
return response_json
69+
def cosntructor(json_dict, *, args = {}, object_route=""):
70+
json_dict.pop(SPECIAL_FIELD_FLAG+"constructor")
71+
response_json = copy.deepcopy(json_dict)
72+
args_to_check = copy.deepcopy(args)
73+
args_to_check.pop(SPECIAL_FIELD_FLAG+"from")
74+
if SPECIAL_FIELD_FLAG+"excludes" in args_to_check:
75+
args_to_check.pop(SPECIAL_FIELD_FLAG+"excludes")
76+
if SPECIAL_FIELD_FLAG+"includes" in args_to_check:
77+
args_to_check.pop(SPECIAL_FIELD_FLAG+"includes")
78+
for arg in args:
79+
new_value = cosntruct_replace(response_json, SPECIAL_FIELD_FLAG+arg, args[arg])
80+
response_json = new_value
81+
return response_json
82+
def special_flags_processing(json_dict, *, args = {}, base_folder=None, base_dict={}, object_route=""):
1783
if SPECIAL_FIELD_FLAG+"constructor" in json_dict:
18-
json_dict.pop(SPECIAL_FIELD_FLAG+"constructor")
84+
json_dict = cosntructor(json_dict, args=args)
85+
base_dict = copy.deepcopy(json_dict)
1986
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])
25-
else:
26-
CustomLogging.error(f"Attribute not found {object_route}.{extends_from[0]}\n{base_dict}")
27-
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")
87+
json_dict = extends(json_dict, base_folder=base_folder, base_dict=base_dict, object_route=object_route)
5288
for attribute in json_dict:
5389
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}")
90+
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}")
5591
return copy.deepcopy(json_dict)
5692

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)
93+
def json_global_compile(json_dict, *, args = {}, base_folder=None, base_dict={}, object_route= ""):
94+
data = special_flags_processing(json_dict, args=args, base_folder=base_folder, base_dict=json_dict, object_route=object_route)
5995
return data
6096
class Compiler:
6197
blueprint: dict = {}
@@ -68,18 +104,20 @@ def __init__(self, main_file) -> None:
68104
def compile_models(self):
69105
if MODELS_FIELD not in self.blueprint and EXTENDS_FIELD not in self.blueprint:
70106
CustomLogging.error("models is not defined")
71-
build = json_global_compile(self.blueprint)
107+
build = json_global_compile(self.blueprint, base_folder=self.main_folder)
72108
for model in build["models"].copy():
73109
model_file_name = self.main_file
74-
if type(model) == str:
110+
if type(build["models"][model]) == str:
75111
model_file_name = search_json(
76112
build["models"][model], base_folder=self.main_folder)
77113
if not model_file_name:
78114
CustomLogging.error(build["models"][model], "path does not exists in")
79115
continue
80116
model_json = load_json_as_dict(model_file_name)
81-
elif type(model) == dict:
82-
model_json = model
117+
elif type(build["models"][model]) == dict:
118+
model_json = build["models"][model]
119+
else:
120+
CustomLogging.error(f"invalid model {model}")
83121
model_build = json_global_compile(model_json, base_folder = os.path.dirname(model_file_name), object_route=model)
84122
build["models"][model] = model_build
85123
pp = pprint.PrettyPrinter(indent=2)
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
{
2+
"models":{
3+
"human":{
4+
"attributes":{
5+
"id":{
6+
"type":"id"
7+
},
8+
"nacimiento":{
9+
"max":20,
10+
"required":true,
11+
"type":"date"
12+
},
13+
"nombre":{
14+
"max":20,
15+
"required":true,
16+
"type":"str"
17+
},
18+
"sector":{
19+
"max":100,
20+
"required":true,
21+
"type":"str"
22+
}
23+
},
24+
"create":{
25+
"request":{
26+
"nacimiento":{
27+
"max":20,
28+
"required":true,
29+
"type":"date"
30+
},
31+
"nombre":{
32+
"max":20,
33+
"required":true,
34+
"type":"str"
35+
},
36+
"sector":{
37+
"max":100,
38+
"required":true,
39+
"type":"str"
40+
}
41+
},
42+
"response":{
43+
"id":{
44+
"type":"id"
45+
}
46+
}
47+
},
48+
"get":{
49+
"request":{
50+
"id":{
51+
"type":"id"
52+
}
53+
},
54+
"response":{
55+
"nacimiento":{
56+
"max":20,
57+
"required":true,
58+
"type":"date"
59+
},
60+
"nombre":{
61+
"max":20,
62+
"required":true,
63+
"type":"str"
64+
},
65+
"sector":{
66+
"max":100,
67+
"required":true,
68+
"type":"str"
69+
}
70+
}
71+
},
72+
"list":{
73+
"response":{
74+
"nombre":{
75+
"max":20,
76+
"required":true,
77+
"type":"str"
78+
}
79+
}
80+
}
81+
}
82+
},
83+
"name":"construct",
84+
"services":{
85+
"human":"utils.services.human"
86+
}
87+
}

0 commit comments

Comments
 (0)