forked from cocotyty/summer
/
sortplugin.go
57 lines (51 loc) · 1.06 KB
/
sortplugin.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
package gen
import (
"go/ast"
"bytes"
"text/template"
"strings"
)
type SortPlugin struct {
tpl *template.Template
}
func NewSortPlugin() *SortPlugin {
return &SortPlugin{template.Must(template.New("name").Parse(sortTpl))}
}
func (SortPlugin *SortPlugin)Tag() string {
return "sort"
}
func (SortPlugin *SortPlugin)Handle(body *bytes.Buffer, imports *bytes.Buffer, spec *ast.TypeSpec, tag string, path string, pkg string) {
if _, ok := spec.Type.(*ast.ArrayType); ok {
sorts := []string{}
if tag != "*" {
tags := strings.Split(tag, ",")
for _, v := range tags {
sorts = append(sorts, strings.TrimSpace(v))
}
}
SortPlugin.tpl.Execute(body, map[string]interface{}{
"name":spec.Name,
"sorts":sorts,
})
}
}
var sortTpl = `
func (arr {{.name}})Len()int{
return len(arr)
}
func (arr {{.name}})Less(i,j int)bool{
{{range .sorts}}
if arr[i].{{.}}!=arr[j].{{.}}{
return arr[i].{{.}}!<arr[j].{{.}}
}
{{else}}
return arr[i]<arr[j]
{{end}}
{{with .sorts}}
return false
{{end}}
}
func (arr {{.name}})Swap(i, j int){
arr[i],arr[j]=arr[j],arr[i]
}
`