-
Notifications
You must be signed in to change notification settings - Fork 1
/
queryselect.go
97 lines (87 loc) · 3 KB
/
queryselect.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
package main
import (
"html/template"
"github.com/fishedee/tools/exception"
)
// QuerySelectGen QuerySelectGen
func QuerySelectGen(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")
}
//生成函数
signature := getFunctionSignature(line, args, []bool{false, false})
if hasQuerySelectGenerate[signature] == true {
return nil
}
hasQuerySelectGenerate[signature] = true
importPackage := map[string]bool{}
setImportPackage(line, secondArgFuncArgument, importPackage)
setImportPackage(line, secondArgFuncResult, importPackage)
argumentDefine := getFunctionArgumentCode(line, args, []bool{false, false})
funcBody := excuteTemplate(querySelectFuncTmpl, map[string]string{
"signature": signature,
"firstArgElemType": getTypeDeclareCode(line, firstArgElem),
"secondArgType": getTypeDeclareCode(line, secondArgFunc),
"secondArgReturnType": getTypeDeclareCode(line, secondArgFuncResult),
})
initBody := excuteTemplate(querySelectInitTmpl, map[string]string{
"signature": signature,
"argumentDefine": argumentDefine,
})
return &QueryGenResponse{
importPackage: importPackage,
funcName: selectFuncPrefix + signature,
funcBody: funcBody,
initBody: initBody,
}
}
const (
selectFuncPrefix = "querySelectV"
)
var (
querySelectFuncTmpl *template.Template
querySelectInitTmpl *template.Template
hasQuerySelectGenerate map[string]bool
)
func init() {
var err error
querySelectFuncTmpl, err = template.New("name").Parse(`
func ` + selectFuncPrefix + `{{ .signature }}(data interface{},selectFunctor interface{})interface{}{
dataIn := data.([]{{ .firstArgElemType }})
selectFunctorIn := selectFunctor.({{ .secondArgType }})
result := make([]{{ .secondArgReturnType }},len(dataIn),len(dataIn))
for i,single := range dataIn{
result[i] = selectFunctorIn(single)
}
return result
}
`)
if err != nil {
panic(err)
}
querySelectInitTmpl, err = template.New("name").Parse(`
query.SelectMacroRegister({{.argumentDefine}},` + selectFuncPrefix + `{{.signature}})
`)
if err != nil {
panic(err)
}
registerQueryGen("github.com/fishedee/tools/query.Select", QuerySelectGen)
hasQuerySelectGenerate = map[string]bool{}
}