diff --git a/cmd/oapi-codegen/oapi-codegen.go b/cmd/oapi-codegen/oapi-codegen.go index 78bea5b1e3..a3e43498c4 100644 --- a/cmd/oapi-codegen/oapi-codegen.go +++ b/cmd/oapi-codegen/oapi-codegen.go @@ -29,7 +29,7 @@ import ( "github.com/oapi-codegen/oapi-codegen/v2/pkg/util" ) -func errExit(format string, args ...interface{}) { +func errExit(format string, args ...any) { if !strings.HasSuffix(format, "\n") { format = format + "\n" } @@ -272,12 +272,13 @@ func main() { } if warnings := opts.Generate.Warnings(); len(warnings) > 0 { - out := "WARNING: A number of warning(s) were returned when validating the GenerateOptions:" + var out strings.Builder + out.WriteString("WARNING: A number of warning(s) were returned when validating the GenerateOptions:") for k, v := range warnings { - out += "\n- " + k + ": " + v + out.WriteString("\n- " + k + ": " + v) } - _, _ = fmt.Fprint(os.Stderr, out) + _, _ = fmt.Fprint(os.Stderr, out.String()) } // If the user asked to output configuration, output it to stdout and exit diff --git a/pkg/codegen/extension.go b/pkg/codegen/extension.go index 579d8a17c9..6bc6bff144 100644 --- a/pkg/codegen/extension.go +++ b/pkg/codegen/extension.go @@ -29,7 +29,7 @@ const ( extOapiCodegenOnlyHonourGoName = "x-oapi-codegen-only-honour-go-name" ) -func extString(extPropValue interface{}) (string, error) { +func extString(extPropValue any) (string, error) { str, ok := extPropValue.(string) if !ok { return "", fmt.Errorf("failed to convert type: %T", extPropValue) @@ -37,11 +37,11 @@ func extString(extPropValue interface{}) (string, error) { return str, nil } -func extTypeName(extPropValue interface{}) (string, error) { +func extTypeName(extPropValue any) (string, error) { return extString(extPropValue) } -func extParsePropGoTypeSkipOptionalPointer(extPropValue interface{}) (bool, error) { +func extParsePropGoTypeSkipOptionalPointer(extPropValue any) (bool, error) { goTypeSkipOptionalPointer, ok := extPropValue.(bool) if !ok { return false, fmt.Errorf("failed to convert type: %T", extPropValue) @@ -49,11 +49,11 @@ func extParsePropGoTypeSkipOptionalPointer(extPropValue interface{}) (bool, erro return goTypeSkipOptionalPointer, nil } -func extParseGoFieldName(extPropValue interface{}) (string, error) { +func extParseGoFieldName(extPropValue any) (string, error) { return extString(extPropValue) } -func extParseOmitEmpty(extPropValue interface{}) (bool, error) { +func extParseOmitEmpty(extPropValue any) (bool, error) { omitEmpty, ok := extPropValue.(bool) if !ok { return false, fmt.Errorf("failed to convert type: %T", extPropValue) @@ -61,7 +61,7 @@ func extParseOmitEmpty(extPropValue interface{}) (bool, error) { return omitEmpty, nil } -func extParseOmitZero(extPropValue interface{}) (bool, error) { +func extParseOmitZero(extPropValue any) (bool, error) { omitZero, ok := extPropValue.(bool) if !ok { return false, fmt.Errorf("failed to convert type: %T", extPropValue) @@ -69,8 +69,8 @@ func extParseOmitZero(extPropValue interface{}) (bool, error) { return omitZero, nil } -func extExtraTags(extPropValue interface{}) (map[string]string, error) { - tagsI, ok := extPropValue.(map[string]interface{}) +func extExtraTags(extPropValue any) (map[string]string, error) { + tagsI, ok := extPropValue.(map[string]any) if !ok { return nil, fmt.Errorf("failed to convert type: %T", extPropValue) } @@ -85,7 +85,7 @@ func extExtraTags(extPropValue interface{}) (map[string]string, error) { return tags, nil } -func extParseGoJsonIgnore(extPropValue interface{}) (bool, error) { +func extParseGoJsonIgnore(extPropValue any) (bool, error) { goJsonIgnore, ok := extPropValue.(bool) if !ok { return false, fmt.Errorf("failed to convert type: %T", extPropValue) @@ -93,8 +93,8 @@ func extParseGoJsonIgnore(extPropValue interface{}) (bool, error) { return goJsonIgnore, nil } -func extParseEnumVarNames(extPropValue interface{}) ([]string, error) { - namesI, ok := extPropValue.([]interface{}) +func extParseEnumVarNames(extPropValue any) ([]string, error) { + namesI, ok := extPropValue.([]any) if !ok { return nil, fmt.Errorf("failed to convert type: %T", extPropValue) } @@ -109,11 +109,11 @@ func extParseEnumVarNames(extPropValue interface{}) ([]string, error) { return names, nil } -func extParseDeprecationReason(extPropValue interface{}) (string, error) { +func extParseDeprecationReason(extPropValue any) (string, error) { return extString(extPropValue) } -func extParseOapiCodegenOnlyHonourGoName(extPropValue interface{}) (bool, error) { +func extParseOapiCodegenOnlyHonourGoName(extPropValue any) (bool, error) { onlyHonourGoName, ok := extPropValue.(bool) if !ok { return false, fmt.Errorf("failed to convert type: %T", extPropValue) diff --git a/pkg/codegen/extension_test.go b/pkg/codegen/extension_test.go index 7a5f363bd7..0223ee9404 100644 --- a/pkg/codegen/extension_test.go +++ b/pkg/codegen/extension_test.go @@ -39,7 +39,7 @@ func Test_extTypeName(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { // kin-openapi no longer returns these as RawMessage - var extPropValue interface{} + var extPropValue any if tt.args.extPropValue != nil { err := json.Unmarshal(tt.args.extPropValue, &extPropValue) assert.NoError(t, err) @@ -93,7 +93,7 @@ func Test_extParsePropGoTypeSkipOptionalPointer(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { // kin-openapi no longer returns these as RawMessage - var extPropValue interface{} + var extPropValue any if tt.args.extPropValue != nil { err := json.Unmarshal(tt.args.extPropValue, &extPropValue) assert.NoError(t, err) diff --git a/pkg/codegen/merge_schemas.go b/pkg/codegen/merge_schemas.go index 04e7b2fa2b..39c499c7da 100644 --- a/pkg/codegen/merge_schemas.go +++ b/pkg/codegen/merge_schemas.go @@ -87,7 +87,7 @@ func mergeAllOf(allOf []*openapi3.SchemaRef) (openapi3.Schema, error) { func mergeOpenapiSchemas(s1, s2 openapi3.Schema, allOf bool) (openapi3.Schema, error) { var result openapi3.Schema - result.Extensions = make(map[string]interface{}) + result.Extensions = make(map[string]any) for k, v := range s1.Extensions { result.Extensions[k] = v } diff --git a/pkg/codegen/operations.go b/pkg/codegen/operations.go index e4d9784702..1743d270ea 100644 --- a/pkg/codegen/operations.go +++ b/pkg/codegen/operations.go @@ -1067,7 +1067,7 @@ func GenerateClientWithResponses(t *template.Template, ops []OperationDefinition } // GenerateTemplates used to generate templates -func GenerateTemplates(templates []string, t *template.Template, ops interface{}) (string, error) { +func GenerateTemplates(templates []string, t *template.Template, ops any) (string, error) { var generatedTemplates []string for _, tmpl := range templates { var buf bytes.Buffer diff --git a/pkg/codegen/prune.go b/pkg/codegen/prune.go index 0d3a889747..e97ba3469e 100644 --- a/pkg/codegen/prune.go +++ b/pkg/codegen/prune.go @@ -2,23 +2,19 @@ package codegen import ( "fmt" + "slices" "github.com/getkin/kin-openapi/openapi3" ) func stringInSlice(a string, list []string) bool { - for _, b := range list { - if b == a { - return true - } - } - return false + return slices.Contains(list, a) } type RefWrapper struct { Ref string HasValue bool - SourceRef interface{} + SourceRef any } func walkSwagger(swagger *openapi3.T, doFn func(RefWrapper) (bool, error)) error { diff --git a/pkg/codegen/schema.go b/pkg/codegen/schema.go index 25f34de38a..cc14db731f 100644 --- a/pkg/codegen/schema.go +++ b/pkg/codegen/schema.go @@ -102,7 +102,7 @@ type Property struct { ReadOnly bool WriteOnly bool NeedsFormTag bool - Extensions map[string]interface{} + Extensions map[string]any Deprecated bool } @@ -270,11 +270,11 @@ func (u UnionElement) String() string { // Method generate union method name for template functions `As/From/Merge`. func (u UnionElement) Method() string { - var method string + var method strings.Builder for _, part := range strings.Split(string(u), `.`) { - method += UppercaseFirstCharacter(part) + method.WriteString(UppercaseFirstCharacter(part)) } - return method + return method.String() } func PropertiesEqual(a, b Property) bool { diff --git a/pkg/codegen/utils.go b/pkg/codegen/utils.go index 79e274f7ee..549d5cffa3 100644 --- a/pkg/codegen/utils.go +++ b/pkg/codegen/utils.go @@ -20,6 +20,7 @@ import ( "net/url" "reflect" "regexp" + "slices" "sort" "strconv" "strings" @@ -205,7 +206,7 @@ func LowercaseFirstCharacters(str string) string { runes := []rune(str) - for i := 0; i < len(runes); i++ { + for i := range runes { next := i + 1 if i != 0 && next < len(runes) && unicode.IsLower(runes[next]) { break @@ -224,25 +225,25 @@ func LowercaseFirstCharacters(str string) string { func ToCamelCase(str string) string { s := strings.Trim(str, " ") - n := "" + var n strings.Builder capNext := true for _, v := range s { if unicode.IsUpper(v) { - n += string(v) + n.WriteString(string(v)) } if unicode.IsDigit(v) { - n += string(v) + n.WriteString(string(v)) } if unicode.IsLower(v) { if capNext { - n += strings.ToUpper(string(v)) + n.WriteString(strings.ToUpper(string(v))) } else { - n += string(v) + n.WriteString(string(v)) } } _, capNext = separatorSet[v] } - return n + return n.String() } // ToCamelCaseWithDigits function will convert query-arg style strings to CamelCase. We will @@ -407,12 +408,7 @@ func schemaXOrder(v *openapi3.SchemaRef) (int64, bool) { // StringInArray checks whether the specified string is present in an array // of strings func StringInArray(str string, array []string) bool { - for _, elt := range array { - if elt == str { - return true - } - } - return false + return slices.Contains(array, str) } // RefPathToObjName returns the name of referenced object without changes. @@ -1074,7 +1070,7 @@ func ParseGoImportExtension(v *openapi3.SchemaRef) (*goImport, error) { goTypeImportExt := v.Value.Extensions[extPropGoImport] - importI, ok := goTypeImportExt.(map[string]interface{}) + importI, ok := goTypeImportExt.(map[string]any) if !ok { return nil, fmt.Errorf("failed to convert type: %T", goTypeImportExt) } @@ -1126,12 +1122,7 @@ func isAdditionalPropertiesExplicitFalse(s *openapi3.Schema) bool { } func sliceContains[E comparable](s []E, v E) bool { - for _, ss := range s { - if ss == v { - return true - } - } - return false + return slices.Contains(s, v) } // FixDuplicateTypeNames renames duplicate type names. diff --git a/pkg/codegen/utils_test.go b/pkg/codegen/utils_test.go index f204fc50c8..3f9e0e8b76 100644 --- a/pkg/codegen/utils_test.go +++ b/pkg/codegen/utils_test.go @@ -160,7 +160,7 @@ func TestSortedSchemaKeysWithXOrder(t *testing.T) { withOrder := func(i float64) *openapi3.SchemaRef { return &openapi3.SchemaRef{ Value: &openapi3.Schema{ - Extensions: map[string]interface{}{"x-order": i}, + Extensions: map[string]any{"x-order": i}, }, } }