-
Notifications
You must be signed in to change notification settings - Fork 4
/
generate.go
157 lines (134 loc) · 3.65 KB
/
generate.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
// generate errors.go -- batteries *NOT* included
//
// usage:
// ./localization [-p <package>] | gofmt > /path/to/errors.go
//
package main
import (
"fmt"
"io/ioutil"
"reflect"
"regexp"
"sort"
"strings"
"unicode/utf8"
toml "github.com/pelletier/go-toml"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
var (
flagPackage = kingpin.Flag(
"package",
"specify the generated package name",
).Short('p').Default("main").String()
)
// TranslatedError describes an error with a comment attribute
type TranslatedError struct {
Comment string `toml:"comment"`
De string `toml:"de"`
EnUS string `toml:"en-us"`
EsES string `toml:"es-es"`
Fr string `toml:"fr"`
Ja string `toml:"ja"`
Ru string `toml:"ru"`
Zh string `toml:"zh"`
}
// Errors lists all errors defined in errors.toml
// XXX would be nice to delete this and use map[string]TranslatedError
type Errors struct {
ErrorAuth TranslatedError
ErrorBanned TranslatedError
ErrorCreate TranslatedError
ErrorCreateSSO TranslatedError
ErrorDatasource TranslatedError
ErrorESIJSON TranslatedError
ErrorHeaderParse TranslatedError
ErrorLimited TranslatedError
ErrorMethod TranslatedError
ErrorNoAuth TranslatedError
ErrorNotFound TranslatedError
ErrorProxy TranslatedError
ErrorRead TranslatedError
ErrorReadResponse TranslatedError
ErrorReadSSO TranslatedError
ErrorRequestSSO TranslatedError
ErrorSSOJSON TranslatedError
ErrorTimeout TranslatedError
ErrorTimeoutSSO TranslatedError
ErrorTokenID TranslatedError
ErrorTokenTime TranslatedError
ErrorWrite TranslatedError
}
func main() {
kingpin.CommandLine.HelpFlag.Short('h')
kingpin.Parse()
rawToml, err := ioutil.ReadFile("errors.toml")
if err != nil {
panic(fmt.Sprintf("failed to read errors.toml: %+v", err))
}
regex := regexp.MustCompile("# (?P<comment>.*)")
withComments := regex.ReplaceAll(rawToml, []byte("comment = \"${comment}\""))
e := Errors{}
if tomlErr := toml.Unmarshal(withComments, &e); tomlErr != nil {
panic(fmt.Sprintf("failed to unmarshal errors.toml: %+v", tomlErr))
}
buildErrorsGo(e)
}
func buildErrorsGo(e Errors) {
val := reflect.Indirect(reflect.ValueOf(e))
names := []string{}
indexes := map[string]int{}
for i := 0; i < val.NumField(); i++ {
name := val.Type().Field(i).Name
names = append(names, name)
indexes[name] = i
}
fmt.Printf(
"// ESI Translated Errors\n//\n"+
"// Note: this file is autogenerated, you shouldn't edit it\n\n"+
"package %s\n\n"+
"var (\n",
*flagPackage,
)
sort.Strings(names)
for j, name := range names {
translatedErr := val.Field(indexes[name]).Interface().(TranslatedError)
tval := reflect.Indirect(reflect.ValueOf(translatedErr))
if j > 0 {
fmt.Println()
}
fmt.Printf(
"\t// %s\n"+
"\t%s = TranslatedError{map[string]string{\n",
translatedErr.Comment,
name,
)
for i := 0; i < tval.NumField(); i++ {
lang := tval.Type().Field(i).Tag.Get("toml")
if lang == "comment" {
continue
}
value := tval.Field(i).Interface().(string)
if utf8.RuneCountInString(value) <= 60 {
fmt.Printf(
"\t\t%q: %q,\n",
lang,
value,
)
} else {
valSplit := strings.Split(value, " ")
buffer := fmt.Sprintf("\t\t%q: \"%s", lang, valSplit[0])
for _, word := range valSplit[1:] {
if utf8.RuneCountInString(buffer)+utf8.RuneCountInString(word) > 60 {
fmt.Printf("%s \" +\n", buffer)
buffer = fmt.Sprintf("\t\t\t\"%s", word)
} else {
buffer += fmt.Sprintf(" %s", word)
}
}
fmt.Printf("%s\",\n", buffer)
}
}
fmt.Println("\t}}")
}
fmt.Println(")")
}