-
Notifications
You must be signed in to change notification settings - Fork 1
/
querysort.go
90 lines (81 loc) · 2.73 KB
/
querysort.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
package main
import (
"go/types"
"html/template"
)
// QuerySortGen QuerySortGen
func QuerySortGen(request QueryGenRequest) *QueryGenResponse {
args := request.args
line := request.pkg.FileSet().Position(request.expr.Pos()).String()
//解析第一个参数
firstArgSlice := getSliceType(line, args[0].Type)
firstArgSliceElem := firstArgSlice.Elem()
//解析第二个参数
secondArgSortType := getContantStringValue(line, args[1].Value)
sortFieldNames, sortFieldIsAscs := analyseSortType(secondArgSortType)
sortFieldTypes := make([]types.Type, len(sortFieldNames), len(sortFieldNames))
sortFieldExtracts := make([]string, len(sortFieldNames), len(sortFieldNames))
for i, sortFieldName := range sortFieldNames {
sortFieldExtracts[i], sortFieldTypes[i] = getExtendFieldType(line, firstArgSliceElem, sortFieldName)
}
//生成函数
signature := getFunctionSignature(line, args, []bool{false, true})
if hasQuerySortGenerate[signature] == true {
return nil
}
hasQuerySortGenerate[signature] = true
importPackage := map[string]bool{}
setImportPackage(line, firstArgSliceElem, importPackage)
argumentDefine := getFunctionArgumentCode(line, args, []bool{false, true})
funcBody := excuteTemplate(querySortFuncTmpl, map[string]string{
"signature": signature,
"firstArgElemType": getTypeDeclareCode(line, firstArgSliceElem),
"sortCode": getCombineLessCompareCode(line, "newData[i]", "newData[j]", sortFieldExtracts, sortFieldIsAscs, sortFieldTypes),
})
initBody := excuteTemplate(querySortInitTmpl, map[string]string{
"signature": signature,
"argumentDefine": argumentDefine,
})
return &QueryGenResponse{
importPackage: importPackage,
funcName: sortFuncPrefix + signature,
funcBody: funcBody,
initBody: initBody,
}
}
const (
sortFuncPrefix = "querySortV"
)
var (
querySortFuncTmpl *template.Template
querySortInitTmpl *template.Template
hasQuerySortGenerate map[string]bool
)
func init() {
var err error
querySortFuncTmpl, err = template.New("name").Parse(`
func ` + sortFuncPrefix + `{{ .signature }}(data interface{},sortType string)interface{}{
dataIn := data.([]{{ .firstArgElemType }})
newData := make([]{{ .firstArgElemType }},len(dataIn),len(dataIn))
copy(newData,dataIn)
query.SortInternal(len(newData),func(i int, j int)int{
{{ .sortCode }}
return 0
},func(i int,j int){
newData[j] , newData[i] = newData[i],newData[j]
})
return newData
}
`)
if err != nil {
panic(err)
}
querySortInitTmpl, err = template.New("name").Parse(`
query.SortMacroRegister({{.argumentDefine}},` + sortFuncPrefix + `{{.signature}})
`)
if err != nil {
panic(err)
}
registerQueryGen("github.com/fishedee/tools/query.Sort", QuerySortGen)
hasQuerySortGenerate = map[string]bool{}
}