prebuild.go
2.7 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// +build prebuild
package main
// prebuild.go generates sort implementations for
// various slice types and combination slice+reflect.Value types.
//
// The combination slice+reflect.Value types are used
// during canonical encode, and the others are used during fast-path
// encoding of map keys.
import (
"bytes"
"go/format"
"io/ioutil"
"os"
"strings"
"text/template"
)
// genInternalSortableTypes returns the types
// that are used for fast-path canonical's encoding of maps.
//
// For now, we only support the highest sizes for
// int64, uint64, float64, bool, string, bytes.
func genInternalSortableTypes() []string {
return []string{
"string",
// "float32",
"float64",
// "uint",
// "uint8",
// "uint16",
// "uint32",
"uint64",
"uintptr",
// "int",
// "int8",
// "int16",
// "int32",
"int64",
"bool",
"time",
"bytes",
}
}
// genInternalSortablePlusTypes returns the types
// that are used for reflection-based canonical's encoding of maps.
//
// For now, we only support the highest sizes for
// int64, uint64, float64, bool, string, bytes.
func genInternalSortablePlusTypes() []string {
return []string{
"string",
"float64",
"uint64",
"uintptr",
"int64",
"bool",
"time",
"bytes",
}
}
func genTypeForShortName(s string) string {
switch s {
case "time":
return "time.Time"
case "bytes":
return "[]byte"
}
return s
}
func genArgs(args ...interface{}) map[string]interface{} {
m := make(map[string]interface{}, len(args)/2)
for i := 0; i < len(args); {
m[args[i].(string)] = args[i+1]
i += 2
}
return m
}
func genEndsWith(s0 string, sn ...string) bool {
for _, s := range sn {
if strings.HasSuffix(s0, s) {
return true
}
}
return false
}
func chkerr(err error) {
if err != nil {
panic(err)
}
}
func run(fnameIn, fnameOut string) {
var err error
funcs := make(template.FuncMap)
funcs["sortables"] = genInternalSortableTypes
funcs["sortablesplus"] = genInternalSortablePlusTypes
funcs["tshort"] = genTypeForShortName
funcs["endswith"] = genEndsWith
funcs["args"] = genArgs
t := template.New("").Funcs(funcs)
fin, err := os.Open(fnameIn)
chkerr(err)
defer fin.Close()
fout, err := os.Create(fnameOut)
chkerr(err)
defer fout.Close()
tmplstr, err := ioutil.ReadAll(fin)
chkerr(err)
t, err = t.Parse(string(tmplstr))
chkerr(err)
var out bytes.Buffer
err = t.Execute(&out, 0)
chkerr(err)
bout, err := format.Source(out.Bytes())
if err != nil {
fout.Write(out.Bytes()) // write out if error, so we can still see.
}
chkerr(err)
// write out if error, as much as possible, so we can still see.
_, err = fout.Write(bout)
chkerr(err)
}
func main() {
run("sort-slice.go.tmpl", "sort-slice.generated.go")
}