Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix generic type that has more than 2 params with only one constraint type #1506

Merged
merged 2 commits into from
Mar 20, 2023
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
26 changes: 21 additions & 5 deletions generics.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ type genericTypeSpec struct {
Name string
}

type formalParamType struct {
Name string
Type string
}

func (t *genericTypeSpec) TypeName() string {
if t.TypeSpec != nil {
return t.TypeSpec.TypeName()
Expand All @@ -36,10 +41,21 @@ func (pkgDefs *PackagesDefinitions) parametrizeGenericType(file *ast.File, origi
return nil
}

genericParamTypeDefs := map[string]*genericTypeSpec{}
if len(genericParams) != len(original.TypeSpec.TypeParams.List) {
//generic[x,y any,z any] considered, TODO what if the type is not `any`, but a concrete one, such as `int32|int64` or an certain interface{}
var formals []formalParamType
for _, field := range original.TypeSpec.TypeParams.List {
for _, ident := range field.Names {
formal := formalParamType{Name: ident.Name}
if ident, ok := field.Type.(*ast.Ident); ok {
formal.Type = ident.Name
}
formals = append(formals, formal)
}
}
if len(genericParams) != len(formals) {
return nil
}
genericParamTypeDefs := map[string]*genericTypeSpec{}

for i, genericParam := range genericParams {
arrayDepth := 0
Expand All @@ -59,7 +75,7 @@ func (pkgDefs *PackagesDefinitions) parametrizeGenericType(file *ast.File, origi
}
}

genericParamTypeDefs[original.TypeSpec.TypeParams.List[i].Names[0].Name] = &genericTypeSpec{
genericParamTypeDefs[formals[i].Name] = &genericTypeSpec{
ArrayDepth: arrayDepth,
TypeSpec: typeDef,
Name: genericParam,
Expand All @@ -68,8 +84,8 @@ func (pkgDefs *PackagesDefinitions) parametrizeGenericType(file *ast.File, origi

name = fmt.Sprintf("%s%s-", string(IgnoreNameOverridePrefix), original.TypeName())
var nameParts []string
for _, def := range original.TypeSpec.TypeParams.List {
if specDef, ok := genericParamTypeDefs[def.Names[0].Name]; ok {
for _, def := range formals {
if specDef, ok := genericParamTypeDefs[def.Name]; ok {
var prefix = ""
if specDef.ArrayDepth == 1 {
prefix = "array_"
Expand Down
8 changes: 8 additions & 0 deletions testdata/generics_basic/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ type Response[T any, X any] struct {
Status string
}

type Response2[T, X any, Y any] struct {
Data T
Meta X

Status Y
}

type StringStruct struct {
Data string
}
Expand All @@ -29,6 +36,7 @@ type StringStruct struct {
// @Success 203 {object} web.GenericResponse[types.Field[int]]
// @Success 204 {object} Response[string, types.Field[int]]
// @Success 205 {object} Response[StringStruct, types.Field[int]]
// @Success 206 {object} Response2[string, types.Field[int],string]
// @Success 222 {object} web.GenericResponseMulti[types.Post, types.Post]
// @Failure 400 {object} web.APIError "We need ID!!"
// @Failure 404 {object} web.APIError "Can not find ID"
Expand Down
20 changes: 20 additions & 0 deletions testdata/generics_basic/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@
"$ref": "#/definitions/api.Response-api_StringStruct-types_Field-int"
}
},
"206": {
"description": "Partial Content",
"schema": {
"$ref": "#/definitions/api.Response2-string-types_Field-int-string"
}
},
"222": {
"description": "",
"schema": {
Expand Down Expand Up @@ -210,6 +216,20 @@
}
}
},
"api.Response2-string-types_Field-int-string": {
"type": "object",
"properties": {
"data": {
"type": "string"
},
"meta": {
"$ref": "#/definitions/types.Field-int"
},
"status": {
"type": "string"
}
}
},
"api.StringStruct": {
"type": "object",
"properties": {
Expand Down