/
utils.go
558 lines (497 loc) · 13.8 KB
/
utils.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
// Copyright 2019 DeepMap, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package codegen
import (
"fmt"
"net/url"
"regexp"
"sort"
"strconv"
"strings"
"unicode"
"github.com/getkin/kin-openapi/openapi3"
"github.com/kenshaw/snaker"
)
var pathParamRE *regexp.Regexp
func init() {
pathParamRE = regexp.MustCompile(`{[.;?]?([^{}*]+)\*?}`)
}
// UppercaseFirstCharacter uppercases the first character of str.
func UppercaseFirstCharacter(str string) string {
if str == "" {
return ""
}
runes := []rune(str)
runes[0] = unicode.ToUpper(runes[0])
return string(runes)
}
// LowercaseFirstCharacter lowercases te first character of str.
func LowercaseFirstCharacter(str string) string {
if str == "" {
return ""
}
runes := []rune(str)
runes[0] = unicode.ToLower(runes[0])
return string(runes)
}
// ToCamelCase converts a string to camel case with proper Go initialisms.
func ToCamelCase(str string) string {
if str != "" && unicode.IsDigit([]rune(str)[0]) {
// FIXME this is so hacky please help
str = "F" + str
str = snaker.ForceCamelIdentifier(str)
return str[1:]
}
return snaker.ForceCamelIdentifier(str)
}
// ToSnakeCase converts a string to snake case.
func ToSnakeCase(str string) string {
str = strings.Replace(str, "-", "_", -1)
return snaker.CamelToSnake(str)
}
// SortedSchemaKeys returns the keys of dict in alphabetically.
func SortedSchemaKeys(dict map[string]*openapi3.SchemaRef) []string {
keys := make([]string, len(dict))
i := 0
for key := range dict {
keys[i] = key
i++
}
sort.Strings(keys)
return keys
}
// SortedPathsKeys returns the keys of dict alphabetically.
func SortedPathsKeys(dict openapi3.Paths) []string {
keys := make([]string, len(dict))
i := 0
for key := range dict {
keys[i] = key
i++
}
sort.Strings(keys)
return keys
}
// SortedOperationsKeys returns the keys of dict alphabetically.
func SortedOperationsKeys(dict map[string]*openapi3.Operation) []string {
keys := make([]string, len(dict))
i := 0
for key := range dict {
keys[i] = key
i++
}
sort.Strings(keys)
return keys
}
// SortedResponsesKeys returns the keys of dict alphabetically.
func SortedResponsesKeys(dict openapi3.Responses) []string {
keys := make([]string, len(dict))
i := 0
for key := range dict {
keys[i] = key
i++
}
sort.Strings(keys)
return keys
}
// SortedContentKeys returns the keys of dict alphabetically.
func SortedContentKeys(dict openapi3.Content) []string {
keys := make([]string, len(dict))
i := 0
for key := range dict {
keys[i] = key
i++
}
sort.Strings(keys)
return keys
}
// SortedStringKeys returns the keys of dict alphabetically.
func SortedStringKeys(dict map[string]string) []string {
keys := make([]string, len(dict))
i := 0
for key := range dict {
keys[i] = key
i++
}
sort.Strings(keys)
return keys
}
// SortedParameterKeys returns the keys of dict alphabetically.
func SortedParameterKeys(dict map[string]*openapi3.ParameterRef) []string {
keys := make([]string, len(dict))
i := 0
for key := range dict {
keys[i] = key
i++
}
sort.Strings(keys)
return keys
}
// SortedRequestBodyKeys returns the keys of dict alphabetically.
func SortedRequestBodyKeys(dict map[string]*openapi3.RequestBodyRef) []string {
keys := make([]string, len(dict))
i := 0
for key := range dict {
keys[i] = key
i++
}
sort.Strings(keys)
return keys
}
// SortedSecurityRequirementKeys eturns the keys of dict alphabetically.
func SortedSecurityRequirementKeys(dict openapi3.SecurityRequirement) []string {
keys := make([]string, len(dict))
i := 0
for key := range dict {
keys[i] = key
i++
}
sort.Strings(keys)
return keys
}
// StringInArray returns if strs contains str.
func StringInArray(str string, strs []string) bool {
for _, s := range strs {
if s == str {
return true
}
}
return false
}
// RefPathToGoType converts refPath to a Go type name.
// #/components/schemas/Foo -> Foo
// #/components/parameters/Bar -> Bar
// #/components/responses/Baz -> Baz
// Remote components (document.json#/Foo) are supported if they present in --import-mapping
// URL components (http://deepmap.com/schemas/document.json#/Foo) are supported if they present in --import-mapping
// Remote and URL also support standard local paths even though the spec doesn't mention them.
func RefPathToGoType(refPath string) (string, error) {
return refPathToGoType(refPath, true)
}
// refPathToGoType returns the Go typename for refPath given its
func refPathToGoType(refPath string, local bool) (string, error) {
if refPath[0] == '#' {
pathParts := strings.Split(refPath, "/")
depth := len(pathParts)
if local {
if depth != 4 {
return "", fmt.Errorf("unexpected reference depth: %d for ref: %s local: %t", depth, refPath, local)
}
} else if depth != 4 && depth != 2 {
return "", fmt.Errorf("unexpected reference depth: %d for ref: %s local: %t", depth, refPath, local)
}
return SchemaNameToTypeName(pathParts[len(pathParts)-1]), nil
}
pathParts := strings.Split(refPath, "#")
if len(pathParts) != 2 {
return "", fmt.Errorf("unsupported reference: %s", refPath)
}
remoteComponent, flatComponent := pathParts[0], pathParts[1]
goImport, ok := importMapping[remoteComponent]
if !ok {
return "", fmt.Errorf("unrecognized external reference '%s'; please provide the known import for this reference using option --import-mapping", remoteComponent)
}
goType, err := refPathToGoType("#"+flatComponent, false)
if err != nil {
return "", err
}
return fmt.Sprintf("%s.%s", goImport.Name, goType), nil
}
// IsGoTypeReference checks if ref links to a valid go type.
// #/components/schemas/Foo -> true
// ./local/file.yml#/components/parameters/Bar -> true
// ./local/file.yml -> false
func IsGoTypeReference(ref string) bool {
return ref != "" && !IsWholeDocumentReference(ref)
}
// IsWholeDocumentReference checks if ref is a whole document reference.
// #/components/schemas/Foo -> false
// ./local/file.yml#/components/parameters/Bar -> false
// ./local/file.yml -> true
// http://example.com/schemas/document.json -> true
// http://example.com/schemas/document.json#/Foo -> false
func IsWholeDocumentReference(ref string) bool {
return ref != "" && !strings.ContainsAny(ref, "#")
}
// SwaggerURIToChiURI converts uri to a Chi-style URI.
// It replaces all swagger parameters with {param}.
//
// Valid input parameters are:
// {param}
// {param*}
// {.param}
// {.param*}
// {;param}
// {;param*}
// {?param}
// {?param*}
func SwaggerURIToChiURI(uri string) string {
return pathParamRE.ReplaceAllString(uri, "{$1}")
}
// OrderedParamsFromURI returns argument names in uri.
// Given /path/{param1}/{.param2*}/{?param3},
// returns [param1, param2, param3]
func OrderedParamsFromURI(uri string) []string {
matches := pathParamRE.FindAllStringSubmatch(uri, -1)
result := make([]string, len(matches))
for i, m := range matches {
result[i] = m[1]
}
return result
}
// ReplacePathParamsWithStr replaces uri parameters with %s
func ReplacePathParamsWithStr(uri string) string {
return pathParamRE.ReplaceAllString(uri, "%s")
}
// SortParamsByPath sorts in to match those in the path URI.
func SortParamsByPath(path string, in []ParameterDefinition) ([]ParameterDefinition, error) {
pathParams := OrderedParamsFromURI(path)
if len(pathParams) != len(in) {
return nil, fmt.Errorf("path '%s' has %d positional parameters, but spec has %d declared",
path, len(pathParams), len(in))
}
out := make([]ParameterDefinition, len(in))
for i, name := range pathParams {
p := ParameterDefinitions(in).FindByName(name)
if p == nil {
return nil, fmt.Errorf("path '%s' refers to parameter '%s', which doesn't exist in specification",
path, name)
}
out[i] = *p
}
return out, nil
}
// IsGoKeyword checks if str is a reserved keyword.
// Returns whether the given string is a go keyword
func IsGoKeyword(str string) bool {
keywords := []string{
"break",
"case",
"chan",
"const",
"continue",
"default",
"defer",
"else",
"fallthrough",
"for",
"func",
"go",
"goto",
"if",
"import",
"interface",
"map",
"package",
"range",
"return",
"select",
"struct",
"switch",
"type",
"var",
}
for _, k := range keywords {
if k == str {
return true
}
}
return false
}
// IsPredeclaredGoIdentifier returns whether str is a go indentifier.
func IsPredeclaredGoIdentifier(str string) bool {
predeclaredIdentifiers := []string{
// Types
"bool",
"byte",
"complex64",
"complex128",
"error",
"float32",
"float64",
"int",
"int8",
"int16",
"int32",
"int64",
"rune",
"string",
"uint",
"uint8",
"uint16",
"uint32",
"uint64",
"uintptr",
// Constants
"true",
"false",
"iota",
// Zero value
"nil",
// Functions
"append",
"cap",
"close",
"complex",
"copy",
"delete",
"imag",
"len",
"make",
"new",
"panic",
"print",
"println",
"real",
"recover",
}
for _, k := range predeclaredIdentifiers {
if k == str {
return true
}
}
return false
}
// IsGoIdentity checks if str is a valid go identifier.
func IsGoIdentity(str string) bool {
for i, c := range str {
if !isValidRuneForGoID(i, c) {
return false
}
}
return IsGoKeyword(str)
}
func isValidRuneForGoID(index int, char rune) bool {
if index == 0 && unicode.IsNumber(char) {
return false
}
return unicode.IsLetter(char) || char == '_' || unicode.IsNumber(char)
}
// IsValidGoIdentity checks if str can be used as a name of variable, constant,
// or type.
func IsValidGoIdentity(str string) bool {
return !IsPredeclaredGoIdentifier(str) && !IsGoIdentity(str)
}
// SanitizeGoIdentity replaces illegal characters in str.
func SanitizeGoIdentity(str string) string {
sanitized := []rune(str)
for i, c := range sanitized {
if !isValidRuneForGoID(i, c) {
sanitized[i] = '_'
} else {
sanitized[i] = c
}
}
str = string(sanitized)
if IsGoKeyword(str) || IsPredeclaredGoIdentifier(str) {
str = "_" + str
}
if !IsValidGoIdentity(str) {
panic("here is a bug")
}
return str
}
// SanitizeEnumNames removes illegal and duplicates chars in enum names.
func SanitizeEnumNames(enumNames []string) map[string]string {
dupCheck := make(map[string]int, len(enumNames))
deDup := make([]string, 0, len(enumNames))
for _, n := range enumNames {
if _, dup := dupCheck[n]; !dup {
deDup = append(deDup, n)
}
dupCheck[n] = 0
}
dupCheck = make(map[string]int, len(deDup))
sanitizedDeDup := make(map[string]string, len(deDup))
for _, n := range deDup {
sanitized := SanitizeGoIdentity(SchemaNameToTypeName(n))
if _, dup := dupCheck[sanitized]; !dup {
sanitizedDeDup[sanitized] = n
} else {
sanitizedDeDup[sanitized+strconv.Itoa(dupCheck[sanitized])] = n
}
dupCheck[sanitized]++
}
return sanitizedDeDup
}
// SchemaNameToTypeName converts name to a valid Go type name.
// It converts name to camel case and is valid in Go.
func SchemaNameToTypeName(name string) string {
if name == "$" {
name = "DollarSign"
} else {
name = ToCamelCase(name)
// Prepend "N" to schemas starting with a number
if name != "" && unicode.IsDigit([]rune(name)[0]) {
name = "N" + name
}
}
return name
}
// SchemaHasAdditionalProperties checks if schema has additional properties.
//
// According to the spec, additionalProperties may be true, false, or a
// schema. If not present, true is implied. If it's a schema, true is implied.
// If it's false, no additional properties are allowed. We're going to act a little
// differently, in that if you want additionalProperties code to be generated,
// you must specify an additionalProperties type
// If additionalProperties it true/false, this field will be non-nil.
func SchemaHasAdditionalProperties(schema *openapi3.Schema) bool {
if schema.AdditionalPropertiesAllowed != nil && *schema.AdditionalPropertiesAllowed {
return true
}
if schema.AdditionalProperties != nil {
return true
}
return false
}
// PathToTypeName converts path to a go type name.
// It converts each entry in path to camel case and joins them with _.
func PathToTypeName(path []string) string {
for i, p := range path {
path[i] = ToCamelCase(p)
}
return strings.Join(path, "_")
}
// StringToGoComment renders a possible multi-line string to a valid Go-Comment.
// Each line is prefixed as a comment.
func StringToGoComment(in string) string {
if len(in) == 0 || len(strings.TrimSpace(in)) == 0 { // ignore empty comment
return ""
}
// Normalize newlines from Windows/Mac to Linux
in = strings.ReplaceAll(in, "\r\n", "\n")
in = strings.ReplaceAll(in, "\r", "\n")
// Add comment to each line
var lines []string
for _, line := range strings.Split(in, "\n") {
lines = append(lines, fmt.Sprintf("// %s", line))
}
in = strings.Join(lines, "\n")
// in case we have a multiline string which ends with \n, we would generate
// empty-line-comments, like `// `. Therefore remove this line comment.
in = strings.TrimSuffix(in, "\n// ")
return in
}
// EscapePathElements escapes non path parameters in path and url encodes them.
func EscapePathElements(path string) string {
elems := strings.Split(path, "/")
for i, e := range elems {
if strings.HasPrefix(e, "{") && strings.HasSuffix(e, "}") {
// This is a path parameter, we don't want to mess with its value
continue
}
elems[i] = url.QueryEscape(e)
}
return strings.Join(elems, "/")
}