-
Notifications
You must be signed in to change notification settings - Fork 1
/
querywhere.go
101 lines (91 loc) · 3.01 KB
/
querywhere.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
package main
import (
"html/template"
"github.com/fishedee/tools/exception"
)
// QueryWhereGen QueryWhereGen
func QueryWhereGen(request QueryGenRequest) *QueryGenResponse {
args := request.args
line := request.pkg.FileSet().Position(request.expr.Pos()).String()
//解析第一个参数
firstArgSlice := getSliceType(line, args[0].Type)
firstArgElem := firstArgSlice.Elem()
//解析第二个参数
secondArgFunc := getFunctionType(line, args[1].Type)
secondArgFuncArguments := getArgumentType(line, secondArgFunc)
secondArgFuncResults := getReturnType(line, secondArgFunc)
if len(secondArgFuncArguments) != 1 {
exception.Throw(1, "%v:selector should be single argument")
}
if len(secondArgFuncResults) != 1 {
exception.Throw(1, "%v:selector should be single return")
}
secondArgFuncArgument := secondArgFuncArguments[0]
secondArgFuncResult := secondArgFuncResults[0]
if firstArgElem.String() != secondArgFuncArgument.String() {
exception.Throw(1, "%v:second selector argument should be equal with first argument")
}
if secondArgFuncResult.String() != "bool" {
exception.Throw(1, "%v:second selector return should be bool type")
}
//生成函数
signature := getFunctionSignature(line, args, []bool{false, false})
if hasQueryWhereGenerate[signature] == true {
return nil
}
hasQueryWhereGenerate[signature] = true
importPackage := map[string]bool{}
setImportPackage(line, secondArgFuncArgument, importPackage)
argumentDefine := getFunctionArgumentCode(line, args, []bool{false, false})
funcBody := excuteTemplate(queryWhereFuncTmpl, map[string]string{
"signature": signature,
"firstArgElemType": getTypeDeclareCode(line, firstArgElem),
"secondArgType": getTypeDeclareCode(line, secondArgFunc),
})
initBody := excuteTemplate(queryWhereInitTmpl, map[string]string{
"signature": signature,
"argumentDefine": argumentDefine,
})
return &QueryGenResponse{
importPackage: importPackage,
funcName: whereFuncPrefix + signature,
funcBody: funcBody,
initBody: initBody,
}
}
const (
whereFuncPrefix = "queryWhereV"
)
var (
queryWhereFuncTmpl *template.Template
queryWhereInitTmpl *template.Template
hasQueryWhereGenerate map[string]bool
)
func init() {
var err error
queryWhereFuncTmpl, err = template.New("name").Parse(`
func ` + whereFuncPrefix + `{{ .signature }}(data interface{},whereFunctor interface{})interface{}{
dataIn := data.([]{{ .firstArgElemType }})
whereFunctorIn := whereFunctor.({{ .secondArgType }})
result := make([]{{ .firstArgElemType }},0,len(dataIn))
for _,single := range dataIn{
shouldStay := whereFunctorIn(single)
if shouldStay == true {
result = append(result,single)
}
}
return result
}
`)
if err != nil {
panic(err)
}
queryWhereInitTmpl, err = template.New("name").Parse(`
query.WhereMacroRegister({{.argumentDefine}},` + whereFuncPrefix + `{{.signature}})
`)
if err != nil {
panic(err)
}
registerQueryGen("github.com/fishedee/tools/query.Where", QueryWhereGen)
hasQueryWhereGenerate = map[string]bool{}
}