Skip to content

Commit

Permalink
support import shadowing
Browse files Browse the repository at this point in the history
  • Loading branch information
eyasy1217 committed Jul 11, 2024
1 parent 2c0a7fd commit a0e7ed1
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
3 changes: 3 additions & 0 deletions mockgen/internal/tests/import_shadowing/foo/foo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package foo

type MyInt int
11 changes: 11 additions & 0 deletions mockgen/internal/tests/import_shadowing/import.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package import_shadowing

import (
"go.uber.org/mock/mockgen/internal/tests/import_shadowing/foo"
)

//go:generate mockgen -destination mock.go -source import.go -package import_shadowing . Bar

type Bar interface {
Hoge(foo string) foo.MyInt
}
59 changes: 59 additions & 0 deletions mockgen/internal/tests/import_shadowing/mock.go

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

14 changes: 14 additions & 0 deletions mockgen/mockgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,16 @@ func (g *generator) Generate(pkg *model.Package, outputPkgName string, outputPac

g.packageMap = make(map[string]string, len(im))
localNames := make(map[string]bool, len(im))
paramNamesInterfaces := make(map[string]struct{}, 0)
for _, intf := range pkg.Interfaces {
for _, m := range intf.Methods {
for _, i := range m.In {
if i.Name != "" {
paramNamesInterfaces[i.Name] = struct{}{}
}
}
}
}
for _, pth := range sortedPaths {
base, ok := packagesName[pth]
if !ok {
Expand All @@ -386,6 +396,10 @@ func (g *generator) Generate(pkg *model.Package, outputPkgName string, outputPac
i++
}

if _, isImportShadowing := paramNamesInterfaces[pkgName]; isImportShadowing {
pkgName = base + "Pkg"
}

// Avoid importing package if source pkg == output pkg
if pth == pkg.PkgPath && outputPackagePath == pkg.PkgPath {
continue
Expand Down

0 comments on commit a0e7ed1

Please sign in to comment.