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

Add support for generic types expressed by selector expressions #84

Merged
merged 1 commit into from
Apr 3, 2024
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
4 changes: 2 additions & 2 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ func findTarget(input processInput) (output processOutput, err error) {
}

output.imports = imports
output.genericTypes = buildGenericTypesFromSpec(ts)
output.genericTypes = buildGenericTypesFromSpec(ts, types, input.astPackage.Name)

if it, ok := ts.Type.(*ast.InterfaceType); ok {
output.methods, err = processInterface(it, targetProcessInput{
Expand Down Expand Up @@ -555,7 +555,7 @@ func processIdent(i *ast.Ident, input targetProcessInput) (methodsList, error) {
return nil, errors.Wrap(errNotAnInterface, t.Name.Name)
}

genericsTypes = buildGenericTypesFromSpec(t)
genericsTypes = buildGenericTypesFromSpec(t, input.types, input.typesPrefix)
break
}
}
Expand Down
38 changes: 28 additions & 10 deletions generator/generics.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,40 @@ func (g genericTypes) buildVars() (string, string) {
return buildGenericsWithBrackets(types), buildGenericsWithBrackets(params)
}

func buildGenericTypesFromSpec(ts *ast.TypeSpec) (types genericTypes) {
func buildGenericTypesFromSpec(ts *ast.TypeSpec, allTypes []*ast.TypeSpec, typesPrefix string) (types genericTypes) {
if ts.TypeParams != nil {
for _, param := range ts.TypeParams.List {
if param != nil {
if gpt, ok := param.Type.(*ast.Ident); ok {
var paramNames []string
for _, name := range param.Names {
if name != nil {
paramNames = append(paramNames, name.Name)
var typeIdentifier string
switch t := param.Type.(type) {
case *ast.Ident:
prefix := ""
if typesPrefix != "" {
for _, at := range allTypes {
if at.Name.Name == t.Name {
prefix = typesPrefix + "."
break
}
}
}
types = append(types, genericType{
Type: gpt.Name,
Names: paramNames,
})

typeIdentifier = prefix + t.Name
case *ast.SelectorExpr:
typeIdentifier = t.X.(*ast.Ident).Name + "." + t.Sel.Name
default:
panic("unsupported generic type")
}

var paramNames []string
for _, name := range param.Names {
if name != nil {
paramNames = append(paramNames, name.Name)
}
}
types = append(types, genericType{
Type: typeIdentifier,
Names: paramNames,
})
}
}
}
Expand Down
116 changes: 114 additions & 2 deletions generator/generics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ func Test_genericTypes_buildVars(t *testing.T) {

func Test_buildGenericTypesFromSpec(t *testing.T) {
type args struct {
ts *ast.TypeSpec
ts *ast.TypeSpec
allTypes []*ast.TypeSpec
typesPrefix string
}
tests := []struct {
name string
Expand Down Expand Up @@ -116,10 +118,120 @@ func Test_buildGenericTypesFromSpec(t *testing.T) {
},
},
},
{
name: "build generic types foo.Bar from spec",
args: args{
ts: &ast.TypeSpec{
TypeParams: &ast.FieldList{
List: []*ast.Field{
{
Type: &ast.SelectorExpr{
X: &ast.Ident{
Name: "foo",
},
Sel: &ast.Ident{
Name: "Bar",
},
},
Names: []*ast.Ident{
{
Name: "I",
},
{
Name: "O",
},
},
},
},
},
},
},
wantTypes: genericTypes{
{
Type: "foo.Bar",
Names: []string{"I", "O"},
},
},
},
{
name: "build generic types Bar from spec without types prefix",
args: args{
ts: &ast.TypeSpec{
TypeParams: &ast.FieldList{
List: []*ast.Field{
{
Type: &ast.Ident{
Name: "Bar",
},
Names: []*ast.Ident{
{
Name: "I",
},
{
Name: "O",
},
},
},
},
},
},
allTypes: []*ast.TypeSpec{
{
Name: &ast.Ident{
Name: "Bar",
},
},
},
},
wantTypes: genericTypes{
{
Type: "Bar",
Names: []string{"I", "O"},
},
},
},
{
name: "build generic types Bar from spec with types prefix",
args: args{
ts: &ast.TypeSpec{
TypeParams: &ast.FieldList{
List: []*ast.Field{
{
Type: &ast.Ident{
Name: "Bar",
},
Names: []*ast.Ident{
{
Name: "I",
},
{
Name: "O",
},
},
},
},
},
},
allTypes: []*ast.TypeSpec{
{
Name: &ast.Ident{
Name: "Bar",
},
},
},
typesPrefix: "prefix",
},
wantTypes: genericTypes{
{
Type: "prefix.Bar",
Names: []string{"I", "O"},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotTypes := buildGenericTypesFromSpec(tt.args.ts); !reflect.DeepEqual(gotTypes, tt.wantTypes) {
if gotTypes := buildGenericTypesFromSpec(tt.args.ts, tt.args.allTypes, tt.args.typesPrefix); !reflect.DeepEqual(gotTypes, tt.wantTypes) {
t.Errorf("buildGenericTypesFromSpec() = %v, want %v", gotTypes, tt.wantTypes)
}
})
Expand Down
Loading