1
|
|
|
def type_name(item): |
2
|
|
|
""" |
3
|
|
|
---- |
4
|
|
|
examples: |
5
|
|
|
|
6
|
|
|
1) type_name('apple') -> 'str' |
7
|
|
|
---- |
8
|
|
|
:param item: |
9
|
|
|
:return: |
10
|
|
|
""" |
11
|
|
|
return type(item).__name__ |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
def schema_fn(type_of_item): |
15
|
|
|
""" |
16
|
|
|
---- |
17
|
|
|
examples: |
18
|
|
|
|
19
|
|
|
1) schema_fn("str") -> get_schema_for_primitive |
20
|
|
|
---- |
21
|
|
|
:param type_of_item: |
22
|
|
|
:return: |
23
|
|
|
""" |
24
|
|
|
type_fn_list = { |
25
|
|
|
"str": get_schema_for_primitive, |
26
|
|
|
"int": get_schema_for_primitive, |
27
|
|
|
"float": get_schema_for_primitive, |
28
|
|
|
"list": get_schema_for_list, |
29
|
|
|
"dict": get_schema_for_dict |
30
|
|
|
} |
31
|
|
|
return type_fn_list[type_of_item] |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
def get_schema_for_primitive(name, type_of_item): |
35
|
|
|
""" |
36
|
|
|
---- |
37
|
|
|
examples: |
38
|
|
|
|
39
|
|
|
1) get_schema_for_primitive('', '') -> [] |
40
|
|
|
---- |
41
|
|
|
:param name: |
42
|
|
|
:param type_of_item: |
43
|
|
|
:return: |
44
|
|
|
""" |
45
|
|
|
return [] |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
def get_schema_for_list(body, type_of_item): |
49
|
|
|
""" |
50
|
|
|
---- |
51
|
|
|
examples: |
52
|
|
|
|
53
|
|
|
@let |
54
|
|
|
schema_object = { |
55
|
|
|
'inner': [{ |
56
|
|
|
'inner': [], |
57
|
|
|
'name': 'key', |
58
|
|
|
'type': 'int' |
59
|
|
|
}], |
60
|
|
|
'name': '__auto__', |
61
|
|
|
'type': 'dict' |
62
|
|
|
} |
63
|
|
|
@end |
64
|
|
|
|
65
|
|
|
1) get_schema_for_list([{"key": 1}], "list") -> schema_object |
66
|
|
|
---- |
67
|
|
|
:param body: |
68
|
|
|
:param type_of_item: |
69
|
|
|
:return: |
70
|
|
|
""" |
71
|
|
|
element = body[0] if isinstance(body, list) and len(body) > 0 else None |
72
|
|
|
return { |
73
|
|
|
"name": "__auto__", |
74
|
|
|
"type": type_name(element), |
75
|
|
|
"inner": get_schema_for(element, type_name(element)) |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
def get_schema_for_dict(body, type_of_item): |
80
|
|
|
""" |
81
|
|
|
---- |
82
|
|
|
examples: |
83
|
|
|
|
84
|
|
|
1) get_schema_for_dict({'key': 1}, 'dict') -> [{'type': 'int', 'inner': [], 'name': 'key'}] |
85
|
|
|
---- |
86
|
|
|
:param body: |
87
|
|
|
:param type_of_item: |
88
|
|
|
:return: |
89
|
|
|
""" |
90
|
|
|
return [{ |
91
|
|
|
"name": key, |
92
|
|
|
"type": type_name(body[key]), |
93
|
|
|
"inner": get_schema_for(body[key], type_name(body[key])) |
94
|
|
|
} for key in body.keys()] |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
def get_schema_for(item, type_of_item): |
98
|
|
|
""" |
99
|
|
|
---- |
100
|
|
|
examples: |
101
|
|
|
|
102
|
|
|
1) get_schema_for({"key": 1}, 'dict') -> [{'type': 'int', 'inner': [], 'name': 'key'}] |
103
|
|
|
---- |
104
|
|
|
:param item: |
105
|
|
|
:param type_of_item: |
106
|
|
|
:return: |
107
|
|
|
""" |
108
|
|
|
return schema_fn(type_of_item)(item, type_of_item) |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
def make_schema_object(req_body): |
112
|
|
|
""" |
113
|
|
|
---- |
114
|
|
|
examples: |
115
|
|
|
|
116
|
|
|
@let |
117
|
|
|
schema_object = { |
118
|
|
|
'inner': [{'inner': [], 'name': 'key', 'type': 'int'}], |
119
|
|
|
'name': '__root__', |
120
|
|
|
'type': 'dict' |
121
|
|
|
} |
122
|
|
|
@end |
123
|
|
|
|
124
|
|
|
1) make_schema_object({"key": 1}) -> schema_object |
125
|
|
|
---- |
126
|
|
|
:param req_body: |
127
|
|
|
:return: |
128
|
|
|
""" |
129
|
|
|
return { |
130
|
|
|
"name": "__root__", |
131
|
|
|
"type": type_name(req_body), |
132
|
|
|
"inner": get_schema_for(req_body, type_name(req_body)) |
133
|
|
|
} |
134
|
|
|
|