-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
147 lines (125 loc) · 3.17 KB
/
handler.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
package handler
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/cheetah-fun-gs/goplus/gostyle"
filepathplus "github.com/cheetah-fun-gs/goplus/path/filepath"
)
// FileRender 处理器文件渲染对象
type FileRender struct {
Imports []string // handlersxxx "%s/internal/biz/handler/xxx"
Handlers []*Render
}
// Render 处理器渲染对象
type Render struct {
PackageTitle string // 首字母大写
PackageName string // 全小写
HandlerName string
}
var handlerChecks = map[string]ast.ObjKind{
"Req": ast.Typ,
"Resp": ast.Typ,
}
func findHandlerRender(packageName string, objs []*ast.Object) (bool, *Render) {
var handlerName string
for _, o := range objs {
if o.Kind == ast.Fun && strings.HasSuffix(o.Name, "Handle") {
if len(o.Name) <= 6 {
fmt.Printf("%s's name is too short, ignore it\n", o.Name)
continue
}
split := strings.Split(o.Name, "")
handlerName = strings.Join(split[:len(split)-6], "")
break
}
}
if handlerName == "" { // 没有发现 handler
return false, nil
}
checked := map[string]bool{}
for k := range handlerChecks {
checked[k] = false
}
for k, v := range handlerChecks {
for _, o := range objs {
if o.Kind == v && o.Name == fmt.Sprintf("%s%s", handlerName, k) {
checked[k] = true
}
}
}
isCheckPass := true
for k, v := range checked {
if !v {
fmt.Printf("%sHandle has no %s%s!\n", handlerName, handlerName, k)
isCheckPass = false
}
}
if !isCheckPass {
return false, nil
}
return true, &Render{
PackageTitle: gostyle.FormatToCamelCase(packageName),
PackageName: packageName,
HandlerName: handlerName,
}
}
// Scan 扫描
func Scan(projectPath, packageName string) (*FileRender, error) {
goFilePaths, err := filepathplus.Files(filepath.Join(projectPath, "internal", "biz", "handler"))
if err != nil {
return nil, err
}
var imports = map[string]string{}
var handlers = []*Render{}
for _, goFilePath := range goFilePaths {
filePackageName, objs, err := astScan(goFilePath)
if err != nil {
return nil, err
}
ok, handlerRender := findHandlerRender(filePackageName, objs)
if ok {
if _, ok := imports[filePackageName]; !ok {
imports[filePackageName] = fmt.Sprintf("%s/internal/biz/handler/%s", packageName, filePackageName)
}
handlers = append(handlers, handlerRender)
}
}
handleFileRender := &FileRender{
Imports: []string{},
Handlers: handlers,
}
for k, v := range imports {
handleFileRender.Imports = append(handleFileRender.Imports,
fmt.Sprintf(`%s "%s"`, k, v))
}
return handleFileRender, nil
}
func astScan(goFilePath string) (packageName string, objs []*ast.Object, err error) {
goFileFd, err := os.Open(goFilePath)
if err != nil {
return
}
defer goFileFd.Close()
fileData, err := ioutil.ReadAll(goFileFd)
if err != nil {
return
}
// Create the AST by parsing src.
fset := token.NewFileSet() // positions are relative to fset
f, err := parser.ParseFile(fset, filepath.Base(goFilePath), string(fileData), parser.ParseComments)
if err != nil {
return
}
packageName = f.Name.Name
objs = []*ast.Object{}
for _, o := range f.Scope.Objects {
objs = append(objs, o)
}
return
}