Skip to content

Commit 12f194b

Browse files
calculator example
1 parent 97812de commit 12f194b

File tree

12 files changed

+195
-20
lines changed

12 files changed

+195
-20
lines changed

README.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,56 @@ defines all the models for a specific entity and its different variants dependin
2424
### Service component
2525
Denoted with the extension ".service.json" (ex: "auth.service.json")
2626

27-
defines the inputs, input processing and outputs of a certain request
27+
defines the inputs, input processing and outputs of a certain request
28+
29+
## Global special attributes
30+
31+
### __constructor
32+
lets you specify wich attributes can be modified and are required
33+
34+
use case:
35+
36+
``` js
37+
// basic_math_service.service.json
38+
{
39+
"__constructor":{
40+
"number_a" : {
41+
"description": "first number to be operated"
42+
"default": 1
43+
},
44+
"number_a" : {
45+
"description": "second number to be operated"
46+
"default": 2
47+
}
48+
},
49+
"endpoints":{
50+
"sum numbers":{
51+
"request":{
52+
"__number_a" : {
53+
"type": "int",
54+
},
55+
"__number_b" : {
56+
"type": "int",
57+
}
58+
},
59+
"response":{
60+
"answer" : {"type": "int"}
61+
}
62+
},
63+
}
64+
}
65+
```
66+
67+
now you can call it like
68+
``` js
69+
//main.app.json
70+
{
71+
"services":{
72+
"math": {
73+
"__from":"basic_math_service.service.json"
74+
"number_a":3,
75+
"number_b":4,
76+
}
77+
}
78+
}
79+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "calculator",
3+
"models": {
4+
},
5+
"services": {
6+
"operate":{
7+
"endpoints": {
8+
"sum":{
9+
"request":{"a":{"type":"int"}, "b":{"type":"int"}},
10+
"response":{"a":{"type":"int"}, "b":{"type":"int"}},
11+
"processing":{
12+
"engine": "python3.8",
13+
"code": [
14+
"print(f\"summing\" {a} + {b})",
15+
"return a+b"
16+
]
17+
}
18+
}
19+
}
20+
}
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "calculator",
3+
"models": {
4+
},
5+
"services": {
6+
"operate":{
7+
"endpoints": {
8+
"sum":{
9+
"request":{"a":{"type":"int"}, "b":{"type":"int"}},
10+
"response":{"a":{"type":"int"}, "b":{"type":"int"}},
11+
"processing":{
12+
"engine": "python3.8",
13+
"code": [
14+
"print(f\"summing\" {a} + {b})",
15+
"return a+b"
16+
]
17+
}
18+
}
19+
}
20+
}
21+
}
22+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"endpoints": {
3+
"sum":{
4+
"request":{"a":{"type":"int"}, "b":{"type":"int"}},
5+
"response":{"a":{"type":"int"}, "b":{"type":"int"}},
6+
"processing":{
7+
"from": "processing.operations.sum",
8+
"number_a": "a",
9+
"number_b": "b",
10+
"message": "calculating sum"
11+
}
12+
}
13+
}
14+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"engine": "python3.8",
3+
"__constructor":{
4+
"description": "what seams like a simple sum",
5+
"number_a":{"type":"str", "default":"number_a"},
6+
"number_b":{"type":"str", "default":"number_b"},
7+
"message":{"type":"str", "default":"adding {__number_a} to {__number_b}"}
8+
},
9+
"inputs":{
10+
"__number_a":{"type":"str", "required":true},
11+
"__number_b":{"type":"str", "required":true}
12+
},
13+
"code": [
14+
{
15+
"type": "function",
16+
"name": "print",
17+
"args": ["f\"__message\""]
18+
},
19+
{
20+
"type": "variable",
21+
"variable_type": "str",
22+
"name": "sum_result",
23+
"equals": ["__number_a+number_b"]
24+
},
25+
{
26+
"type": "return",
27+
"args": ["sum_result"]
28+
}
29+
]
30+
}

blueprints/examples/facebook/main.app.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
"name": "facebook",
33
"models": {
44
"user":"global.models.user",
5-
"conversation":"global.comunication.chat.models.conversation",
6-
"message":"global.comunication.chat.feed.models.message"
5+
"conversation":"global.communication.chat.models.conversation",
6+
"message":"global.communication.chat.feed.models.message"
77
},
88
"services": {
9-
"user":"global.utils.services.user"
9+
"user":"global.utils.services.user",
10+
"conversation":"global.communication.chat.services.conversation",
11+
"message":"global.communication.chat.services.message"
1012
}
1113
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
3+
}

compiler/compiler.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from json_compiler.Compiler import Compiler as json_compiler
22
import sys
33
COMPILERS = {
4-
"django-postgres": "under development",
5-
"flask-mongo": "under development",
6-
"nodejs-mongo": "under development",
7-
"json": json_compiler
4+
"django-postgres.v1": "under development",
5+
"flask-mongo.v1": "under development",
6+
"nodejs-mongo.v1": "under development",
7+
"json.v1": json_compiler
88
}
99
def compile(compiler_name, project_name):
1010
compiler = COMPILERS[compiler_name](project_name)
@@ -18,4 +18,4 @@ def main():
1818
return
1919

2020
main()
21-
#python .\compiler.py c json ../blueprints/examples/facebook/main.app.json
21+
#python .\compiler.py c "json.v1" ../blueprints/examples/facebook/main.app.json

compiler/json_compiler/Compiler.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import json
2-
from searcher import search_json
2+
from utils.searcher import search_json
33
MODELS_FIELD = "models"
4-
EXTENDS_FIELD = "__extends"
4+
SPECIAL_FIELD_FLAG="__"
5+
EXTENDS_FIELD = SPECIAL_FIELD_FLAG+"extends"
6+
57

68
class Compiler:
79
blueprint:dict = {}
@@ -13,8 +15,12 @@ def compile_models(self):
1315
if MODELS_FIELD not in self.blueprint and EXTENDS_FIELD not in self.blueprint:
1416
raise NameError("models is not defined")
1517
for model in self.blueprint["models"]:
16-
print(model)
18+
if model.startswith(SPECIAL_FIELD_FLAG):
19+
if model == "__extends":
20+
print("extends!")
21+
# Extend
22+
continue
1723
if type(model) == str:
18-
search_json(model, self.main_file)
24+
print("model", search_json(self.blueprint["models"][model], self.main_file))
1925
def compile(self):
2026
self.compile_models()

compiler/searcher.py

Lines changed: 0 additions & 7 deletions
This file was deleted.

compiler/utils/searcher.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import os
2+
GLOBAL_ROUTE = "../blueprints"
3+
def get_dir(path):
4+
return [ x.split(".") for x in os.listdir(path)]
5+
def get_json_file(splitted_route, dir_path):
6+
files_names = get_dir(dir_path)
7+
for file_name in files_names:
8+
if splitted_route[0] == file_name[0]:
9+
new_path = dir_path+"/"+".".join(file_name)
10+
if os.path.isfile(new_path):
11+
return new_path
12+
else:
13+
new_splitted_route = splitted_route[1:]
14+
nested = get_json_file(new_splitted_route, new_path)
15+
return nested
16+
17+
#get_json(file_name.)
18+
return False
19+
def search_json(route, base_file):
20+
splitted_route = route.split(".")
21+
# Local Path
22+
json_component = get_json_file(splitted_route, (os.path.dirname(base_file)))
23+
if json_component:
24+
return json_component
25+
# Global Path
26+
json_component = get_json_file(splitted_route, GLOBAL_ROUTE)
27+
if json_component:
28+
return json_component

0 commit comments

Comments
 (0)