-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathfuncs.go
220 lines (197 loc) · 5.59 KB
/
funcs.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
package codemillx
import (
"fmt"
"reflect"
"github.com/hudangwei/codemillx/codemill"
)
var funcs = make(Funcs)
func init() {
funcs["Package"] = reflect.ValueOf(Package)
funcs["Func"] = reflect.ValueOf(Func)
funcs["Method"] = reflect.ValueOf(Method)
funcs["Interface"] = reflect.ValueOf(Interface)
funcs["Type"] = reflect.ValueOf(Type)
funcs["Field"] = reflect.ValueOf(Field)
funcs["Result"] = reflect.ValueOf(Result)
funcs["InResult"] = reflect.ValueOf(InResult)
funcs["OutResult"] = reflect.ValueOf(OutResult)
funcs["Param"] = reflect.ValueOf(Param)
funcs["InParam"] = reflect.ValueOf(InParam)
funcs["OutParam"] = reflect.ValueOf(OutParam)
funcs["IsReceiver"] = reflect.ValueOf(IsReceiver)
funcs["InIsReceiver"] = reflect.ValueOf(InIsReceiver)
funcs["OutIsReceiver"] = reflect.ValueOf(OutIsReceiver)
}
type CommentGroupMetaData struct {
ModelKind string
CommentFuncs []CommentFunc
}
type CommentFunc struct {
Name string
Params []interface{}
}
type Funcs map[string]reflect.Value
func (f Funcs) Call(name string, modelKind string, sel *codemill.Selector, params ...interface{}) (result []reflect.Value, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("%v", r)
}
}()
if _, ok := f[name]; !ok {
err = fmt.Errorf("%s does not exist", name)
return
}
if len(params) != f[name].Type().NumIn()-2 {
err = fmt.Errorf("The number of params is not adapted")
return
}
in := make([]reflect.Value, 0)
//添加2个默认参数(modelKind string, sel *codemill.Selector)
in = append(in, reflect.ValueOf(modelKind))
in = append(in, reflect.ValueOf(sel))
for _, param := range params {
in = append(in, reflect.ValueOf(param))
}
result = f[name].Call(in)
return
}
func Package(modelKind string, sel *codemill.Selector, pkgPath string) {
if sel.Kind == codemill.SelectorKindFunc {
if fn, ok := sel.Qualifier.(*codemill.FuncQualifier); ok {
fn.PkgPath = pkgPath
}
} else if sel.Kind == codemill.SelectorKindStruct {
if v, ok := sel.Qualifier.(*codemill.StructQualifier); ok {
v.PkgPath = pkgPath
}
} else if sel.Kind == codemill.SelectorKindType {
if v, ok := sel.Qualifier.(*codemill.TypeQualifier); ok {
v.PkgPath = pkgPath
}
}
}
func Func(modelKind string, sel *codemill.Selector, funcName string) {
if sel.Kind == codemill.SelectorKindFunc {
if fn, ok := sel.Qualifier.(*codemill.FuncQualifier); ok {
fn.FunctionName = funcName
}
}
}
func Method(modelKind string, sel *codemill.Selector, meth, funcName string) {
if sel.Kind == codemill.SelectorKindFunc {
if fn, ok := sel.Qualifier.(*codemill.FuncQualifier); ok {
fn.Receiver = meth
fn.FunctionName = funcName
}
}
}
func Interface(modelKind string, sel *codemill.Selector, interfaceName, funcName string) {
if sel.Kind == codemill.SelectorKindFunc {
if fn, ok := sel.Qualifier.(*codemill.FuncQualifier); ok {
fn.Interface = interfaceName
fn.FunctionName = funcName
}
}
}
func Type(modelKind string, sel *codemill.Selector, typeName string) {
if sel.Kind == codemill.SelectorKindType {
if v, ok := sel.Qualifier.(*codemill.TypeQualifier); ok {
v.TypeName = typeName
}
}
}
func Field(modelKind string, sel *codemill.Selector, structName string, fields []string) {
if sel.Kind == codemill.SelectorKindStruct {
if v, ok := sel.Qualifier.(*codemill.StructQualifier); ok {
v.StructName = structName
v.Fields = fields
}
}
}
func Result(modelKind string, sel *codemill.Selector, args []int) {
if sel.Kind == codemill.SelectorKindFunc {
if fn, ok := sel.Qualifier.(*codemill.FuncQualifier); ok {
if len(args) == 0 {
fn.Results = []int{0}
} else {
fn.Results = args
}
}
}
}
func InResult(modelKind string, sel *codemill.Selector, args []int) {
if sel.Kind == codemill.SelectorKindFunc {
if fn, ok := sel.Qualifier.(*codemill.FuncQualifier); ok {
if len(args) == 0 {
fn.Inp.Results = []int{0}
} else {
fn.Inp.Results = args
}
}
}
}
func OutResult(modelKind string, sel *codemill.Selector, args []int) {
if sel.Kind == codemill.SelectorKindFunc {
if fn, ok := sel.Qualifier.(*codemill.FuncQualifier); ok {
if len(args) == 0 {
fn.Out.Results = []int{0}
} else {
fn.Out.Results = args
}
}
}
}
func Param(modelKind string, sel *codemill.Selector, args []int) {
if sel.Kind == codemill.SelectorKindFunc {
if fn, ok := sel.Qualifier.(*codemill.FuncQualifier); ok {
if len(args) == 0 {
fn.Parameters = []int{0}
} else {
fn.Parameters = args
}
}
}
}
func InParam(modelKind string, sel *codemill.Selector, args []int) {
if sel.Kind == codemill.SelectorKindFunc {
if fn, ok := sel.Qualifier.(*codemill.FuncQualifier); ok {
if len(args) == 0 {
fn.Inp.Parameters = []int{0}
} else {
fn.Inp.Parameters = args
}
}
}
}
func OutParam(modelKind string, sel *codemill.Selector, args []int) {
if sel.Kind == codemill.SelectorKindFunc {
if fn, ok := sel.Qualifier.(*codemill.FuncQualifier); ok {
if len(args) == 0 {
fn.Out.Parameters = []int{0}
} else {
fn.Out.Parameters = args
}
}
}
}
func IsReceiver(modelKind string, sel *codemill.Selector) {
if sel.Kind == codemill.SelectorKindFunc {
if fn, ok := sel.Qualifier.(*codemill.FuncQualifier); ok {
fn.IsReceiver = true
}
}
}
func InIsReceiver(modelKind string, sel *codemill.Selector) {
if sel.Kind == codemill.SelectorKindFunc {
if fn, ok := sel.Qualifier.(*codemill.FuncQualifier); ok {
fn.Inp.IsReceiver = true
}
}
}
func OutIsReceiver(modelKind string, sel *codemill.Selector) {
if sel.Kind == codemill.SelectorKindFunc {
if fn, ok := sel.Qualifier.(*codemill.FuncQualifier); ok {
fn.Out.IsReceiver = true
}
}
}