-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathastfile.go
79 lines (64 loc) · 1.46 KB
/
astfile.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
package template
import (
"fmt"
"io"
"sort"
"strings"
)
type astFile struct {
header, macroses iAstNode
}
func (n *astFile) GetImports() []string {
res := []string{}
if n.macroses != nil {
res = append(res, n.macroses.GetImports()...)
}
return res
}
func (n *astFile) GetStrings() []string {
res := []string{}
if n.macroses != nil {
res = append(res, n.macroses.GetStrings()...)
}
return res
}
func (n *astFile) WriteGo(w io.Writer, opts *GenGoOpts) {
io.WriteString(w, "package "+opts.PackageName+"\n")
if n.header == nil {
n.header = &astHeader{}
}
opts.Imports = append(opts.Imports, n.GetImports()...)
n.header.WriteGo(w, opts)
stringsSet := make(map[string]struct{})
for _, s := range n.GetStrings() {
stringsSet[s] = struct{}{}
}
if len(stringsSet) > 0 {
stringsSlice := make([]string, 0, len(stringsSet))
for s := range stringsSet {
stringsSlice = append(stringsSlice, s)
}
sort.Strings(stringsSlice)
io.WriteString(w, "var (\n")
for _, s := range stringsSlice {
io.WriteString(w, strVarName(s, opts.FileName)+" = []byte{")
s := strings.Replace(s, `\"`, `"`, -1)
s = strings.Replace(s, `\\`, `\`, -1)
for i, b := range []byte(s) {
if i > 0 {
if i%50 == 0 {
io.WriteString(w, ",\n")
} else {
io.WriteString(w, ", ")
}
}
fmt.Fprintf(w, "0x%X", b)
}
io.WriteString(w, "}\n")
}
io.WriteString(w, ")\n\n")
}
if n.macroses != nil {
n.macroses.WriteGo(w, opts)
}
}