forked from go-gorm/gen
/
method.go
145 lines (125 loc) · 3.56 KB
/
method.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
package parser
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"strings"
)
func DefaultMethodTableName(structName string) *Method {
return &Method{
Receiver: Param{IsPointer: true, Type: structName},
MethodName: "TableName",
Doc: fmt.Sprint("TableName ", structName, "'s table name "),
Result: []Param{{Type: "string"}},
Body: fmt.Sprintf("{\n\treturn TableName%s\n} ", structName),
}
}
// Method Apply to query struct and base struct custom method
type Method struct {
Receiver Param
MethodName string
Doc string
Params []Param
Result []Param
Body string
}
// FuncSign function signature
func (m Method) FuncSign() string {
return fmt.Sprintf("%s(%s) (%s)", m.MethodName, m.GetParamInTmpl(), m.GetResultParamInTmpl())
}
// GetBaseStructTmpl return method bind info string
func (m *Method) GetBaseStructTmpl() string {
return m.Receiver.TmplString()
}
// GetParamInTmpl return param list
func (m *Method) GetParamInTmpl() string {
return paramToString(m.Params)
}
// GetResultParamInTmpl return result list
func (m *Method) GetResultParamInTmpl() string {
return paramToString(m.Result)
}
// paramToString param list to string used in tmpl
func paramToString(params []Param) string {
res := make([]string, len(params))
for i, param := range params {
res[i] = param.TmplString()
}
return strings.Join(res, ",")
}
// DocComment return comment sql add "//" every line
func (m *Method) DocComment() string {
return strings.Replace(strings.TrimSpace(m.Doc), "\n", "\n//", -1)
}
// DIYMethods user Custom methods bind to db base struct
type DIYMethods struct {
BaseStructType string
MethodName string
pkgPath string
currentFile string
pkgFiles []string
Methods []*Method
}
func (m *DIYMethods) parserPath(path string) error {
pathList := strings.Split(path, ".")
if len(pathList) < 3 {
return fmt.Errorf("parser diy method error")
}
m.pkgPath = strings.Join(pathList[:len(pathList)-2], ".")
methodName := pathList[len(pathList)-1]
m.MethodName = methodName[:len(methodName)-3]
structName := pathList[len(pathList)-2]
m.BaseStructType = strings.Trim(structName, "()*")
return nil
}
// Visit ast visit function
func (m *DIYMethods) Visit(n ast.Node) (w ast.Visitor) {
switch t := n.(type) {
case *ast.FuncDecl:
// check base struct and method name is expect
structMeta := getParamList(t.Recv)
if len(structMeta) != 1 {
return
}
if structMeta[0].Type != m.BaseStructType {
return
}
// if m.MethodName is null will generate all methods
if m.MethodName != "" && m.MethodName != t.Name.Name {
return
}
// use ast read bind start package is UNDEFINED ,set it null string
structMeta[0].Package = ""
m.Methods = append(m.Methods, &Method{
Receiver: structMeta[0],
MethodName: t.Name.String(),
Doc: t.Doc.Text(),
Body: getBody(m.currentFile, int(t.Body.Pos()), int(t.Body.End())),
Params: getParamList(t.Type.Params),
Result: getParamList(t.Type.Results),
})
}
return m
}
// read old file get method body
func getBody(fileName string, start, end int) string {
f1, err := ioutil.ReadFile(fileName)
if err != nil {
return "{}"
}
return string(f1[start-1 : end-1])
}
// LoadMethods ast read file get diy method
func (m *DIYMethods) LoadMethods() error {
for _, filename := range m.pkgFiles {
f, err := parser.ParseFile(token.NewFileSet(), filename, nil, parser.ParseComments)
if err != nil {
return fmt.Errorf("can't parse file %q: %s", filename, err)
}
m.currentFile = filename
ast.Walk(m, f)
}
return nil
}