-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathfastbin.go
63 lines (53 loc) · 1.06 KB
/
fastbin.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
package fastbin
import (
"log"
"os"
"path/filepath"
"reflect"
)
var types []reflect.Type
func Register(v interface{}) {
RegisterType(reflect.TypeOf(v))
}
func RegisterType(t reflect.Type) {
for i := 0; i < len(types); i++ {
if t == types[i] {
return
}
}
types = append(types, t)
}
func Types() []reflect.Type {
return types
}
func GenCode() {
a := newAnalyzer()
a.Analyze(types)
gopath := os.Getenv("GOPATH")
if gopath == "" {
panic("GOPATH environment variable missing")
}
path, err := filepath.Abs(gopath)
if err != nil {
panic(err)
}
path = filepath.Join(path, "src")
for _, pkg := range a.Packages {
saveCode(
filepath.Join(path, pkg.Path),
filepath.Base(pkg.Path)+".fastbin.go",
genPackage(pkg),
)
}
}
func saveCode(dir, filename string, code []byte) {
filename = filepath.Join(dir, filename)
file, err := os.Create(filename)
if err != nil {
log.Fatalf("Create file '%s' failed: %s", filename, err)
}
if _, err := file.Write(code); err != nil {
log.Fatalf("Write file '%s' failed: %s", filename, err)
}
file.Close()
}