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
Changes from 1 commit
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