-
Notifications
You must be signed in to change notification settings - Fork 0
/
types_gen.go
74 lines (66 loc) · 1.68 KB
/
types_gen.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
// +build ignore
package main
import (
"bytes"
"go/format"
"io/ioutil"
"os"
"strings"
)
var lengthFunction = `func (s Int64Set) Len() int {
return len(s)
}`
func main() {
f, err := os.Open("hashset.go")
if err != nil {
panic(err)
}
filedata, err := ioutil.ReadAll(f)
if err != nil {
panic(err)
}
w := new(bytes.Buffer)
w.WriteString(`// Code generated by go run types_gen.go; DO NOT EDIT.` + "\r\n")
w.WriteString(string(filedata)[strings.Index(string(filedata), "package hashset") : strings.Index(string(filedata), "\n")+1])
ts := []string{"Float32", "Float64", "Int32", "Int16", "Int", "Uint64", "Uint32", "Uint16", "Uint"} // all types need to be converted
for _, upper := range ts {
lower := strings.ToLower(upper)
data := string(filedata)
// Remove header.
data = data[strings.Index(data, "\n")+1:]
// Remove the special case.
data = strings.Replace(data, lengthFunction, "", -1)
// Common cases.
data = strings.Replace(data, "int64", lower, -1)
data = strings.Replace(data, "Int64", upper, -1)
if inSlice(lowerSlice(ts), lower) {
data = strings.Replace(data, "length "+lower, "length int64", 1)
}
// Add the special case.
data = data + strings.Replace(lengthFunction, "Int64Set", upper+"Set", 1)
w.WriteString(data)
w.WriteString("\r\n")
}
out, err := format.Source(w.Bytes())
if err != nil {
panic(err)
}
if err := ioutil.WriteFile("types.go", out, 0660); err != nil {
panic(err)
}
}
func lowerSlice(s []string) []string {
n := make([]string, len(s))
for i, v := range s {
n[i] = strings.ToLower(v)
}
return n
}
func inSlice(s []string, val string) bool {
for _, v := range s {
if v == val {
return true
}
}
return false
}