-
Notifications
You must be signed in to change notification settings - Fork 1
/
generator.go
230 lines (202 loc) · 5.81 KB
/
generator.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
package main
import (
"bufio"
"container/list"
"fmt"
"io"
"os"
"path"
"regexp"
"strings"
)
const (
CSV_LIST_SEPARATOR = "|"
FILE_SUFFIX_NAME = "go"
ENUM_CLASS_NAME = "ErrorEnum"
)
type Generator struct {
inputFile string
outputDir string
}
func (generator Generator) Generate() {
dataMap, attributeList := obtainMapData(generator.inputFile)
generator.GenerateConstantFile(dataMap, attributeList)
generator.GenerateEnumFile(dataMap, attributeList)
}
func (generator Generator) GenerateConstantFile(dataMap map[string]map[string]string, attributeList []string) {
outputDir := generator.outputDir
for _, attribute := range attributeList {
className := toBigHump(attribute)
filePath := obtainOutputFilePath(outputDir, className)
content := obtainConstantTemplate(attribute, dataMap)
generateFile(filePath, content)
}
}
func (generator Generator) GenerateEnumFile(dataMap map[string]map[string]string, attributeList []string) {
name := ENUM_CLASS_NAME
filePath := obtainOutputFilePath(generator.outputDir, name)
content := obtainEnumTemplate(attributeList, dataMap)
generateFile(filePath, content)
}
func obtainMapData(inputFile string) (map[string]map[string]string, []string) {
dataList := list.New()
rowList := parseFile(inputFile)
for _, row := range rowList {
dataList.PushBack(splitRowData(row))
}
attributeList := dataList.Front().Value.([]string)
attributeSize := len(attributeList)
dataMap := make(map[string]map[string]string)
for data := dataList.Front(); data != nil; data = data.Next() {
rowDataList := data.Value.([]string)
rowDataList = fillList(rowDataList, attributeSize)
name := obtainLabelName(rowDataList[0])
itemMap := make(map[string]string)
dataMap[name] = itemMap
for index := 0; index < attributeSize; index++ {
attribute := attributeList[index]
itemMap[attribute] = rowDataList[index]
}
}
return dataMap, attributeList
}
func splitRowData(row string) []string {
var dataList []string
rowArr := strings.Split(string(row), CSV_LIST_SEPARATOR)
for _, data := range rowArr {
dataList = append(dataList, data)
}
return dataList
}
func obtainLabelName(name string) string {
name = strings.Trim(name, " ")
name = strings.ToUpper(name)
regex, _ := regexp.Compile("\\s+")
return regex.ReplaceAllString(name, "_")
}
func fillList(list []string, size int) []string {
if len(list) < size {
for index := 0; index < size-len(list); index++ {
list = append(list, "")
}
}
return list
}
func obtainPropertyName(property string) string {
return toSmallHump(property)
}
func toBigHump(str string) string {
return toHump(str, false)
}
func toSmallHump(str string) string {
return toHump(str, true)
}
func toHump(str string, isSmallHump bool) string {
var builder strings.Builder
strArr := strings.Split(str, "[ _-]")
for index := 0; index < len(strArr); index++ {
subStr := strArr[index]
if index != 0 || !isSmallHump {
subStr = capitalize(subStr)
}
builder.WriteString(subStr)
}
return builder.String()
}
func capitalize(str string) string {
firstStr := string(str[0:1])
firstStr = strings.ToUpper(firstStr)
return firstStr + string(str[1:])
}
func obtainEnumTemplate(attributeList []string, dataMap map[string]map[string]string) string {
propertyList := obtainEnumPropertyList(attributeList)
return fmt.Sprintf("%s\n%s", obtainEnumDataStruct(propertyList), obtainEnumDataList(propertyList, dataMap))
}
func obtainEnumDataStruct(propertyList []string) string {
type Data struct {
name string
}
var builder strings.Builder
builder.WriteString("type Data struct {\n")
for _, property := range propertyList {
builder.WriteString(fmt.Sprintf("\t%s string\n", property))
}
builder.WriteString("}\r\n")
return builder.String()
}
func obtainEnumDataList(propertyList []string, dataMap map[string]map[string]string) string {
var nameList []string
for key := range dataMap {
nameList = append(nameList, key)
}
var builder strings.Builder
builder.WriteString("var (\n")
for _, name := range nameList {
var propertyArr []string
itemMap := dataMap[name]
for _, property := range propertyList {
propertyArr = append(propertyArr, fmt.Sprintf("\"%s\"", itemMap[property]))
}
propertyArrString := strings.Join(propertyArr, ", ")
builder.WriteString(fmt.Sprintf("\t%s = Data{%s}\n", name, propertyArrString))
}
builder.WriteString(")\r\n")
return builder.String()
}
func obtainEnumPropertyList(attributeList []string) []string {
return attributeList
}
func obtainConstantTemplate(attribute string, dataMap map[string]map[string]string) string {
var builder strings.Builder
builder.WriteString("const (\n")
for itemName, itemMap := range dataMap {
format := fmt.Sprintf("\t%s = \"%s\"\n", itemName, itemMap[attribute])
builder.WriteString(format)
}
builder.WriteString(")\r\n")
return builder.String()
}
func obtainOutputFilePath(outputDir string, name string) string {
preGenerateDir(outputDir)
return fmt.Sprintf("%s%c%s.%s", outputDir, os.PathSeparator, name, FILE_SUFFIX_NAME)
}
func preGenerateDir(directory string) {
_, err := os.Stat(directory)
if err != nil {
os.Mkdir(directory, 0777)
}
}
func generateFile(outputPath string, content string) {
file, err := os.Create(outputPath)
if err != nil {
fmt.Println(err)
} else {
_, err = file.WriteString(content)
}
defer file.Close()
}
func parseFile(inputFile string) []string {
var dataList []string
fi, err := os.Open(inputFile)
if err != nil {
fmt.Printf("Error: %s\n", err)
return dataList
}
defer fi.Close()
br := bufio.NewReader(fi)
for {
line, _, err := br.ReadLine()
if err == io.EOF {
break
}
dataList = append(dataList, string(line))
}
return dataList
}
func main() {
currentDir, _ := os.Getwd()
inputFile := path.Join(currentDir, "error-code.csv")
outputDir := path.Join(currentDir, "output", "go")
generator := Generator{inputFile, outputDir}
generator.Generate()
}