Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions cmd/oapi-codegen/oapi-codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down Expand Up @@ -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
Expand Down
26 changes: 13 additions & 13 deletions pkg/codegen/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,48 +29,48 @@ 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)
}
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)
}
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)
}
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)
}
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)
}
Expand All @@ -85,16 +85,16 @@ 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)
}
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)
}
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions pkg/codegen/extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/codegen/merge_schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/codegen/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 3 additions & 7 deletions pkg/codegen/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions pkg/codegen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ type Property struct {
ReadOnly bool
WriteOnly bool
NeedsFormTag bool
Extensions map[string]interface{}
Extensions map[string]any
Deprecated bool
}

Expand Down Expand Up @@ -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 {
Expand Down
31 changes: 11 additions & 20 deletions pkg/codegen/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"net/url"
"reflect"
"regexp"
"slices"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion pkg/codegen/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
},
}
}
Expand Down