-
Notifications
You must be signed in to change notification settings - Fork 1
/
querycombine.go
105 lines (94 loc) · 3.53 KB
/
querycombine.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
package main
import (
"html/template"
"github.com/fishedee/tools/exception"
)
// QueryCombineGen QueryCombineGen
func QueryCombineGen(request QueryGenRequest) *QueryGenResponse {
args := request.args
line := request.pkg.FileSet().Position(request.expr.Pos()).String()
//解析第一个参数
firstArgSlice := getSliceType(line, args[0].Type)
firstArgElem := firstArgSlice.Elem()
//解析第二个参数
secondArgSlice := getSliceType(line, args[1].Type)
secondArgElem := secondArgSlice.Elem()
//解析第三个参数
thirdArgFunc := getFunctionType(line, args[2].Type)
thirdArgFuncArgument := getArgumentType(line, thirdArgFunc)
thirdArgFuncReturn := getReturnType(line, thirdArgFunc)
if len(thirdArgFuncArgument) != 2 {
exception.Throw(1, "%v:should be two argument", line)
}
if len(thirdArgFuncReturn) != 1 {
exception.Throw(1, "%v:should be one return", line)
}
if thirdArgFuncArgument[0].String() != firstArgElem.String() {
exception.Throw(1, "%v:groupFunctor first argument should be equal with first argument %v!=%v", line, thirdArgFuncArgument[0], firstArgElem)
}
if thirdArgFuncArgument[1].String() != secondArgElem.String() {
exception.Throw(1, "%v:groupFunctor second argument should be equal with second argument %v!=%v", line, thirdArgFuncArgument[1], secondArgElem)
}
//生成函数
signature := getFunctionSignature(line, args, []bool{false, false, false})
if hasQueryCombineGenerate[signature] == true {
return nil
}
hasQueryCombineGenerate[signature] = true
importPackage := map[string]bool{}
setImportPackage(line, firstArgElem, importPackage)
setImportPackage(line, secondArgElem, importPackage)
setImportPackage(line, thirdArgFuncReturn[0], importPackage)
argumentDefine := getFunctionArgumentCode(line, args, []bool{false, false, false})
funcBody := excuteTemplate(queryCombineFuncTmpl, map[string]string{
"signature": signature,
"firstArgElemType": getTypeDeclareCode(line, firstArgElem),
"secondArgElemType": getTypeDeclareCode(line, secondArgElem),
"thirdArgType": getTypeDeclareCode(line, thirdArgFunc),
"thirdArgReturnType": getTypeDeclareCode(line, thirdArgFuncReturn[0]),
})
initBody := excuteTemplate(queryCombineInitTmpl, map[string]string{
"signature": signature,
"argumentDefine": argumentDefine,
})
return &QueryGenResponse{
importPackage: importPackage,
funcName: combineFuncPrefix + signature,
funcBody: funcBody,
initBody: initBody,
}
}
const (
combineFuncPrefix = "queryCombineV"
)
var (
queryCombineFuncTmpl *template.Template
queryCombineInitTmpl *template.Template
hasQueryCombineGenerate map[string]bool
)
func init() {
var err error
queryCombineFuncTmpl, err = template.New("name").Parse(`
func ` + combineFuncPrefix + `{{ .signature }}(leftData interface{},rightData interface{},combineFunctor interface{})interface{}{
leftDataIn := leftData.([]{{ .firstArgElemType }})
rightDataIn := rightData.([]{{ .secondArgElemType }})
combineFunctorIn := combineFunctor.({{ .thirdArgType }})
newData := make([]{{ .thirdArgReturnType }},len(leftDataIn),len(leftDataIn))
for i := 0 ;i != len(leftDataIn);i++{
newData[i] = combineFunctorIn(leftDataIn[i],rightDataIn[i])
}
return newData
}
`)
if err != nil {
panic(err)
}
queryCombineInitTmpl, err = template.New("name").Parse(`
query.CombineMacroRegister({{.argumentDefine}},` + combineFuncPrefix + `{{.signature}})
`)
if err != nil {
panic(err)
}
registerQueryGen("github.com/fishedee/tools/query.Combine", QueryCombineGen)
hasQueryCombineGenerate = map[string]bool{}
}