-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate-schemas.py
41 lines (29 loc) · 1.15 KB
/
generate-schemas.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
from __future__ import print_function
import json
class AthenaSchemaFromJson(object):
def __init__(self, file_name):
self.file_name = file_name
with open(file_name) as schema:
self.json = json.load(schema)
@staticmethod
def write(msg):
print(msg, end='')
def process_list(self, spec):
return ",".join(["%s" % (self.get_type(key)) for key in spec])
def process_dict(self, spec):
return ",".join(["%s:%s" % (key, self.get_type(spec[key])) for key in spec])
def get_type(self, e):
if isinstance(e, dict):
return "struct<%s>" % self.process_dict(e)
elif isinstance(e, list):
return "array<%s>" % self.process_list(e)
else:
return e
def generate(self):
print("*** Generating schema for [%s]" % self.file_name)
schema_filename = self.file_name.replace(".json", ".schema")
with open(schema_filename, "w") as schema:
schema.write(self.get_type(self.json))
if __name__ == '__main__':
for schema in ['bid-request-2.0.json', 'bid-response-2.0.json']:
AthenaSchemaFromJson(schema).generate()