-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjsonschema.py
45 lines (36 loc) · 1.48 KB
/
jsonschema.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
import json
from schema import Schema, Table, Column, Relationship
class SchemaEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, Schema):
return obj.tables
if isinstance(obj, Table):
return {"columns": obj.columns,
"relationships": obj.relationships,
"primary_key": obj.primary_key,
"include": obj.include}
if isinstance(obj, Column):
return {"mysql_type": obj.mysql_type,
"options": obj.options}
if isinstance(obj, Relationship):
return {"name": obj.name,
"remote_table": obj.table,
"remote_column": obj.remote_column,
"action": obj.action}
return json.JSONEncoder.default(self, obj)
def json2schema(data):
schema = Schema()
for name, t in data.items():
table = schema.tables[name] = Table(name)
table.include = t["include"]
table.primary_key = t["primary_key"]
for c, r in t["relationships"].items():
relationship = table.relationships[c] = \
Relationship(r["name"], c,
r["remote_table"],
r["remote_column"])
relationship.action = r["action"]
for name, c in t["columns"].items():
column = table.columns[name] = \
Column(name, c["mysql_type"], c["options"])
return schema