/
openapi.go
259 lines (227 loc) · 6.89 KB
/
openapi.go
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package ddlconv
import (
"encoding/json"
"github.com/pingcap/parser"
"github.com/pingcap/parser/ast"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)
type DDLConverter struct {
Openapi string `json:"openapi" yaml:"openapi"`
Info map[string]string `json:"info" yaml:"info"`
Paths struct{} `json:"paths" yaml:"paths"`
Components Components `json:"components" yaml:"components"`
}
func New(sql ...string) (*DDLConverter, error) {
o := &DDLConverter{
Openapi: "3.0.1",
Info: map[string]string{
"title": "database-schemas",
"version": "1.0",
},
Paths: struct{}{},
Components: struct {
Schemas map[string]OpenapiSchema `json:"schemas" yaml:"schemas"`
}{Schemas: make(map[string]OpenapiSchema, 0)},
}
if len(sql) == 0 {
return o, nil
}
for _, s := range sql {
if _, err := o.WriteString(s); err != nil {
return nil, err
}
}
return o, nil
}
func (o *DDLConverter) Write(sql []byte) (int, error) {
return o.WriteString(string(sql))
}
func (o *DDLConverter) WriteString(sql string) (int, error) {
p := parser.New()
nodes, _, err := p.Parse(sql, "", "")
if err != nil {
return 0, err
}
for _, node := range nodes {
node.Accept(o)
}
return len(sql), nil
}
func (o *DDLConverter) Enter(in ast.Node) (ast.Node, bool) {
switch in.(type) {
case ast.DDLNode:
return o.enterDDL(in.(ast.DDLNode))
default:
return in, true
}
}
func (o *DDLConverter) enterDDL(in ast.DDLNode) (out ast.Node, skip bool) {
out = in
skip = true
switch in.(type) {
case *ast.DropTableStmt:
stmt := in.(*ast.DropTableStmt)
names := ExtractDropNames(stmt)
for _, name := range names {
delete(o.Components.Schemas, name)
}
case *ast.CreateTableStmt:
stmt := in.(*ast.CreateTableStmt)
name := ExtractCreateName(stmt)
if _, ok := o.Components.Schemas[name]; ok {
return
}
var schema OpenapiSchema
schema.Name = name
schema.Type = "object"
schema.Properties = make(map[string]*openapiSchemaProperty, 0)
for _, col := range stmt.Cols {
var property openapiSchemaProperty
property.XRaw = ExtractColName(col)
property.Name = snake2LowerCamel(property.XRaw)
format := ExtractColType(col)
property.Type = mysqlType2OpenapiType(format)
property.Example = genExample(property.XRaw, property.Type)
property.Description = ExtractColComment(col)
property.XSource = schema.Name
schema.Properties[property.Name] = &property
}
o.Components.Schemas[schema.Name] = schema
case *ast.RenameTableStmt:
stmt := in.(*ast.RenameTableStmt)
newName, oldName := ExtractRename(stmt)
schema, ok := o.Components.Schemas[oldName]
if !ok {
return
}
schema.Name = newName
delete(o.Components.Schemas, oldName)
o.Components.Schemas[newName] = schema // 如果新名称已存在, 则会覆盖
case *ast.AlterTableStmt:
stmt := in.(*ast.AlterTableStmt)
schemaName := ExtractAlterTableName(stmt)
schema, ok := o.Components.Schemas[schemaName]
if !ok {
return
}
for _, spec := range stmt.Specs {
switch spec.Tp {
case ast.AlterTableAddColumns:
var property openapiSchemaProperty
property.XSource = schemaName
property.XRaw = ExtractAlterTableAddColName(spec)
property.Name = snake2LowerCamel(property.XRaw)
format := ExtractAlterTableAddColType(spec)
property.Type = mysqlType2OpenapiType(format)
property.Example = genExample(property.XRaw, property.Type)
property.Description = ExtractAlterTableAddColComment(spec)
schema.Properties[property.Name] = &property
case ast.AlterTableChangeColumn:
oldRaw := ExtractAlterTableChangeColOldName(spec)
property, ok := schema.getProperty(oldRaw)
if !ok {
continue
}
if raw := ExtractAlterTableChangeColNewName(spec); raw != "" {
property.XRaw = raw
property.Name = snake2LowerCamel(property.XRaw)
}
if format := ExtractAlterTableChangeColType(spec); format != "" {
// property.Format = format
property.Type = mysqlType2OpenapiType(format)
}
property.Example = genExample(property.XRaw, property.Type)
if desc := ExtractAlterTableChangeColComment(spec); desc != "" {
property.Description = desc
}
delete(schema.Properties, oldRaw)
schema.Properties[property.Name] = property
case ast.AlterTableModifyColumn:
raw := ExtractAlterTableModifyColName(spec)
property, ok := schema.getProperty(raw)
if !ok {
continue
}
if format := ExtractAlterTableModifyColType(spec); format != "" {
// property.Format = format
property.Type = mysqlType2OpenapiType(format)
}
property.Example = genExample(property.XRaw, property.Type)
if desc := ExtractAlterTableModifyColComment(spec); desc != "" {
property.Description = desc
}
case ast.AlterTableDropColumn:
raw := ExtractAlterTableDropColName(spec)
property, ok := schema.getProperty(raw)
if !ok {
continue
}
delete(schema.Properties, property.Name)
default:
log.Warnf("unsupport alter table spec type: %v", spec.Tp)
}
}
}
return
}
func (o *DDLConverter) Leave(in ast.Node) (ast.Node, bool) {
return in, false
}
func (o *DDLConverter) YAML() []byte {
if o == nil {
return nil
}
data, err := yaml.Marshal(*o)
if err != nil {
return nil
}
return data
}
func (o *DDLConverter) JSON() []byte {
if o == nil {
return nil
}
data, err := json.MarshalIndent(*o, "", " ")
if err != nil {
return nil
}
return data
}
type OpenapiSchema struct {
Name string `json:"-" yaml:"-"`
Type string `json:"type" yaml:"type"`
Properties map[string]*openapiSchemaProperty `json:"properties" yaml:"properties"`
}
func (s OpenapiSchema) getProperty(raw string) (*openapiSchemaProperty, bool) {
for _, v := range s.Properties {
if v.XRaw == raw {
return v, true
}
}
return nil, false
}
type openapiSchemaProperty struct {
Name string `json:"-" yaml:"-"`
Type string `json:"type" yaml:"type"`
// Format string `json:"format,omitempty" yaml:"format"`
Example interface{} `json:"example" yaml:"example"`
Description string `json:"description" yaml:"description"`
XRaw string `json:"x-dice-raw" yaml:"x-dice-raw"`
XSource string `json:"x-dice-source" yaml:"x-dice-source"`
}
type Components struct {
Schemas map[string]OpenapiSchema `json:"schemas" yaml:"schemas"`
}