-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
238 lines (206 loc) · 4.15 KB
/
main.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package main
import (
"bufio"
"flag"
"fmt"
"io"
"log"
"os"
"strconv"
"strings"
"text/template"
)
var (
fp = flag.String("fp", "", "")
)
// Config for generate enum struct
type Config struct {
Type string
Name string
Elements []*Pair
}
// Pair struct
type Pair struct {
N string
V string
}
func main() {
flag.Parse()
if *fp == "" {
log.Printf("usage: go run main.go -fp=[config file path]")
return
}
f, err := os.Open(*fp)
if err != nil {
log.Printf("fail to open file %v, error: %v", *fp, err)
return
}
defer f.Close()
cfg, err := parseFile(f)
if err != nil {
log.Printf("parse content fail, error: %v", err)
return
}
err = checkConfig(cfg)
if err != nil {
log.Printf("check config fail, error %v", err)
}
tmpl, err := template.New("enum").Parse(templateText)
if err != nil {
log.Printf("create template fail, error: %v", err)
return
}
nf, err := os.Create("enum.go")
if err != nil {
log.Printf("create file fail, error: %v", err)
return
}
defer nf.Close()
err = tmpl.Execute(nf, cfg)
if err != nil {
log.Printf("execute fail, error: %v", err)
return
}
log.Printf("DONE")
return
}
type contentMode struct {
empty int
dataType int
name int
list int
}
var (
modeEnum = contentMode{
empty: 0,
dataType: 1,
name: 2,
list: 3,
}
)
func parseFile(f *os.File) (*Config, error) {
r := bufio.NewReader(f)
cfg := &Config{}
mode := modeEnum.empty
section := []string{}
for {
s, err := r.ReadString('\n')
if err == io.EOF {
break
}
if err != nil {
log.Printf("read string fail, error: %v", err)
return nil, err
}
nextMode := mode
switch {
case strings.HasPrefix(s, "type:"):
nextMode = modeEnum.dataType
case strings.HasPrefix(s, "name:"):
nextMode = modeEnum.name
case strings.HasPrefix(s, "list:"):
nextMode = modeEnum.list
default:
// Do nothing
}
if nextMode != mode {
if mode != modeEnum.empty {
err = parse(mode, section, cfg)
if err != nil {
log.Printf("parse fail, mode: %v, error: %v", mode, err)
return nil, err
}
}
section = []string{}
mode = nextMode
continue
}
section = append(section, s)
}
err := parse(mode, section, cfg)
if err != nil {
log.Printf("parse fail, mode: %v, error: %v", mode, err)
return nil, err
}
return cfg, nil
}
func parse(mode int, section []string, cfg *Config) error {
switch mode {
case modeEnum.dataType:
if len(section) != 1 {
return fmt.Errorf("wrong setting for data type")
}
v := trim(section[0])
cfg.Type = v
case modeEnum.name:
if len(section) != 1 {
return fmt.Errorf("wrong setting for data type")
}
v := trim(section[0])
cfg.Name = v
case modeEnum.list:
for _, line := range section {
l := trim(line)
ts := strings.SplitN(l, "=", 2)
p := &Pair{
N: trim(ts[0]),
}
if len(ts) == 2 {
p.V = trim(ts[1])
}
cfg.Elements = append(cfg.Elements, p)
}
default:
return fmt.Errorf("no this mode %v", mode)
}
return nil
}
func checkConfig(cfg *Config) error {
cfg.Name = strings.ToLower(cfg.Name)
cfg.Type = strings.ToLower(cfg.Type)
// only support string and int
if cfg.Type != "string" && cfg.Type != "int" {
err := fmt.Errorf("not support data type: %v", cfg.Type)
log.Print(err.Error())
return err
}
setElementsNameCapital(cfg)
setElementsValue(cfg)
return nil
}
func setElementsNameCapital(cfg *Config) {
for i := range cfg.Elements {
cfg.Elements[i].N = strings.Title(cfg.Elements[i].N)
}
}
func setElementsValue(cfg *Config) {
if cfg.Type == "string" {
return
}
inc := 0
for i := range cfg.Elements {
if cfg.Elements[i].V == "iota" {
cfg.Elements[i].V = ""
inc = 0
}
if cfg.Elements[i].V == "" {
cfg.Elements[i].V = strconv.Itoa(inc)
inc++
}
}
}
func trim(s string) string {
return strings.Trim(s, "\n\t ")
}
var templateText = `package {{.Name}}
// Alias hide the real type of the enum
// and users can use it to define the var for accepting enum
type Alias = {{.Type}}
type list struct { {{range $i, $v := (.Elements)}}
{{$v.N}} Alias{{end}}
}
// Enum for public use
var Enum = &list{ {{range $i, $v := (.Elements)}}
{{$v.N}}: {{$v.V}},{{end}}
}
`