-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenObject.py
executable file
·116 lines (94 loc) · 3.28 KB
/
genObject.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import json
def _printParents(parents):
result = parents[0]
for parent in parents[1:]:
result += "->%s" % parent
return result
def _genProperty(discoveryDoc, prop, parents):
if '$ref' in prop:
return _genObject(discoveryDoc, prop['$ref'], parents)
elif 'type' in prop:
if prop['type'] == "any":
return {'@type': "type.googleapis.com/google.protobuf.Empty"}
elif prop['type'] == "array":
return [_genProperty(discoveryDoc, prop['items'], parents)]
elif prop['type'] == "boolean":
return True
elif prop['type'] == "integer":
if prop['format'] == "int32":
return -0x80000000
elif prop['format'] == "uint32":
return 0xFFFFFFFF
elif prop['type'] == "number":
if prop['format'] == "double":
return -0.1
elif prop['format'] == "float":
return 0.1
elif prop['type'] == "object":
return _genObject(discoveryDoc, prop, parents)
elif prop['type'] == "string":
if 'enum' in prop:
return prop['enum'][0]
if 'format' not in prop:
return "RANDOM_STRING"
elif prop['format'] == "byte":
return "UkFORE9NX1RFWFQ="
elif prop['format'] == "date":
return "2030-12-31"
elif prop['format'] == "date-time":
return "2031-11-30T23:00:30.123Z"
elif prop['format'] == "int64":
return "-9223372036854775808"
elif prop['format'] == "uint64":
return "9223372036854775808"
elif prop['format'] == "byte":
return "9223372036854775808"
elif prop['format'] == "google-duration":
return "-315576000000.000000001s"
elif prop['format'] == "google-datetime":
return "9999-12-31T23:59:59.999999999Z"
def _genObject(discoveryDoc, objName, parents):
result = {}
if objName in parents:
sys.stderr.write(
"\033[31;1mWARNING:\n"
f'\033[0m {objName} object found within itself, empty object '
"returned as child to avoid recursion\n"
f'\033[33;1m Parents: {_printParents(parents)}\033[0m\n'
)
return result
if type(objName) == dict:
obj = objName
newParents = parents
else:
obj = discoveryDoc['schemas'][objName]
newParents = parents + [objName]
if 'properties' in obj:
propDict = obj['properties']
for propName in propDict:
result[propName] = _genProperty(
discoveryDoc, propDict[propName], newParents)
if 'additionalProperties' in obj:
extras = obj['additionalProperties']
result['RANDOM_PROPERTY_NAME'] = _genProperty(
discoveryDoc, extras, newParents)
# HACK: I do not understand why this should be this way
if 'type' in extras:
if not 'properties' in obj and extras['type'] == "any":
return {'@type': "type.googleapis.com/google.protobuf.Empty"}
return result
def genObject(discoveryDoc, objName):
return _genObject(discoveryDoc, objName, [])
if __name__ == "__main__":
if len(sys.argv) < 3:
print("Usage: ./genObject.py <discovery document> <object name>")
exit(1)
discoveryDocPath = sys.argv[1]
objName = sys.argv[2]
with open(discoveryDocPath) as discoveryDocFile:
discoveryDoc = json.load(discoveryDocFile)
result = genObject(discoveryDoc, objName)
print(json.dumps(result, indent=2, sort_keys=True))