-
Notifications
You must be signed in to change notification settings - Fork 2
/
templates.go
245 lines (218 loc) · 5.67 KB
/
templates.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
239
240
241
242
243
244
245
package internal
import (
"bytes"
_ "embed"
"go/format"
"os"
"path/filepath"
"regexp"
"slices"
"sort"
"strings"
"text/template"
)
func formatWriteFile(buf []byte, outputPath string) error {
// https://github.com/golang/go/issues/62559
buf = bytes.ReplaceAll(buf, []byte("\r\n"), []byte("\n"))
content, err := format.Source(buf)
if err != nil {
return err
}
outFile, err := os.Create(outputPath)
if err != nil {
return err
}
defer outFile.Close()
_, err = outFile.Write(content)
if err != nil {
return err
}
return nil
}
//go:embed templates/method.tmpl
var methodTempl string
func genMethod(ep *endpoint, outputPath string) error {
templ, err := template.New("method").Funcs(template.FuncMap{
"camelLowerToCamel": camelLowerToCamel,
"requestMethod": func(availHttpMethods []string) string {
if slices.Contains(availHttpMethods, "GET") {
return "getRequest"
}
return "postRequest"
},
"metaReturnType": func(ep *endpoint) string {
return "*GenericMeta"
},
"dataReturnType": func(ep *endpoint) string {
return toPointerType(ep.DataType.ToPkgAccessName(ep.Game))
},
"realmList": func(realmIdcs []string) string {
realms := make([]string, 0, len(realmIdcs))
for _, realmIdx := range realmIdcs {
realms = append(realms, "Realm"+strings.Title(realmIdx))
}
return strings.Join(realms, ", ")
},
"replaceAll": strings.ReplaceAll,
"summaryDoc": func(doc string) string {
return commentDocumentation(doc)[3:]
},
"paramDoc": func(param *goType) string {
res := param.Name + ":\n"
res += " " + strings.ReplaceAll(param.Description, "\n", "\n ")
res = strings.TrimSuffix(res, " ")
return commentDocumentation(res)
},
"optionsType": func(ep *endpoint) string {
return toPointerType(ep.OptionsType.ToPkgAccessName(ep.Game))
},
"valueToStringConv": valueToStringConv,
"valueToStringConvOptions": func(typ string, value string) string {
return valueToStringConv(typ, "options."+value)
},
}).Parse(methodTempl)
if err != nil {
return err
}
buf := &bytes.Buffer{}
err = templ.Execute(buf, ep)
if err != nil {
return err
}
return formatWriteFile(buf.Bytes(), filepath.Join(outputPath, ep.Id+".go"))
}
//go:embed templates/structBase.tmpl
var structBaseTempl string
//go:embed templates/struct.tmpl
var structTempl string
func genStructs(ep *endpoint, outputPath string) error {
templ, err := template.New("struct").Funcs(template.FuncMap{
"imports": func(ep *endpoint) []string {
tmp := map[string]struct{}{}
if ep.OptionsType != nil {
for imp := range ep.OptionsType.AllImports() {
tmp[imp] = struct{}{}
}
}
if ep.MetaType != nil {
for imp := range ep.MetaType.AllImports() {
tmp[imp] = struct{}{}
}
}
if ep.DataType != nil {
for imp := range ep.DataType.AllImports() {
tmp[imp] = struct{}{}
}
}
imports := make([]string, 0, len(tmp))
for imp := range tmp {
imports = append(imports, imp)
}
sort.Strings(imports)
return imports
},
"commentIndentDoc": func(doc string, indent int) string {
return strings.ReplaceAll(commentDocumentation(doc), "//", "\t//")
},
"indentation": func(depth int) string {
return strings.Repeat("\t", depth)
},
"summaryDoc": func(doc string) string {
return commentDocumentation(doc)[3:]
},
"inc": func(val int) int {
return val + 1
},
// used to track the nested indentation
"combine": func(t *goType, depth int) struct {
T *goType
Depth int
} {
return struct {
T *goType
Depth int
}{t, depth}
},
}).Parse(structTempl)
if err != nil {
return err
}
templ, err = templ.New("structBase").Parse(structBaseTempl)
if err != nil {
return err
}
buf := &bytes.Buffer{}
err = templ.ExecuteTemplate(buf, "structBase", ep)
if err != nil {
return err
}
filename := strings.TrimPrefix(ep.Id, ep.Game+"_")
return formatWriteFile(buf.Bytes(), filepath.Join(outputPath, ep.Game, filename+".go"))
}
//go:embed templates/services.tmpl
var servicesTempl string
type serviceData struct {
Name string
LongName string
}
func genServices(services []serviceData, outputPath string) error {
templ, err := template.New("services").Funcs(template.FuncMap{
"snakeToCamel": snakeToCamel,
}).Parse(servicesTempl)
if err != nil {
return err
}
buf := &bytes.Buffer{}
err = templ.Execute(buf, services)
if err != nil {
return err
}
return formatWriteFile(buf.Bytes(), filepath.Join(outputPath, "services.go"))
}
//go:embed templates/sections.tmpl
var sectionsTempl string
type sectionData struct {
Name string
LongName string
ApiUrlFormat string
}
var rxApiUrl = regexp.MustCompile(`(.*\.)([a-z]+)(.*)`)
// sectionData key is name
func genSections(sections []*sectionData, outputPath string) error {
templ, err := template.New("sections").Funcs(template.FuncMap{
"formatApiUrl": func(url string) string {
return strings.TrimSuffix(rxApiUrl.ReplaceAllString(url, "$1%s$3"), "/")
},
}).Parse(sectionsTempl)
if err != nil {
return err
}
buf := &bytes.Buffer{}
err = templ.Execute(buf, sections)
if err != nil {
return err
}
return formatWriteFile(buf.Bytes(), filepath.Join(outputPath, "sections.go"))
}
//go:embed templates/realms.tmpl
var realmsTempl string
type realmData struct {
LongName string
Tld string
Index string
}
// sectionData key is name
func genRealms(realms []*realmData, outputPath string) error {
templ, err := template.New("realms").Funcs(template.FuncMap{
"title": strings.Title,
}).Parse(realmsTempl)
if err != nil {
return err
}
buf := &bytes.Buffer{}
err = templ.Execute(buf, realms)
if err != nil {
return err
}
return formatWriteFile(buf.Bytes(), filepath.Join(outputPath, "realms.go"))
}