/
to_gocode.go
345 lines (302 loc) · 7.75 KB
/
to_gocode.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
package swagger
import (
"bytes"
"errors"
"fmt"
"html/template"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/go-openapi/spec"
"github.com/gookit/gcli/v2"
"github.com/gookit/goutil/dump"
"github.com/gookit/goutil/fsutil"
"github.com/gookit/goutil/strutil"
"github.com/gookit/ini/v2"
"github.com/gookit/slog"
)
const stdoutName = "stdout"
// refer links:
// https://petstore.swagger.io/
// https://petstore3.swagger.io/
var swag2codeOpts = struct {
tplDir string
writer io.Writer
// options
OutDir string
PkgName string
SwagFile string
Template string
// GroupType paths group type:
// none - dont group
// path - by path
// tag - by tag name
GroupType string
GroupSuffix string
ActionSuffix string
SpecPaths gcli.Strings
}{
tplDir: "resource/templates", // default dir
}
var (
tplEngine *template.Template
tplSuffix = ".tpl"
tplFuncs = template.FuncMap{
"join": strings.Join,
"lower": strings.ToLower,
"upper": strings.ToUpper,
"upFirst": strutil.UpperFirst,
}
)
var GenCode = &gcli.Command{
Name: "swag2go",
Aliases: []string{"swag2code"},
UseFor: "generate go API service codes by swagger.yaml or swagger.json",
Config: func(c *gcli.Command) {
c.StrOpt(&swag2codeOpts.SwagFile,
"swagger-file",
"f",
"api",
"the swagger doc filepath",
)
c.StrOpt(&swag2codeOpts.PkgName, "pgk-name", "p", "./swagger.json", "the generated package name")
c.StrOpt(&swag2codeOpts.OutDir,
"output",
"o",
"./gocodes",
`the output directory for generated codes
if input 'stdout' will print codes on terminal
`)
c.StrVar(&swag2codeOpts.Template, gcli.FlagMeta{
Name: "template",
Desc: "the template name for generate codes",
Shorts: []string{"t"},
DefVal: "rux-controller",
})
c.StrVar(&swag2codeOpts.GroupType, gcli.FlagMeta{
Name: "group-type",
Desc: `the code generate group type. allow:
none - dont generate group struct
path - group by path name
tag - group by tag name
`,
DefVal: "tag",
Validator: func(val string) error {
ss := []string{"none", "path", "tag"}
for _, s := range ss {
if s == val {
return nil
}
}
return fmt.Errorf("'group-type' must one of %v", ss)
},
})
c.StrVar(&swag2codeOpts.GroupSuffix, gcli.FlagMeta{
Name: "group-suffix",
Desc: "Add suffix for group name. eg: API, Controller",
DefVal: "API",
})
c.StrVar(&swag2codeOpts.ActionSuffix, gcli.FlagMeta{
Name: "action-suffix",
Desc: "Add suffix for action name. eg: Action, Method",
})
c.VarOpt(&swag2codeOpts.SpecPaths, "paths", "", "only generate for the given spec paths")
c.Examples = `
{$fullCmd} --paths /anything /blog/{id}
{$fullCmd} -f testdata/swagger.json --paths /anything/{anything} --group-type path -o stdout
`
},
Func: func(c *gcli.Command, args []string) (err error) {
// load swagger doc file
if err := loadDocFile(swag2codeOpts.SwagFile); err != nil {
return err
}
// check paths
if len(swagger.SwaggerProps.Paths.Paths) == 0 {
return errors.New("API doc 'paths' is empty")
}
// load and parse template
if err = loadAndParseTemplate(); err != nil {
return
}
dump.Config(func(d *dump.Dumper) {
d.MaxDepth = 8
})
outDir := swag2codeOpts.OutDir
if outDir != stdoutName && fsutil.IsDir(outDir) {
slog.Info("create the output directory:", outDir)
err = os.MkdirAll(outDir, 0664)
if err != nil {
return err
}
}
// only generate special paths.
if len(swag2codeOpts.SpecPaths) > 0 {
slog.Info("will only generate for the paths: ", swag2codeOpts.SpecPaths)
for _, path := range swag2codeOpts.SpecPaths {
if pathItem, ok := swagger.SwaggerProps.Paths.Paths[path]; ok {
err = generateByPathItem(path, pathItem)
if err != nil {
return
}
} else { // path not exists
slog.Errorf("- the path '%s' is not exists on docs, skip gen", path)
}
}
} else {
for path, pathItem := range swagger.SwaggerProps.Paths.Paths {
err = generateByPathItem(path, pathItem)
if err != nil {
return
}
}
}
return
},
}
func loadAndParseTemplate() error {
tplDir := ini.String("swag2code.templateDir")
if tplDir != "" {
swag2codeOpts.tplDir = tplDir
}
tplFile := swag2codeOpts.Template
if !strings.HasSuffix(tplFile, tplSuffix) {
tplFile += tplSuffix
}
if !fsutil.IsFile(tplFile) {
// find from default template dir
tplFile = filepath.Join(swag2codeOpts.tplDir, tplFile)
if !fsutil.IsFile(tplFile) {
return errors.New("template file not exists, file: " + swag2codeOpts.Template)
}
}
slog.Infof("use the template file: %s", tplFile)
bts, err := ioutil.ReadFile(tplFile)
if err != nil {
return err
}
root := template.New("swag2code")
slog.Info("do parsing the template contents")
// tpl, err := tplEngine.New(swag2codeOpts.Template).Funcs(tplFuncs).ParseFiles(tplFile)
tplEngine, err = root.New(swag2codeOpts.Template).Funcs(tplFuncs).Parse(string(bts))
return err
}
type GroupData struct {
GroupName string
GroupDesc string
GroupPath string
PkgName string
Actions []ActionItem
TagMap map[string]string
}
func (gd *GroupData) addActionItem(method string, op *spec.Operation) {
desc := op.Description
if desc == "" {
if op.Summary != "" {
desc = op.Summary
} else {
var prefix string
switch method {
case http.MethodGet:
prefix = "query"
case http.MethodPost:
prefix = "create"
case http.MethodPut, http.MethodPatch:
prefix = "update"
default:
prefix = "do"
}
desc = prefix + " operation for the " + gd.GroupName
}
}
// dump.P(op.OperationProps)
actionName := strutil.UpperFirst(strings.ToLower(method))
item := ActionItem{
Path: "",
Tags: op.Tags,
METHOD: method,
// other
MethodName: actionName,
MethodDesc: desc,
}
gd.Actions = append(gd.Actions, item)
// load tag desc
if gd.GroupDesc == "" && len(op.Tags) > 0 {
tag, ok := swagger.GetTagInfo(op.Tags[0])
if ok {
gd.GroupDesc = tag.Description
}
}
}
func (gd *GroupData) appendTags(tags []string) {
}
// ActionItem struct
type ActionItem struct {
// Path route path
Path string
Tags []string
// METHOD the request METHOD. eg. GET
METHOD string
// MethodName the action method name
MethodName string
// MethodDesc the action method desc
MethodDesc string
}
// TagComments line
func (a ActionItem) TagComments() string {
if len(a.Tags) == 0 {
return ""
}
return fmt.Sprintf("\n// @Tags %s", a.Tags[0])
}
func generateByPathItem(path string, pathItem spec.PathItem) (err error) {
var subpath string
tmpPath := strings.Trim(path, "/")
grpName := tmpPath
if strings.ContainsRune(tmpPath, '/') {
nodes := strings.SplitN(tmpPath, "/", 2)
grpName, subpath = nodes[0], nodes[1]
}
// dump.P(group, subpath)
// dump.P(pathItem)
slog.Infof("will generate for the path '%s'. (group: %s, subpath: %s)", path, grpName, subpath)
data := &GroupData{
PkgName: swag2codeOpts.PkgName,
GroupName: grpName,
GroupPath: path,
}
piProps := pathItem.PathItemProps
if piProps.Get != nil {
data.addActionItem(http.MethodGet, piProps.Get)
}
if piProps.Put != nil {
data.addActionItem(http.MethodPut, piProps.Put)
}
if piProps.Head != nil {
data.addActionItem(http.MethodPut, piProps.Head)
}
if piProps.Post != nil {
data.addActionItem(http.MethodPost, piProps.Post)
}
if piProps.Patch != nil {
data.addActionItem(http.MethodPatch, piProps.Patch)
}
if piProps.Delete != nil {
data.addActionItem(http.MethodDelete, piProps.Delete)
}
// dump.P(pathItem.PathItemProps)
slog.Info("do rendering the template contents")
buf := new(bytes.Buffer)
err = tplEngine.Execute(buf, data)
if err == nil {
slog.Infof("output the generated contents to: %s", swag2codeOpts.OutDir)
// dump to stdout
if swag2codeOpts.OutDir == stdoutName {
_, err = buf.WriteTo(os.Stdout)
}
}
return
}