forked from simplejia/wsp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
464 lines (429 loc) · 10.7 KB
/
main.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
// A partner for golang webserver
// Created by simplejia [7/2016]
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"go/ast"
"go/build"
"go/parser"
"go/printer"
"go/token"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
)
var (
controllerPath string
filterPath string
lowerFlag bool
snakeFlag bool
shortFlag bool
buf = new(bytes.Buffer)
)
type filter struct {
ImportPath string
PackageName string
FuncName string
Params map[string]interface{}
}
type ca struct {
ImportPath string
PackageName string
RelativePath string
TypeName string
MethodName string
PreFilters []*filter
PostFilters []*filter
}
func exit() {
println(buf.String())
println("Failed!")
os.Exit(-1)
}
func prettyPrint(in interface{}) string {
bs, err := json.MarshalIndent(in, ".", " ")
if err != nil {
return ""
}
return string(bs)
}
func lower(str string) string {
if len(str) == 0 {
return ""
}
return strings.ToLower(str[0:1]) + str[1:]
}
func snake(src string) string {
thisUpper := false
prevUpper := false
buf := bytes.NewBufferString("")
for i, v := range src {
if v >= 'A' && v <= 'Z' {
thisUpper = true
} else {
thisUpper = false
}
if i > 0 && thisUpper && !prevUpper {
buf.WriteRune('_')
}
prevUpper = thisUpper
buf.WriteRune(v)
}
return strings.ToLower(buf.String())
}
func getImportPath(file string) string {
dir := filepath.Dir(file)
for _, path := range build.Default.SrcDirs() {
path, _ = filepath.Abs(path)
path, _ = filepath.EvalSymlinks(path)
dir, _ = filepath.EvalSymlinks(dir)
if strings.HasPrefix(dir, path) {
return strings.Replace(dir[len(path)+1:], string(filepath.Separator), "/", -1)
}
}
fmt.Fprintf(buf, "get import path fail\n")
exit()
return ""
}
func getRelativePath(controllerPath, file string) string {
pathP, _ := filepath.Abs(controllerPath)
pathP, _ = filepath.EvalSymlinks(pathP)
pathC := filepath.Dir(file)
pathC, _ = filepath.EvalSymlinks(pathC)
if strings.HasPrefix(pathC, pathP) {
return pathC[len(pathP):]
}
return ""
}
func getFilters(doc string, es []*ca) (preFilters, postFilters []*filter) {
if len(doc) == 0 {
return
}
f1 := func(token string) ([]string, map[string]map[string]interface{}) {
token = "[" + token + "]"
arr := []interface{}{}
err := json.Unmarshal([]byte(token), &arr)
if err != nil {
fmt.Fprintf(buf, "Filter annotation: %s, error: %v [must be json array]\n", doc, err)
exit()
}
arrRet, mapRet := []string{}, map[string]map[string]interface{}{}
for _, e := range arr {
switch ev := e.(type) {
case string:
arrRet = append(arrRet, ev)
mapRet[ev] = nil
case map[string]interface{}:
for k, v := range ev {
vv := map[string]interface{}{}
d, _ := json.Marshal(v)
err := json.Unmarshal(d, &vv)
if err != nil {
fmt.Fprintf(buf, "Filter annotation: %s[%s], error: %v [must be json object]\n", doc, d, err)
exit()
}
arrRet = append(arrRet, k)
mapRet[k] = vv
break
}
}
}
return arrRet, mapRet
}
f2 := func(token string) (filters []*filter) {
token1 := token + "("
pos1 := strings.Index(doc, token1)
if pos1 == -1 {
return nil
}
token2 := ")"
pos2 := strings.Index(doc[pos1+len(token1):], token2)
if pos2 == -1 {
return nil
}
pos2 += pos1 + len(token1)
partial := doc[pos1+len(token1) : pos2]
arr, hmap := f1(partial)
for _, name := range arr {
params := hmap[name]
if !func() bool {
for _, e := range es {
if e.MethodName == name {
filters = append(filters, &filter{
ImportPath: e.ImportPath,
PackageName: e.PackageName,
FuncName: e.MethodName,
Params: params,
})
return true
}
}
return false
}() {
fmt.Fprintf(buf, "Filter config fail, name not right: %s", name)
exit()
}
}
return
}
preFilters, postFilters = f2("@prefilter"), f2("@postfilter")
return
}
func parseGo4Controller(file string, es4Filter []*ca) (es []*ca, err error) {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, file, nil, parser.ParseComments)
if err != nil {
return
}
for _, decl := range f.Decls {
mdecl, ok := decl.(*ast.FuncDecl)
if !ok {
continue
}
if !mdecl.Name.IsExported() {
continue
}
str := new(bytes.Buffer)
printer.Fprint(str, fset, mdecl)
matches := regexp.MustCompile(`func \(.+? \*?(.+?)\) .+?\(.+? .+?\.ResponseWriter, .+? \*.+?.Request\) {(.|\n)*}`).FindStringSubmatch(str.String())
if len(matches) == 0 {
continue
}
preFilters, postFilters := getFilters(mdecl.Doc.Text(), es4Filter)
es = append(es, &ca{
ImportPath: getImportPath(file),
PackageName: f.Name.String(),
RelativePath: getRelativePath(controllerPath, file),
TypeName: matches[1],
MethodName: mdecl.Name.String(),
PreFilters: preFilters,
PostFilters: postFilters,
})
}
return
}
func parseGo4Filter(file string) (es []*ca, err error) {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, file, nil, 0)
if err != nil {
return
}
for _, decl := range f.Decls {
mdecl, ok := decl.(*ast.FuncDecl)
if !ok {
continue
}
if !mdecl.Name.IsExported() {
continue
}
str := new(bytes.Buffer)
printer.Fprint(str, fset, mdecl)
if matched, _ := regexp.MatchString(`func .+?\(.+? .+?\.ResponseWriter, .+? \*.+?.Request, .+? map\[string\]interface{}\) bool {(.|\n)*}`, str.String()); !matched {
continue
}
es = append(es, &ca{
ImportPath: getImportPath(file),
PackageName: f.Name.String(),
MethodName: mdecl.Name.String(),
})
}
return
}
func getGoFiles(path string) (files []string, err error) {
err = filepath.Walk(path, func(path string, info os.FileInfo, err error) (reterr error) {
if err != nil {
reterr = err
return
}
if info.IsDir() {
return
}
if filepath.Ext(path) != ".go" {
return
}
if strings.HasSuffix(path, "_test.go") {
return
}
absPath, err := filepath.Abs(path)
if err != nil {
reterr = err
return
}
files = append(files, absPath)
return
})
if err != nil {
return
}
return
}
func gen(es []*ca) (err error) {
s := ""
s += "// generated by wsp, DO NOT EDIT.\n\n"
s += "package main\n\n"
s += "import \"net/http\"\n"
s += "import \"time\"\n"
added := map[string]bool{}
for _, e := range es {
if added[e.ImportPath] {
continue
}
added[e.ImportPath] = true
s += "import \"" + e.ImportPath + "\"\n"
}
for _, e := range es {
for _, f := range e.PreFilters {
if added[f.ImportPath] {
continue
}
added[f.ImportPath] = true
s += "import \"" + f.ImportPath + "\"\n"
}
for _, f := range e.PostFilters {
if added[f.ImportPath] {
continue
}
added[f.ImportPath] = true
s += "import \"" + f.ImportPath + "\"\n"
}
}
f := func(path string, filters []*filter, space string) (str string) {
for _, filter := range filters {
params := "map[string]interface{}{"
a := []string{}
for k, v := range filter.Params {
switch vv := v.(type) {
case bool:
a = append(a, "\""+k+"\": "+strconv.FormatBool(vv))
case string:
a = append(a, "\""+k+"\": "+"\""+vv+"\"")
case float64:
a = append(a, "\""+k+"\": "+strconv.FormatFloat(vv, 'f', -1, 64))
default:
fmt.Fprintf(buf, "Filter annotation: %v[%v], not support type\n", filter.Params, vv)
exit()
}
}
a = append(a, "\"__T__\": t")
a = append(a, "\"__C__\": c")
a = append(a, "\"__E__\": e")
a = append(a, "\"__P__\": "+"\""+path+"\"")
params += strings.Join(a, ", ")
params += "}"
str += space + "if ok := " + filter.PackageName + "." + filter.FuncName + "(w, r, " + params + "); !ok {\n"
str += space + "\treturn\n"
str += space + "}\n"
}
return
}
s += "\n"
s += "func init() {\n"
for _, e := range es {
s += "\thttp.HandleFunc(\""
path := ""
relativePath := ""
if !shortFlag {
relativePath = e.RelativePath
}
if lowerFlag {
path = lower(relativePath) + "/" + lower(e.TypeName) + "/" + lower(e.MethodName)
} else if snakeFlag {
path = snake(relativePath) + "/" + snake(e.TypeName) + "/" + snake(e.MethodName)
} else {
path = relativePath + "/" + e.TypeName + "/" + e.MethodName
}
s += path
s += "\", func(w http.ResponseWriter, r *http.Request) {\n"
s += "\t\tt := time.Now()\n"
s += "\t\t_ = t\n"
s += "\t\tvar e interface{}\n"
s += "\t\tc := new(" + e.PackageName + "." + e.TypeName + ")\n"
s += "\t\tdefer func() {\n"
s += "\t\t\te = recover()\n"
s += f(path, e.PostFilters, "\t\t\t")
s += "\t\t}()\n"
s += f(path, e.PreFilters, "\t\t")
s += "\t\tc." + e.MethodName + "(w, r)\n"
s += "\t})\n\n"
}
s += "}"
ioutil.WriteFile("WSP.go", []byte(s), 0644)
return
}
func main() {
flag.StringVar(&controllerPath, "c", "controller", "Specify the directory of controller path")
flag.StringVar(&filterPath, "f", "filter", "Specify the directory of filter path")
flag.BoolVar(&lowerFlag, "l", false, "Controller/Action, lower first alpha")
flag.BoolVar(&snakeFlag, "s", false, "Controller/Action, snake case pattern")
flag.BoolVar(&shortFlag, "d", false, "Do not add directory name")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "A partner for golang webserver\n")
fmt.Fprintf(os.Stderr, "version: 1.7, Created by simplejia [7/2016]\n\n")
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
// process filter
es4Filter := func() (ret []*ca) {
files, err := getGoFiles(filterPath)
if err != nil {
// 允许filter文件夹不存在
if os.IsNotExist(err) {
return
}
fmt.Fprintln(buf, "Scan source file error", err)
exit()
}
fmt.Fprintln(buf, "\nScan files(filter):")
for pos, file := range files {
fmt.Fprintln(buf, pos, file)
}
fmt.Fprintln(buf, "\nProcess files(filter):")
for pos, file := range files {
fmt.Fprintln(buf, pos, file)
es, err := parseGo4Filter(file)
if err != nil {
fmt.Fprintln(buf, "Parse source file error", err)
exit()
}
ret = append(ret, es...)
}
fmt.Fprintln(buf, "\nMethod(Func) list(filter):")
fmt.Fprintln(buf, prettyPrint(ret))
return
}()
// process controller
es4Controller := func() (ret []*ca) {
files, err := getGoFiles(controllerPath)
if err != nil {
fmt.Fprintln(buf, "Scan source file error", err)
exit()
}
fmt.Fprintln(buf, "\nScan files(controller):")
for pos, file := range files {
fmt.Fprintln(buf, pos, file)
}
fmt.Fprintln(buf, "\nProcess files(controller):")
for pos, file := range files {
fmt.Fprintln(buf, pos, file)
es, err := parseGo4Controller(file, es4Filter)
if err != nil {
fmt.Fprintln(buf, "Parse source file error", err)
exit()
}
ret = append(ret, es...)
}
fmt.Fprintln(buf, "\nMethod list(controller):")
fmt.Fprintln(buf, prettyPrint(ret))
return
}()
gen(es4Controller)
println("Success!")
return
}