Skip to content

Commit 8d5cae8

Browse files
recursive constructor
1 parent 06533c4 commit 8d5cae8

File tree

5 files changed

+63
-29
lines changed

5 files changed

+63
-29
lines changed

blueprints/examples/calculator/v2/services/operate.service.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
"endpoints": {
33
"sum":{
44
"request":{"a":{"type":"int"}, "b":{"type":"int"}},
5-
"response":{"a":{"type":"int"}, "b":{"type":"int"}},
5+
"response":{"result":{"type":"int"}},
66
"processing":{
7-
"from": "processing.operations.sum",
8-
"number_a": "a",
9-
"number_b": "b",
10-
"message": "calculating sum"
7+
"__extends":{
8+
"__from": "processing.operations.sum"
9+
}
1110
}
1211
}
1312
}
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
{
22
"engine": "python3.8",
3+
"__description": "what seams like a simple sum",
34
"__constructor":{
4-
"description": "what seams like a simple sum",
55
"number_a":{"type":"str", "default":"number_a"},
66
"number_b":{"type":"str", "default":"number_b"},
77
"message":{"type":"str", "default":"adding {__number_a} to {__number_b}"}
88
},
99
"inputs":{
10-
"__number_a":{"type":"str", "required":true},
11-
"__number_b":{"type":"str", "required":true}
10+
"__number_a":{"type":"int", "required":true},
11+
"__number_b":{"type":"int", "required":true}
12+
},
13+
"outputs":{
14+
"result":{"type":"int", "required":true}
1215
},
1316
"code": [
1417
{
@@ -19,12 +22,12 @@
1922
{
2023
"type": "variable",
2124
"variable_type": "str",
22-
"name": "sum_result",
25+
"name": "result",
2326
"equals": ["__number_a+number_b"]
2427
},
2528
{
2629
"type": "return",
27-
"args": ["sum_result"]
30+
"args": ["result"]
2831
}
2932
]
3033
}

compiler/compiler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ def main():
2121
# python .\compiler.py c "json.v1" ../blueprints/examples/facebook/main.app.json
2222
# python .\compiler.py c "json.v1" ./sample_blueprints/samples/import.app.json
2323
# python .\compiler.py c "json.v1" ./sample_blueprints/samples/construct.app.json
24+
# python .\compiler.py c "json.v1" ../blueprints/examples/calculator/v2/main.app.json

compiler/json_compiler/Compiler.py

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from utils.CustomLogging import CustomLogging
77

88
MODELS_FIELD = "models"
9+
SEVICES_FIELD = "services"
910
SPECIAL_FIELD_FLAG = "__"
1011
EXTENDS_FIELD = SPECIAL_FIELD_FLAG+"extends"
1112

@@ -23,7 +24,7 @@ def extends(json_dict, *, base_folder=None, base_dict={}, object_route=""):
2324
if extends_from[0] in base_dict:
2425
attr_build = special_flags_processing(base_dict[extends_from[0]], base_dict=base_dict, object_route = new_route)
2526
else:
26-
CustomLogging.error(f"Attribute {extends_from[0]} not found \n{base_dict}")
27+
CustomLogging.error(f"Attribute {extends_from[0]} not found extending {object_route}\n{base_dict}")
2728
else:
2829
attr_file_name = search_json(json_dict[SPECIAL_FIELD_FLAG+"extends"][SPECIAL_FIELD_FLAG+"from"], base_folder=base_folder)
2930
if not attr_file_name:
@@ -86,9 +87,13 @@ def cosntructor(json_dict, *, args = {}, object_route=""):
8687
if default_attribute.startswith(SPECIAL_FIELD_FLAG):
8788
continue
8889
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
90+
updates = True
91+
while updates: # Dangerous Loop
92+
for arg in args_to_check:
93+
new_value = cosntruct_replace(response_json, SPECIAL_FIELD_FLAG+arg, args_to_check[arg])
94+
if new_value == response_json:
95+
updates = False
96+
response_json = new_value
9297
return response_json
9398
def special_flags_processing(json_dict, *, args = {}, base_folder=None, base_dict={}, object_route=""):
9499
if SPECIAL_FIELD_FLAG+"constructor" in json_dict:
@@ -112,27 +117,53 @@ def __init__(self, main_file) -> None:
112117
self.main_file = main_file
113118
self.blueprint = load_json_as_dict(main_file)
114119

115-
def compile_models(self):
116-
if MODELS_FIELD not in self.blueprint and EXTENDS_FIELD not in self.blueprint:
117-
CustomLogging.error("models is not defined")
118-
build = json_global_compile(self.blueprint, base_folder=self.main_folder)
119-
for model in build["models"].copy():
120+
def compile_models(self, build):
121+
if MODELS_FIELD not in build:
122+
CustomLogging.error(f"{MODELS_FIELD} field is not defined")
123+
models = build[MODELS_FIELD]
124+
for model in models.copy():
120125
model_file_name = self.main_file
121-
if type(build["models"][model]) == str:
126+
if type(models[model]) == str:
122127
model_file_name = search_json(
123-
build["models"][model], base_folder=self.main_folder)
128+
models[model], base_folder=self.main_folder)
124129
if not model_file_name:
125-
CustomLogging.error(build["models"][model], "path does not exists in")
130+
CustomLogging.error(models[model], "path does not exists in")
126131
continue
127132
model_json = load_json_as_dict(model_file_name)
128-
elif type(build["models"][model]) == dict:
129-
model_json = build["models"][model]
133+
elif type(models[model]) == dict:
134+
model_json = models[model]
130135
else:
131136
CustomLogging.error(f"invalid model {model}")
132137
model_build = json_global_compile(model_json, base_folder = os.path.dirname(model_file_name), object_route=model)
133-
build["models"][model] = model_build
134-
pp = pprint.PrettyPrinter(indent=2)
135-
pp.pprint(build)
138+
models[model] = model_build
139+
build[MODELS_FIELD] = models
140+
return build
141+
def compile_services(self, build):
142+
if SEVICES_FIELD not in build:
143+
CustomLogging.error(f"{SEVICES_FIELD} field is not defined")
144+
services = build[SEVICES_FIELD]
145+
for service in services.copy():
146+
service_file_name = self.main_file
147+
if type(services[service]) == str:
148+
service_file_name = search_json(
149+
services[service], base_folder=self.main_folder)
150+
if not service_file_name:
151+
CustomLogging.error(services[service], "path does not exists in")
152+
continue
153+
service_json = load_json_as_dict(service_file_name)
154+
elif type(services[service]) == dict:
155+
service_json = services[service]
156+
else:
157+
CustomLogging.error(f"invalid service {service}")
158+
service_build = json_global_compile(service_json, base_folder = os.path.dirname(service_file_name), object_route=service)
159+
services[service] = service_build
160+
build[SEVICES_FIELD] = services
161+
return build
136162

137163
def compile(self):
138-
self.compile_models()
164+
build = json_global_compile(self.blueprint, base_folder=self.main_folder)
165+
build = self.compile_models(build)
166+
build = self.compile_services(build)
167+
#print(build)
168+
pp = pprint.PrettyPrinter(indent=2)
169+
pp.pprint(build)

compiler/sample_blueprints/samples/utils/models/human.model.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"attributes":{
99
"id": {"type":"id"},
1010
"__name":{"type":"str", "max":20, "required":true},
11-
"__birth":{"type":"date", "max":20, "required":true},
11+
"__birth":{"type":"date", "required":true},
1212
"__sector":{"type":"str", "max":100, "required":true}
1313
},
1414
"create":{

0 commit comments

Comments
 (0)