Skip to content

Commit

Permalink
Fix issue with type aliases in moq
Browse files Browse the repository at this point in the history
  • Loading branch information
LandonTClipp committed Dec 26, 2024
1 parent b814a32 commit d416405
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 210 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 0 additions & 64 deletions pkg/fixtures/iface_new_type/mock_interface_1_test.go

This file was deleted.

64 changes: 0 additions & 64 deletions pkg/fixtures/iface_new_type/mock_interface_2_test.go

This file was deleted.

64 changes: 0 additions & 64 deletions pkg/fixtures/iface_new_type/mock_interface_3_test.go

This file was deleted.

40 changes: 29 additions & 11 deletions pkg/registry/method_scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"go/types"
"strconv"

"github.com/rs/zerolog"
)

// MethodScope is the sub-registry for allocating variables present in
Expand Down Expand Up @@ -74,23 +76,37 @@ func (m MethodScope) searchVar(name string) (*Var, bool) {
return nil, false
}

func (m MethodScope) populateImportNamedType(
ctx context.Context,
t interface {
Obj() *types.TypeName
TypeArgs() *types.TypeList
},
imports map[string]*Package,
) {
if pkg := t.Obj().Pkg(); pkg != nil {
imports[pkg.Path()] = m.registry.AddImport(ctx, pkg)
}
// The imports of a Type with a TypeList must be added to the imports list
// For example: Foo[otherpackage.Bar] , must have otherpackage imported
if targs := t.TypeArgs(); targs != nil {
for i := 0; i < targs.Len(); i++ {
m.populateImports(ctx, targs.At(i), imports)
}
}
}

// populateImports extracts all the package imports for a given type
// recursively. The imported packages by a single type can be more than
// one (ex: map[a.Type]b.Type).
func (m MethodScope) populateImports(ctx context.Context, t types.Type, imports map[string]*Package) {
log := zerolog.Ctx(ctx).With().
Str("type-str", t.String()).Logger()
switch t := t.(type) {
case *types.Named:
if pkg := t.Obj().Pkg(); pkg != nil {
imports[pkg.Path()] = m.registry.AddImport(ctx, pkg)
}
// The imports of a Type with a TypeList must be added to the imports list
// For example: Foo[otherpackage.Bar] , must have otherpackage imported
if targs := t.TypeArgs(); targs != nil {
for i := 0; i < targs.Len(); i++ {
m.populateImports(ctx, targs.At(i), imports)
}
}

m.populateImportNamedType(ctx, t, imports)
case *types.Alias:
m.populateImportNamedType(ctx, t, imports)
case *types.Array:
m.populateImports(ctx, t.Elem(), imports)

Expand Down Expand Up @@ -127,6 +143,8 @@ func (m MethodScope) populateImports(ctx context.Context, t types.Type, imports
for i := 0; i < t.NumEmbeddeds(); i++ {
m.populateImports(ctx, t.EmbeddedType(i), imports)
}
default:
log.Debug().Msg("unable to determine type of object")
}
}

Expand Down

0 comments on commit d416405

Please sign in to comment.