This repository has been archived by the owner on Apr 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
109 lines (103 loc) · 2.05 KB
/
utils.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"fmt"
"go/format"
"os"
"path/filepath"
"strings"
)
var (
p = fmt.Printf
w = fmt.Fprintf
fs = fmt.Sprintf
)
func checkError(err error) {
if err != nil {
panic(err)
}
}
func isGoReservedWord(s string) bool {
return map[string]bool{
// key words
"break": true,
"default": true,
"func": true,
"interface": true,
"select": true,
"case": true,
"defer": true,
"go": true,
"map": true,
"struct": true,
"chan": true,
"else": true,
"goto": true,
"package": true,
"switch": true,
"const": true,
"fallthrough": true,
"if": true,
"range": true,
"type": true,
"continue": true,
"for": true,
"import": true,
"return": true,
"var": true,
// builtin functions
"append": true,
"cap": true,
"close": true,
"complex": true,
"copy": true,
"delete": true,
"imag": true,
"len": true,
"make": true,
"new": true,
"panic": true,
"print": true,
"println": true,
"real": true,
"recover": true,
// builtin types
"bool": true,
"byte": true,
"complex128": true,
"complex64": true,
"error": true,
"float32": true,
"float64": true,
"int": true,
"int16": true,
"int32": true,
"int64": true,
"int8": true,
"rune": true,
"string": true,
"uint": true,
"uint16": true,
"uint32": true,
"uint64": true,
"uint8": true,
"uintptr": true,
}[s]
}
func (self *Generator) formatAndOutput(name string, source []byte) {
f, err := os.Create(filepath.Join(self.outputDir, self.PackageName+"_"+name+".go"))
checkError(err)
formatted, err := format.Source(source)
if err != nil {
f.Write(source)
f.Close()
checkError(err)
}
f.Write(formatted)
f.Close()
}
func replace(t string, m map[string]string) string {
for key, value := range m {
t = strings.Replace(t, fs("$$%s", key), value, -1)
}
return t
}