-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdemo.py
98 lines (77 loc) · 3.02 KB
/
demo.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
# -*- coding:utf-8 -*-
import sqlalchemy as sa
import sqlalchemy.orm as orm
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class Group(Base):
__tablename__ = "Group"
pk = sa.Column(sa.Integer, primary_key=True, doc="primary key")
name = sa.Column(sa.String(255), default="", nullable=False)
class User(Base):
__tablename__ = "User"
pk = sa.Column(sa.Integer, primary_key=True, doc="primary key")
name = sa.Column(sa.String(255), default="", nullable=True)
group_id = sa.Column(sa.Integer, sa.ForeignKey(Group.pk), nullable=False)
group = orm.relationship(Group, uselist=False, backref="users")
## ------ForeignKeyWalker--------
import pprint as pp
from alchemyjsonschema import SchemaFactory
from alchemyjsonschema import ForeignKeyWalker
factory = SchemaFactory(ForeignKeyWalker)
pp.pprint(factory(User))
"""
{'properties': {'group_id': {'type': 'integer'},
'name': {'maxLength': 255, 'type': 'string'},
'pk': {'description': 'primary key', 'type': 'integer'}},
'required': ['pk', 'group_id'],
'title': 'User',
'type': 'object'}
"""
## ------NoForeignKeyWalker--------
import pprint as pp
from alchemyjsonschema import SchemaFactory
from alchemyjsonschema import NoForeignKeyWalker
factory = SchemaFactory(NoForeignKeyWalker)
pp.pprint(factory(User))
"""
{'properties': {'name': {'maxLength': 255, 'type': 'string'},
'pk': {'description': 'primary key', 'type': 'integer'}},
'required': ['pk'],
'title': 'User',
'type': 'object'}
"""
## ------StructuralWalker--------
import pprint as pp
from alchemyjsonschema import SchemaFactory
from alchemyjsonschema import StructuralWalker
factory = SchemaFactory(StructuralWalker)
pp.pprint(factory(User))
"""
{'definitions': {'Group': {'properties': {'pk': {'description': 'primary key',
'type': 'integer'},
'name': {'maxLength': 255,
'type': 'string'}},
'type': 'object'}},
'properties': {'pk': {'description': 'primary key', 'type': 'integer'},
'name': {'maxLength': 255, 'type': 'string'},
'group': {'$ref': '#/definitions/Group'}},
'required': ['pk'],
'title': 'User',
'type': 'object'}
"""
pp.pprint(factory(Group))
"""
{'definitions': {'User': {'properties': {'pk': {'description': 'primary key',
'type': 'integer'},
'name': {'maxLength': 255,
'type': 'string'}},
'type': 'object'}},
'description': 'model for test',
'properties': {'pk': {'description': 'primary key', 'type': 'integer'},
'name': {'maxLength': 255, 'type': 'string'},
'users': {'items': {'$ref': '#/definitions/User'},
'type': 'array'}},
'required': ['pk', 'name'],
'title': 'Group',
'type': 'object'}
"""