-
Notifications
You must be signed in to change notification settings - Fork 249
/
doc.go
420 lines (401 loc) · 11.1 KB
/
doc.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package codegen
import (
"bytes"
"encoding/json"
"fmt"
"github.com/iancoleman/strcase"
"github.com/sirupsen/logrus"
"github.com/unionj-cloud/go-doudou/cmd/internal/astutils"
v3helper "github.com/unionj-cloud/go-doudou/cmd/internal/openapi/v3"
"github.com/unionj-cloud/go-doudou/toolkit/constants"
v3 "github.com/unionj-cloud/go-doudou/toolkit/openapi/v3"
"github.com/unionj-cloud/go-doudou/toolkit/stringutils"
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"strings"
"text/template"
"time"
)
func getSchemaNames(vofile string) []string {
fset := token.NewFileSet()
root, err := parser.ParseFile(fset, vofile, nil, parser.ParseComments)
if err != nil {
panic(err)
}
sc := astutils.NewStructCollector(ExprStringP)
ast.Walk(sc, root)
structs := sc.DocFlatEmbed()
var ret []string
for _, item := range structs {
ret = append(ret, item.Name)
}
return ret
}
func schemasOf(vofile string) []v3.Schema {
fset := token.NewFileSet()
root, err := parser.ParseFile(fset, vofile, nil, parser.ParseComments)
if err != nil {
panic(err)
}
sc := astutils.NewStructCollector(ExprStringP)
ast.Walk(sc, root)
structs := sc.DocFlatEmbed()
var ret []v3.Schema
for _, item := range structs {
ret = append(ret, v3helper.NewSchema(item))
}
return ret
}
func enumsOf(vofile string) (map[string][]astutils.MethodMeta, map[string][]string) {
fset := token.NewFileSet()
root, err := parser.ParseFile(fset, vofile, nil, parser.ParseComments)
if err != nil {
panic(err)
}
sc := astutils.NewEnumCollector(ExprStringP)
ast.Walk(sc, root)
return sc.Methods, sc.Consts
}
const (
get = "GET"
post = "POST"
put = "PUT"
delete = "DELETE"
)
func operationOf(method astutils.MethodMeta, httpMethod string) v3.Operation {
var ret v3.Operation
var params []v3.Parameter
ret.Description = strings.Join(method.Comments, "\n")
// If http method is "POST" and each parameters' type is one of v3.Int, v3.Int64, v3.Bool, v3.String, v3.Float32, v3.Float64,
// then we use application/x-www-form-urlencoded as Content-type, and we make one ref schema from them as request body.
var simpleCnt int
for _, item := range method.Params {
if v3helper.IsBuiltin(item) || item.Type == "context.Context" {
simpleCnt++
}
}
if httpMethod == post && simpleCnt == len(method.Params) {
ret.RequestBody = postFormUrl(method)
} else {
// Simple parameters such as v3.Int, v3.Int64, v3.Bool, v3.String, v3.Float32, v3.Float64 and corresponding Array type
// will be put into query parameter as url search params no matter what http method is.
// Complex parameters such as structs in vo package, map and corresponding slice/array type
// will be put into request body as json content type.
// File and file array parameter will be put into request body as multipart/form-data content type.
upload := false
for _, item := range method.Params {
if item.Type == "context.Context" {
continue
}
pschemaType := v3helper.SchemaOf(item)
if reflect.DeepEqual(pschemaType, v3.FileArray) || pschemaType == v3.File {
upload = true
break
}
}
if upload {
ret.RequestBody = uploadFile(method)
} else {
for _, item := range method.Params {
if item.Type == "context.Context" {
continue
}
pschema := v3helper.CopySchema(item)
v3helper.RefAddDoc(&pschema, strings.Join(item.Comments, "\n"))
required := !v3helper.IsOptional(item.Type)
if v3helper.IsBuiltin(item) {
params = append(params, v3.Parameter{
Name: strcase.ToLowerCamel(item.Name),
In: v3.InQuery,
Schema: &pschema,
Description: pschema.Description,
Required: required,
})
} else {
var content v3.Content
mt := &v3.MediaType{
Schema: &pschema,
}
reflect.ValueOf(&content).Elem().FieldByName("JSON").Set(reflect.ValueOf(mt))
ret.RequestBody = &v3.RequestBody{
Content: &content,
Required: required,
}
}
}
}
}
ret.Parameters = params
ret.Responses = response(method)
return ret
}
func response(method astutils.MethodMeta) *v3.Responses {
var respContent v3.Content
var hasFile bool
var fileDoc string
for _, item := range method.Results {
if item.Type == "*os.File" {
hasFile = true
fileDoc = strings.Join(item.Comments, "\n")
break
}
}
if hasFile {
respContent.Stream = &v3.MediaType{
Schema: &v3.Schema{
Type: v3.StringT,
Format: v3.BinaryF,
Description: fileDoc,
},
}
} else {
title := method.Name + "Resp"
respSchema := v3.Schema{
Type: v3.ObjectT,
Title: title,
Properties: make(map[string]*v3.Schema),
}
for _, item := range method.Results {
if item.Type == "error" {
continue
}
key := item.Name
if stringutils.IsEmpty(key) {
key = item.Type[strings.LastIndex(item.Type, ".")+1:]
}
rschema := v3helper.CopySchema(item)
v3helper.RefAddDoc(&rschema, strings.Join(item.Comments, "\n"))
prop := strcase.ToLowerCamel(key)
respSchema.Properties[prop] = &rschema
if !v3helper.IsOptional(item.Type) {
respSchema.Required = append(respSchema.Required, prop)
}
}
v3helper.Schemas[title] = respSchema
respContent.JSON = &v3.MediaType{
Schema: &v3.Schema{
Ref: "#/components/schemas/" + title,
},
}
}
return &v3.Responses{
Resp200: &v3.Response{
Content: &respContent,
},
}
}
func uploadFile(method astutils.MethodMeta) *v3.RequestBody {
title := method.Name + "Req"
reqSchema := v3.Schema{
Type: v3.ObjectT,
Title: title,
Properties: make(map[string]*v3.Schema),
}
for _, item := range method.Params {
if item.Type == "context.Context" {
continue
}
pschemaType := v3helper.SchemaOf(item)
if reflect.DeepEqual(pschemaType, v3.FileArray) || pschemaType == v3.File || v3helper.IsBuiltin(item) {
pschema := v3helper.CopySchema(item)
pschema.Description = strings.Join(item.Comments, "\n")
prop := strcase.ToLowerCamel(item.Name)
reqSchema.Properties[prop] = &pschema
if !v3helper.IsOptional(item.Type) {
reqSchema.Required = append(reqSchema.Required, prop)
}
}
}
v3helper.Schemas[title] = reqSchema
mt := &v3.MediaType{
Schema: &v3.Schema{
Ref: "#/components/schemas/" + title,
},
}
var content v3.Content
reflect.ValueOf(&content).Elem().FieldByName("FormData").Set(reflect.ValueOf(mt))
return &v3.RequestBody{
Content: &content,
Required: len(reqSchema.Required) > 0,
}
}
func postFormUrl(method astutils.MethodMeta) *v3.RequestBody {
title := method.Name + "Req"
reqSchema := v3.Schema{
Type: v3.ObjectT,
Title: title,
Properties: make(map[string]*v3.Schema),
}
for _, item := range method.Params {
if item.Type == "context.Context" {
continue
}
pschema := v3helper.CopySchema(item)
pschema.Description = strings.Join(item.Comments, "\n")
prop := strcase.ToLowerCamel(item.Name)
reqSchema.Properties[prop] = &pschema
if !v3helper.IsOptional(item.Type) {
reqSchema.Required = append(reqSchema.Required, prop)
}
}
v3helper.Schemas[title] = reqSchema
mt := &v3.MediaType{
Schema: &v3.Schema{
Ref: "#/components/schemas/" + title,
},
}
var content v3.Content
reflect.ValueOf(&content).Elem().FieldByName("FormURL").Set(reflect.ValueOf(mt))
return &v3.RequestBody{
Content: &content,
Required: len(reqSchema.Required) > 0,
}
}
func pathsOf(ic astutils.InterfaceCollector, routePatternStrategy int) map[string]v3.Path {
if len(ic.Interfaces) == 0 {
return nil
}
pathmap := make(map[string]v3.Path)
inter := ic.Interfaces[0]
for _, method := range inter.Methods {
endpoint := fmt.Sprintf("/%s", pattern(method.Name))
if routePatternStrategy == 1 {
endpoint = fmt.Sprintf("/%s/%s", strings.ToLower(inter.Name), noSplitPattern(method.Name))
}
hm := httpMethod(method.Name)
op := operationOf(method, hm)
if val, ok := pathmap[endpoint]; ok {
reflect.ValueOf(&val).Elem().FieldByName(strings.Title(strings.ToLower(hm))).Set(reflect.ValueOf(&op))
pathmap[endpoint] = val
} else {
var v3path v3.Path
reflect.ValueOf(&v3path).Elem().FieldByName(strings.Title(strings.ToLower(hm))).Set(reflect.ValueOf(&op))
pathmap[endpoint] = v3path
}
}
return pathmap
}
var gofileTmpl = `package {{.SvcPackage}}
import "github.com/unionj-cloud/go-doudou/framework/http/onlinedoc"
func init() {
onlinedoc.Oas = ` + "`" + `{{.Doc}}` + "`" + `
}
`
// GenDoc generates OpenAPI 3.0 description json file.
// Not support alias type in vo file.
func GenDoc(dir string, ic astutils.InterfaceCollector, routePatternStrategy int) {
var (
err error
svcname string
docfile string
gofile string
fi os.FileInfo
api v3.API
data []byte
paths map[string]v3.Path
tpl *template.Template
sqlBuf bytes.Buffer
source string
)
svcname = ic.Interfaces[0].Name
docfile = filepath.Join(dir, strings.ToLower(svcname)+"_openapi3.json")
fi, err = os.Stat(docfile)
if err != nil && !os.IsNotExist(err) {
panic(err)
}
if fi != nil {
logrus.Warningln("file " + docfile + " will be overwritten")
}
gofile = filepath.Join(dir, strings.ToLower(svcname)+"_openapi3.go")
fi, err = os.Stat(gofile)
if err != nil && !os.IsNotExist(err) {
panic(err)
}
if fi != nil {
logrus.Warningln("file " + gofile + " will be overwritten")
}
paths = pathsOf(ic, routePatternStrategy)
api = v3.API{
Openapi: "3.0.2",
Info: &v3.Info{
Title: svcname,
Description: strings.Join(ic.Interfaces[0].Comments, "\n"),
Version: fmt.Sprintf("v%s", time.Now().Local().Format(constants.FORMAT10)),
},
Servers: []v3.Server{
{
URL: fmt.Sprintf("http://localhost:%d", 6060),
},
},
Paths: paths,
Components: &v3.Components{
Schemas: v3helper.Schemas,
},
}
data, err = json.Marshal(api)
err = ioutil.WriteFile(docfile, data, os.ModePerm)
if err != nil {
panic(err)
}
if tpl, err = template.New("doc.go.tmpl").Parse(gofileTmpl); err != nil {
panic(err)
}
if err = tpl.Execute(&sqlBuf, struct {
SvcPackage string
Doc string
}{
SvcPackage: ic.Package.Name,
Doc: string(data),
}); err != nil {
panic(err)
}
source = strings.TrimSpace(sqlBuf.String())
astutils.FixImport([]byte(source), gofile)
}
func ParseVo(dir string) {
var (
err error
vos []v3.Schema
allMethods map[string][]astutils.MethodMeta
allConsts map[string][]string
)
vodir := filepath.Join(dir, "vo")
var files []string
err = filepath.Walk(vodir, astutils.Visit(&files))
if err != nil {
panic(err)
}
for _, file := range files {
v3helper.SchemaNames = append(v3helper.SchemaNames, getSchemaNames(file)...)
}
allMethods = make(map[string][]astutils.MethodMeta)
allConsts = make(map[string][]string)
for _, file := range files {
methods, consts := enumsOf(file)
for k, v := range methods {
allMethods[k] = append(allMethods[k], v...)
}
for k, v := range consts {
allConsts[k] = append(allConsts[k], v...)
}
}
for k, v := range allMethods {
if astutils.IsEnum(v) {
v3helper.Enums[k] = astutils.EnumMeta{
Name: k,
Values: allConsts[k],
}
}
}
for _, file := range files {
vos = append(vos, schemasOf(file)...)
}
for _, item := range vos {
v3helper.Schemas[item.Title] = item
}
}