/
gen.go
342 lines (268 loc) · 7.15 KB
/
gen.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package gen
import (
"bytes"
_ "embed"
"fmt"
"go/format"
"os"
"sort"
"strings"
"text/template"
"github.com/iancoleman/strcase"
)
var outputDir string
//go:embed templates/api_op.go.tpl
var apiFileTemplate string
var tpl *template.Template
func init() {
var err error
funcMap := template.FuncMap{
"toCamel": strcase.ToCamel,
"cleanLF": func(str string) string {
return strings.ReplaceAll(str, "\n", " ")
},
}
tpl, err = template.New("api_op").Funcs(funcMap).Parse(apiFileTemplate)
if err != nil {
panic(err)
}
}
func GenApis(output string, apiSchemas []ApiSchema) error {
outputDir = output
return genApis(apiSchemas)
}
func genApis(apiSchemas []ApiSchema) error {
for _, apiSchema := range apiSchemas {
err := genApi(apiSchema)
if err != nil {
return err
}
}
return nil
}
func genApi(apiSchema ApiSchema) error {
for method, api := range apiSchema.Info {
switch method {
case ApiMethodGet:
case ApiMethodPost:
case ApiMethodPut:
case ApiMethodDelete:
default:
return fmt.Errorf("unknown method: %s", method)
}
err := genApiFile(apiSchema.Path, api)
if err != nil {
return err
}
}
err := genApis(apiSchema.Children)
if err != nil {
return err
}
return nil
}
func genApiFile(path string, api ApiInfo) error {
apiDefine := NewApiDefine(path, api)
var buf bytes.Buffer
err := tpl.Execute(&buf, apiDefine)
if err != nil {
return err
}
apiFileContent := buf.Bytes()
// format code
apiFileContent, err = format.Source(apiFileContent)
if err != nil {
return err
}
os.WriteFile(fmt.Sprintf("%s/api_op_%s.go", outputDir, apiDefine.Name), apiFileContent, 0644)
return nil
}
type ApiDefine struct {
Name string
Description string
APILink string
Method ApiMethod
Path string
RequestPayload []ApiPayload
ResponsePayload []ApiPayload
}
func NewApiDefine(path string, api ApiInfo) *ApiDefine {
name := genApiName(path, api.Name)
description := api.Description
apiLink := fmt.Sprintf("https://pve.proxmox.com/pve-docs/api-viewer/#%s", path)
method := api.Method
var requstTagName string
switch method {
case ApiMethodGet:
requstTagName = "query"
case ApiMethodPost, ApiMethodPut, ApiMethodDelete:
requstTagName = "json"
}
var requestPayload, responsePayload []ApiPayload
// for debug api
// if path == "/nodes/{node}/apt/repositories" && method == ApiMethodGet {
// fmt.Println("x")
// }
requestPayload = genApiPayload(name+"Request", requstTagName, api.Parameters)
responsePayload = genApiPayload(name+"Response", "json", api.Returns)
apiDefine := ApiDefine{
Name: name,
Description: description,
APILink: apiLink,
Method: method,
Path: path,
RequestPayload: requestPayload,
ResponsePayload: responsePayload,
}
return &apiDefine
}
func genApiName(path, name string) string {
var result string
for _, p := range strings.Split(path, "/") {
if len(p) > 0 && p[0] != '{' {
result += strcase.ToCamel(p)
}
}
result += strcase.ToCamel(name)
return result
}
type ApiPayload struct {
Name string
Comment string
Type string
Fields []ApiPayloadField
}
type ApiPayloadField struct {
Name string
Type string
Tag string
Comment string
}
func genApiPayload(name string, tagName string, params *JsonSchema) []ApiPayload {
if params == nil {
return nil
}
result := []ApiPayload{}
payload := ApiPayload{
Name: name,
Comment: params.Description,
}
// default type is object
if params.Type == "" {
params.Type = JsonSchemaTypeObject
}
switch params.Type {
case JsonSchemaTypeArrary:
if params.Items == nil {
payload.Type = "[]interface{}"
} else {
switch params.Items.Type {
case JsonSchemaTypeArrary:
payload.Type = "[]" + name + "Item"
subPayloads := genApiPayload(name+"Item"+"Item", tagName, params.Items)
result = append(result, subPayloads...)
case JsonSchemaTypeObject:
payload.Type = "[]" + name + "Item"
subPayloads := genApiPayload(name+"Item", tagName, params.Items)
result = append(result, subPayloads...)
case JsonSchemaTypeBoolean:
payload.Type = "[]bool"
case JsonSchemaTypeNumber:
payload.Type = "[]float64"
case JsonSchemaTypeInteger:
payload.Type = "[]int64"
case JsonSchemaTypeString:
payload.Type = "[]string"
case JsonSchemaTypeAny:
payload.Type = "[]interface{}"
case JsonSchemaTypeNull:
payload.Type = "[]struct{}"
}
}
case JsonSchemaTypeObject:
if params.Properties != nil {
payload.Type = "struct"
fields, subPayloads := genFields(name, tagName, params.Properties)
payload.Fields = append(payload.Fields, fields...)
result = append(result, subPayloads...)
}
if params.AdditionalProperties.Schema != nil &&
params.AdditionalProperties.Schema.Type == JsonSchemaTypeObject {
payload.Type = "struct"
fields, subPayloads := genFields(name, tagName, params.AdditionalProperties.Schema.Properties)
payload.Fields = append(payload.Fields, fields...)
result = append(result, subPayloads...)
}
if params.Properties == nil && params.AdditionalProperties.Schema == nil {
payload.Type = "interface{}"
}
case JsonSchemaTypeBoolean:
payload.Type = "bool"
case JsonSchemaTypeNumber:
payload.Type = "float64"
case JsonSchemaTypeInteger:
payload.Type = "int64"
case JsonSchemaTypeString:
payload.Type = "string"
case JsonSchemaTypeAny:
payload.Type = "interface{}"
case JsonSchemaTypeNull:
payload.Type = "struct{}"
}
// sort payload fields
sort.Slice(payload.Fields, func(i, j int) bool {
return strings.Compare(payload.Fields[i].Name, payload.Fields[j].Name) == -1
})
result = append(result, payload)
// sort payload list
sort.Slice(result, func(i, j int) bool {
return strings.Compare(result[i].Name, result[j].Name) == -1
})
return result
}
func genFields(name, tagName string, properties map[string]JsonSchema) ([]ApiPayloadField, []ApiPayload) {
fields := []ApiPayloadField{}
subPayloads := []ApiPayload{}
for fieldName, param := range properties {
field := ApiPayloadField{
Name: fieldName,
Tag: tagName,
Comment: param.Description,
}
// default field type
if param.Type == "" {
param.Type = JsonSchemaTypeString
}
switch param.Type {
case JsonSchemaTypeArrary:
if param.Items == nil {
field.Type = "[]interface{}"
} else {
field.Type = "[]" + name + strcase.ToCamel(fieldName) + "Item"
subP := genApiPayload(name+strcase.ToCamel(fieldName)+"Item", tagName, param.Items)
subPayloads = append(subPayloads, subP...)
}
case JsonSchemaTypeObject:
field.Type = name + strcase.ToCamel(fieldName)
subP := genApiPayload(name+strcase.ToCamel(fieldName), tagName, ¶m)
subPayloads = append(subPayloads, subP...)
case JsonSchemaTypeBoolean:
field.Type = "bool"
case JsonSchemaTypeNumber:
field.Type = "float64"
case JsonSchemaTypeInteger:
field.Type = "int64"
case JsonSchemaTypeString:
field.Type = "string"
case JsonSchemaTypeAny:
field.Type = "interface{}"
case JsonSchemaTypeNull:
field.Type = "struct"
}
if param.Optional {
field.Type = "*" + field.Type
}
fields = append(fields, field)
}
return fields, subPayloads
}