Skip to content

Commit b00a8ea

Browse files
style(gofix): Apply go fix (#2229)
Signed-off-by: Gaiaz Iusipov <g.iusipov@gmail.com>
1 parent 4b72bdb commit b00a8ea

File tree

9 files changed

+41
-53
lines changed

9 files changed

+41
-53
lines changed

cmd/oapi-codegen/oapi-codegen.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
"github.com/oapi-codegen/oapi-codegen/v2/pkg/util"
3030
)
3131

32-
func errExit(format string, args ...interface{}) {
32+
func errExit(format string, args ...any) {
3333
if !strings.HasSuffix(format, "\n") {
3434
format = format + "\n"
3535
}
@@ -272,12 +272,13 @@ func main() {
272272
}
273273

274274
if warnings := opts.Generate.Warnings(); len(warnings) > 0 {
275-
out := "WARNING: A number of warning(s) were returned when validating the GenerateOptions:"
275+
var out strings.Builder
276+
out.WriteString("WARNING: A number of warning(s) were returned when validating the GenerateOptions:")
276277
for k, v := range warnings {
277-
out += "\n- " + k + ": " + v
278+
out.WriteString("\n- " + k + ": " + v)
278279
}
279280

280-
_, _ = fmt.Fprint(os.Stderr, out)
281+
_, _ = fmt.Fprint(os.Stderr, out.String())
281282
}
282283

283284
// If the user asked to output configuration, output it to stdout and exit

pkg/codegen/extension.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,48 +29,48 @@ const (
2929
extOapiCodegenOnlyHonourGoName = "x-oapi-codegen-only-honour-go-name"
3030
)
3131

32-
func extString(extPropValue interface{}) (string, error) {
32+
func extString(extPropValue any) (string, error) {
3333
str, ok := extPropValue.(string)
3434
if !ok {
3535
return "", fmt.Errorf("failed to convert type: %T", extPropValue)
3636
}
3737
return str, nil
3838
}
3939

40-
func extTypeName(extPropValue interface{}) (string, error) {
40+
func extTypeName(extPropValue any) (string, error) {
4141
return extString(extPropValue)
4242
}
4343

44-
func extParsePropGoTypeSkipOptionalPointer(extPropValue interface{}) (bool, error) {
44+
func extParsePropGoTypeSkipOptionalPointer(extPropValue any) (bool, error) {
4545
goTypeSkipOptionalPointer, ok := extPropValue.(bool)
4646
if !ok {
4747
return false, fmt.Errorf("failed to convert type: %T", extPropValue)
4848
}
4949
return goTypeSkipOptionalPointer, nil
5050
}
5151

52-
func extParseGoFieldName(extPropValue interface{}) (string, error) {
52+
func extParseGoFieldName(extPropValue any) (string, error) {
5353
return extString(extPropValue)
5454
}
5555

56-
func extParseOmitEmpty(extPropValue interface{}) (bool, error) {
56+
func extParseOmitEmpty(extPropValue any) (bool, error) {
5757
omitEmpty, ok := extPropValue.(bool)
5858
if !ok {
5959
return false, fmt.Errorf("failed to convert type: %T", extPropValue)
6060
}
6161
return omitEmpty, nil
6262
}
6363

64-
func extParseOmitZero(extPropValue interface{}) (bool, error) {
64+
func extParseOmitZero(extPropValue any) (bool, error) {
6565
omitZero, ok := extPropValue.(bool)
6666
if !ok {
6767
return false, fmt.Errorf("failed to convert type: %T", extPropValue)
6868
}
6969
return omitZero, nil
7070
}
7171

72-
func extExtraTags(extPropValue interface{}) (map[string]string, error) {
73-
tagsI, ok := extPropValue.(map[string]interface{})
72+
func extExtraTags(extPropValue any) (map[string]string, error) {
73+
tagsI, ok := extPropValue.(map[string]any)
7474
if !ok {
7575
return nil, fmt.Errorf("failed to convert type: %T", extPropValue)
7676
}
@@ -85,16 +85,16 @@ func extExtraTags(extPropValue interface{}) (map[string]string, error) {
8585
return tags, nil
8686
}
8787

88-
func extParseGoJsonIgnore(extPropValue interface{}) (bool, error) {
88+
func extParseGoJsonIgnore(extPropValue any) (bool, error) {
8989
goJsonIgnore, ok := extPropValue.(bool)
9090
if !ok {
9191
return false, fmt.Errorf("failed to convert type: %T", extPropValue)
9292
}
9393
return goJsonIgnore, nil
9494
}
9595

96-
func extParseEnumVarNames(extPropValue interface{}) ([]string, error) {
97-
namesI, ok := extPropValue.([]interface{})
96+
func extParseEnumVarNames(extPropValue any) ([]string, error) {
97+
namesI, ok := extPropValue.([]any)
9898
if !ok {
9999
return nil, fmt.Errorf("failed to convert type: %T", extPropValue)
100100
}
@@ -109,11 +109,11 @@ func extParseEnumVarNames(extPropValue interface{}) ([]string, error) {
109109
return names, nil
110110
}
111111

112-
func extParseDeprecationReason(extPropValue interface{}) (string, error) {
112+
func extParseDeprecationReason(extPropValue any) (string, error) {
113113
return extString(extPropValue)
114114
}
115115

116-
func extParseOapiCodegenOnlyHonourGoName(extPropValue interface{}) (bool, error) {
116+
func extParseOapiCodegenOnlyHonourGoName(extPropValue any) (bool, error) {
117117
onlyHonourGoName, ok := extPropValue.(bool)
118118
if !ok {
119119
return false, fmt.Errorf("failed to convert type: %T", extPropValue)

pkg/codegen/extension_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func Test_extTypeName(t *testing.T) {
3939
for _, tt := range tests {
4040
t.Run(tt.name, func(t *testing.T) {
4141
// kin-openapi no longer returns these as RawMessage
42-
var extPropValue interface{}
42+
var extPropValue any
4343
if tt.args.extPropValue != nil {
4444
err := json.Unmarshal(tt.args.extPropValue, &extPropValue)
4545
assert.NoError(t, err)
@@ -93,7 +93,7 @@ func Test_extParsePropGoTypeSkipOptionalPointer(t *testing.T) {
9393
for _, tt := range tests {
9494
t.Run(tt.name, func(t *testing.T) {
9595
// kin-openapi no longer returns these as RawMessage
96-
var extPropValue interface{}
96+
var extPropValue any
9797
if tt.args.extPropValue != nil {
9898
err := json.Unmarshal(tt.args.extPropValue, &extPropValue)
9999
assert.NoError(t, err)

pkg/codegen/merge_schemas.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func mergeAllOf(allOf []*openapi3.SchemaRef) (openapi3.Schema, error) {
8787
func mergeOpenapiSchemas(s1, s2 openapi3.Schema, allOf bool) (openapi3.Schema, error) {
8888
var result openapi3.Schema
8989

90-
result.Extensions = make(map[string]interface{})
90+
result.Extensions = make(map[string]any)
9191
for k, v := range s1.Extensions {
9292
result.Extensions[k] = v
9393
}

pkg/codegen/operations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,7 @@ func GenerateClientWithResponses(t *template.Template, ops []OperationDefinition
10671067
}
10681068

10691069
// GenerateTemplates used to generate templates
1070-
func GenerateTemplates(templates []string, t *template.Template, ops interface{}) (string, error) {
1070+
func GenerateTemplates(templates []string, t *template.Template, ops any) (string, error) {
10711071
var generatedTemplates []string
10721072
for _, tmpl := range templates {
10731073
var buf bytes.Buffer

pkg/codegen/prune.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,19 @@ package codegen
22

33
import (
44
"fmt"
5+
"slices"
56

67
"github.com/getkin/kin-openapi/openapi3"
78
)
89

910
func stringInSlice(a string, list []string) bool {
10-
for _, b := range list {
11-
if b == a {
12-
return true
13-
}
14-
}
15-
return false
11+
return slices.Contains(list, a)
1612
}
1713

1814
type RefWrapper struct {
1915
Ref string
2016
HasValue bool
21-
SourceRef interface{}
17+
SourceRef any
2218
}
2319

2420
func walkSwagger(swagger *openapi3.T, doFn func(RefWrapper) (bool, error)) error {

pkg/codegen/schema.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ type Property struct {
102102
ReadOnly bool
103103
WriteOnly bool
104104
NeedsFormTag bool
105-
Extensions map[string]interface{}
105+
Extensions map[string]any
106106
Deprecated bool
107107
}
108108

@@ -270,11 +270,11 @@ func (u UnionElement) String() string {
270270

271271
// Method generate union method name for template functions `As/From/Merge`.
272272
func (u UnionElement) Method() string {
273-
var method string
273+
var method strings.Builder
274274
for _, part := range strings.Split(string(u), `.`) {
275-
method += UppercaseFirstCharacter(part)
275+
method.WriteString(UppercaseFirstCharacter(part))
276276
}
277-
return method
277+
return method.String()
278278
}
279279

280280
func PropertiesEqual(a, b Property) bool {

pkg/codegen/utils.go

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"net/url"
2121
"reflect"
2222
"regexp"
23+
"slices"
2324
"sort"
2425
"strconv"
2526
"strings"
@@ -205,7 +206,7 @@ func LowercaseFirstCharacters(str string) string {
205206

206207
runes := []rune(str)
207208

208-
for i := 0; i < len(runes); i++ {
209+
for i := range runes {
209210
next := i + 1
210211
if i != 0 && next < len(runes) && unicode.IsLower(runes[next]) {
211212
break
@@ -224,25 +225,25 @@ func LowercaseFirstCharacters(str string) string {
224225
func ToCamelCase(str string) string {
225226
s := strings.Trim(str, " ")
226227

227-
n := ""
228+
var n strings.Builder
228229
capNext := true
229230
for _, v := range s {
230231
if unicode.IsUpper(v) {
231-
n += string(v)
232+
n.WriteString(string(v))
232233
}
233234
if unicode.IsDigit(v) {
234-
n += string(v)
235+
n.WriteString(string(v))
235236
}
236237
if unicode.IsLower(v) {
237238
if capNext {
238-
n += strings.ToUpper(string(v))
239+
n.WriteString(strings.ToUpper(string(v)))
239240
} else {
240-
n += string(v)
241+
n.WriteString(string(v))
241242
}
242243
}
243244
_, capNext = separatorSet[v]
244245
}
245-
return n
246+
return n.String()
246247
}
247248

248249
// ToCamelCaseWithDigits function will convert query-arg style strings to CamelCase. We will
@@ -407,12 +408,7 @@ func schemaXOrder(v *openapi3.SchemaRef) (int64, bool) {
407408
// StringInArray checks whether the specified string is present in an array
408409
// of strings
409410
func StringInArray(str string, array []string) bool {
410-
for _, elt := range array {
411-
if elt == str {
412-
return true
413-
}
414-
}
415-
return false
411+
return slices.Contains(array, str)
416412
}
417413

418414
// RefPathToObjName returns the name of referenced object without changes.
@@ -1074,7 +1070,7 @@ func ParseGoImportExtension(v *openapi3.SchemaRef) (*goImport, error) {
10741070

10751071
goTypeImportExt := v.Value.Extensions[extPropGoImport]
10761072

1077-
importI, ok := goTypeImportExt.(map[string]interface{})
1073+
importI, ok := goTypeImportExt.(map[string]any)
10781074
if !ok {
10791075
return nil, fmt.Errorf("failed to convert type: %T", goTypeImportExt)
10801076
}
@@ -1126,12 +1122,7 @@ func isAdditionalPropertiesExplicitFalse(s *openapi3.Schema) bool {
11261122
}
11271123

11281124
func sliceContains[E comparable](s []E, v E) bool {
1129-
for _, ss := range s {
1130-
if ss == v {
1131-
return true
1132-
}
1133-
}
1134-
return false
1125+
return slices.Contains(s, v)
11351126
}
11361127

11371128
// FixDuplicateTypeNames renames duplicate type names.

pkg/codegen/utils_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func TestSortedSchemaKeysWithXOrder(t *testing.T) {
160160
withOrder := func(i float64) *openapi3.SchemaRef {
161161
return &openapi3.SchemaRef{
162162
Value: &openapi3.Schema{
163-
Extensions: map[string]interface{}{"x-order": i},
163+
Extensions: map[string]any{"x-order": i},
164164
},
165165
}
166166
}

0 commit comments

Comments
 (0)