Caracteristici
alchemyjsonschema este biblioteca pentru convertirea modelului sqlalchemys in jsonschema.
- folosind alchemyjsonschema ca comanda
- folosind alchemyjsonschema ca biblioteca
ca biblioteca
avand trei stiluri de iesire.
- NoForeignKeyWalker – ignorati relatiile
- ForeignKeyWalker – asteptarea informatiilor despre relatie este o cheie straina
- StructuralWalker – iesire completa (se asteapta ca informatiile despre relatie sa fie date JSON complete)
exemple
dumping JSON cu peste trei stiluri de iesire.
modelele tinta sunt aici. Grup si utilizator.
# – * – 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): “” “model for test” “” __tablename__ = “Grup” pk = sa.Column (sa.Integer, primary_key = True, doc = “key primar”) name = sa.Column (sa.String (255), default = “”, nullable = False) Utilizator (Baza): __tablename__ = “Utilizator” pk = sa.Column (sa.Integer, primary_key = True, doc = “cheie primara”) name = sa.Column (sa.String (255), implicit = “”, nullable = Adevarat) group_id = sa.Column (sa.Integer, sa.ForeignKey (Group.pk), nullable = False) group = orm.relationship (Group, uselist = False, backref = “users”)
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’ : ‘sir’}, ‘pk’: {‘descriere’: ‘cheie principala’, ‘tip’: ‘intreg’}}, ‘obligatoriu’: [‘pk’], ‘title’: ‘Utilizator’, ‘tip ‘: ‘obiect’} “””
ForeignKeyWalker
StructuralWalker
import pprint ca pp din alchemyjsonschema import SchemaFactory din alchemyjsonschema import StructuralWalker factory = SchemaFactory (StructuralWalker) pp.pprint (factory (User)) “” “{‘definitii’: {‘Group’: {‘properties’: {‘pk’: {‘description’: ‘cheie principala’, ‘type’: ‘intreg’}, ‘name’: {‘maxLength’: 255, ‘type’: ‘sir’}}, ‘type’: ‘obiect’}}, ‘properties’: {‘pk’: {‘description’: ‘cheie principala’, ‘type’: ‘intreg’}, ‘name’: {‘maxLength’: 255, ‘type’: ‘string’}, ‘group ‘: {‘ $ ref ‘:’ # / definitii / Grup ‘}},’ obligatoriu ‘: [‘ pk ‘],’ title ‘:’ Utilizator ‘,’ tip ‘:’ obiect ‘} “””pp.pprint (fabrica (Grup))” “” {‘definitii’: {‘Utilizator’: {‘proprietati’: {‘pk’: {‘descriere’: ‘cheie primara’, ‘tip’: ‘intreg’ }, ‘name’: {‘maxLength’: 255, ‘type’: ‘string’}}, ‘type’: ‘object’}}, ‘description’: ‘model pentru test’, ‘properties’: {‘pk ‘: {‘ description ‘:’ cheie principala ‘,’ type ‘:’ intreg ‘},’ name ‘: {‘ maxLength ‘: 255,’ type ‘:’ string ‘},’ users ‘: {‘ items ‘: {‘$ ref’: ‘# / definition / User’}, ‘type’: ‘array’}}, ‘required’: [‘pk’, ‘name’], ‘title’: ‘Group’, ‘type’ : ‘obiect’} “””pprint (fabrica (grup)) “” “{‘definitii’: {‘Utilizator’: {‘proprietati’: {‘pk’: {‘descriere’: ‘cheie primara’, ‘tip’: ‘intreg’}, ‘ nume ‘: {‘ maxLength ‘: 255,’ type ‘:’ sir ‘}},’ type ‘:’ obiect ‘}},’ description ‘:’ model pentru test ‘,’ proprietati ‘: {‘ pk ‘: { ‘description’: ‘cheie principala’, ‘type’: ‘intreg’}, ‘name’: {‘maxLength’: 255, ‘type’: ‘string’}, ‘users’: {‘items’: {‘$ ref ‘:’ # / definitii / Utilizator ‘},’ tip ‘:’ matrice ‘}},’ obligatoriu ‘: [‘ pk ‘,’ nume ‘],’ titlu ‘:’ Grup ‘,’ tip ‘:’ obiect ‘} “” “pprint (fabrica (grup)) “” “{‘definitii’: {‘Utilizator’: {‘proprietati’: {‘pk’: {‘descriere’: ‘cheie primara’, ‘tip’: ‘intreg’}, ‘ nume ‘: {‘ maxLength ‘: 255,’ type ‘:’ sir ‘}},’ type ‘:’ obiect ‘}},’ description ‘:’ model pentru test ‘,’ proprietati ‘: {‘ pk ‘: { ‘description’: ‘cheie principala’, ‘type’: ‘intreg’}, ‘name’: {‘maxLength’: 255, ‘type’: ‘string’}, ‘users’: {‘items’: {‘$ ref ‘:’ # / definitii / Utilizator ‘},’ tip ‘:’ matrice ‘}},’ obligatoriu ‘: [‘ pk ‘,’ nume ‘],’ titlu ‘:’ Grup ‘,’ tip ‘:’ obiect ‘} “” “definitii ‘: {‘ Utilizator ‘: {‘ proprietati ‘: {‘ pk ‘: {‘ descriere ‘:’ cheie primara ‘,’ tip ‘:’ intreg ‘},’ nume ‘: {‘ lungime maxima ‘: 255,’ tip ‘:’ sir ‘}},’ tip ‘:’ obiect ‘}},’ descriere ‘:’ model pentru test ‘,’ proprietati ‘: {‘ pk ‘: {‘ descriere ‘:’ cheie principala ‘,’ tip ‘ : ‘intreg’}, ‘nume’: {‘maxLength’: 255, ‘type’: ‘sir’}, ‘utilizatori’: {‘articole’: {‘$ ref’: ‘# / definitii / Utilizator’}, ‘type’: ‘array’}}, ‘required’: [‘pk’, ‘name’], ‘title’: ‘Group’, ‘type’: ‘object’} “” “definitii ‘: {‘ Utilizator ‘: {‘ proprietati ‘: {‘ pk ‘: {‘ descriere ‘:’ cheie primara ‘,’ tip ‘:’ intreg ‘},’ nume ‘: {‘ lungime maxima ‘: 255,’ tip ‘:’ sir ‘}},’ tip ‘:’ obiect ‘}},’ descriere ‘:’ model pentru test ‘,’ proprietati ‘: {‘ pk ‘: {‘ descriere ‘:’ cheie principala ‘,’ tip ‘ : ‘intreg’}, ‘nume’: {‘maxLength’: 255, ‘type’: ‘sir’}, ‘utilizatori’: {‘articole’: {‘$ ref’: ‘# / definitii / Utilizator’}, ‘type’: ‘array’}}, ‘required’: [‘pk’, ‘name’], ‘title’: ‘Group’, ‘type’: ‘object’} “” “‘cheie primara’, ‘type’: ‘intreg’}, ‘name’: {‘maxLength’: 255, ‘type’: ‘string’}}, ‘type’: ‘object’}}, ‘description’: ‘ model pentru test ‘,’ properties ‘: {‘ pk ‘: {‘ description ‘:’ cheie primara ‘,’ type ‘:’ intreg ‘},’ name ‘: {‘ maxLength ‘: 255,’ type ‘:’ string ‘},’ users ‘: {‘ items ‘: {‘ $ ref ‘:’ # / definition / User ‘},’ type ‘:’ array ‘}},’ required ‘: [‘ pk ‘,’ name ‘] , ‘title’: ‘Grupare’, ‘type’: ‘obiect’} “” “‘cheie primara’, ‘type’: ‘intreg’}, ‘name’: {‘maxLength’: 255, ‘type’: ‘string’}}, ‘type’: ‘object’}}, ‘description’: ‘ model pentru test ‘,’ properties ‘: {‘ pk ‘: {‘ description ‘:’ cheie primara ‘,’ type ‘:’ intreg ‘},’ name ‘: {‘ maxLength ‘: 255,’ type ‘:’ string ‘},’ users ‘: {‘ items ‘: {‘ $ ref ‘:’ # / definition / User ‘},’ type ‘:’ array ‘}},’ required ‘: [‘ pk ‘,’ name ‘] , ‘title’: ‘Grupare’, ‘type’: ‘obiect’} “” “description ‘:’ model pentru test ‘,’ properties ‘: {‘ pk ‘: {‘ description ‘:’ cheie primara ‘,’ type ‘:’ intreg ‘},’ name ‘: {‘ maxLength ‘: 255,’ type ‘:’ sir ‘},’ utilizatori ‘: {‘ elemente ‘: {‘ $ ref ‘:’ # / definitii / Utilizator ‘},’ tip ‘:’ matrice ‘}},’ obligatoriu ‘: [‘ pk ‘, ‘name’], ‘title’: ‘Group’, ‘type’: ‘object’} “” “description ‘:’ model pentru test ‘,’ properties ‘: {‘ pk ‘: {‘ description ‘:’ cheie primara ‘,’ type ‘:’ intreg ‘},’ name ‘: {‘ maxLength ‘: 255,’ type ‘:’ sir ‘},’ utilizatori ‘: {‘ elemente ‘: {‘ $ ref ‘:’ # / definitii / Utilizator ‘},’ tip ‘:’ matrice ‘}},’ obligatoriu ‘: [‘ pk ‘, ‘name’], ‘title’: ‘Group’, ‘type’: ‘object’} “” “obligatoriu ‘: [‘ pk ‘,’ name ‘],’ title ‘:’ Group ‘,’ type ‘:’ object ‘} “” “obligatoriu ‘: [‘ pk ‘,’ name ‘],’ title ‘:’ Group ‘,’ type ‘:’ object ‘} “” “
ca comanda
folosind alchemyjsonschema ca comanda (numele comenzii este si alchemyjsonschema).
Ajutor
$ alchemyjsonschema –help usage: alchemyjsonschema [-h] [–walker {noforeignkey, foreignkey, structural}] [–decision {default, fullset}] [–depth DEPTH] [–out OUT] tinta argumente pozitionale: directionati modulul sau clasa pentru a extrage scheme din argumente optionale: -h, –help afisati acest mesaj de ajutor si iesiti –walker {noforeignkey, foreignkey, structural} –decision {implicit, fullset} –depth DEPTH –out OUT iesire in fisier
Daca mai sus exista doua definitii ale modelului (Utilizator, Grup) in alchemyjsonschema.tests.models.
Tinta este pozitia clasei sau pozitia modulului. de exemplu,
- pozitia clasei – alchemyjsonschema.tests.models: User
- pozitia modulului – alchemyjsonschema.tests.models
exemplu
Utilizarea StructuralWalker prin linia de comanda (–walker structural). Desigur, NoForeignKeyWalker este noforeignkey, iar ForeignKeyWalker este strain.
$ alchemyjsonschema –walker structural alchemyjsonschema.tests.models: Grup {“definitii”: {“Grup”: {“proprietati”: {“culoare”: {“enum”: [“rosu”, “verde”, “galben” , “blue”], “maxLength”: 6, “type”: “string”}, “created_at”: {“format”: “data-ora”, “type”: “string”}, “name”: { “maxLength”: 255, “type”: “string”}, “pk”: {“description”: “cheie principala”, “type”: “intreg”}, “users”: {“items”: {“$ ref “:” # / definition / User “},” type “:” array “}},” required “: [” pk “],” title “:” Group “,”type”: “object”}, “User”: {“properties”: {“created_at”: {“format”: “data-ora”, “type”: “sir”}, “name”: {“maxLength “: 255,” type “:” sir “},” pk “: {” description “:” cheie primara “,” type “:” intreg “}},” obligatoriu “: [” pk “],” type ” : “obiect” } } }intreg “}},” obligatoriu “: [” pk “],” tip “:” obiect “}}}intreg “}},” obligatoriu “: [” pk “],” tip “:” obiect “}}}
Iesirea nu este aceeasi atunci cand se utilizeaza direct clasa Walker. Aceasta este o iesire la indemana pentru ceva de genul instrumentului swagger (OpenAPI 2.0).
apendice: ce este –decizia?
ce este decizia? (TODO: descriere blanda)
$ alchemyjsonschema –walker structural alchemyjsonschema.tests.models: Utilizator | jq. -S> /tmp/default.json $ alchemyjsonschema –decision useforeignkey –walker structural alchemyjsonschema.tests.models: Utilizator | jq. -S> /tmp/useforeignkey.json $ diff -u /tmp/default.json /tmp/useforeignkey.json
— /tmp/default.json 2017-01-02 22: 49: 44.000000000 +0900 +++ /tmp/useforeignkey.json 2017-01-02 22: 53: 13.000000000 +0900 @@ -1,43 +1 , 14 @@ {“definitii”: {- “Grup”: {- “proprietati”: {- “culoare”: {- “enum”: [- “rosu”, – “verde”, – “galben”, – “albastru” -], – “maxLength”: 6, – “type”: “sir” -}, – “created_at”: {- “format”: “data-ora”, – “type”: “sir” – }, – “nume”: {- “maxLength”: 255, – “type”: “sir” -}, – “pk”: {- “description”: “cheie principala”, – “type”: “intreg” -} -}, – “required”: [- “pk” -], – “type”: “object” -}, “Utilizator “: {” properties “: {” created_at “: {” format “:” data-ora “,” tip “:” sir “}, -” grup “: {-” $ ref “:” # / definitii / Group “+” group_id “: {+” relation “:” group “, +” type “:” integer “},” name “: {” maxLength “: 255,