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 10, 2024
1 parent 2c0a7fd commit e80cb0b
Show file tree
Hide file tree
Showing 4 changed files with 91 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.

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

isImportShadowing := func() bool {
for _, intf := range pkg.Interfaces {
for _, m := range intf.Methods {
for _, p := range m.In {
if p.Name == pkgName {
return true
}
}
}
}
return false
}

if isImportShadowing() {
pkgName = base + "Pkg"
}

// Avoid importing package if source pkg == output pkg
if pth == pkg.PkgPath && outputPackagePath == pkg.PkgPath {
continue
Expand Down Expand Up @@ -756,6 +773,7 @@ func (g *generator) getArgNames(m *model.Method, in bool) []string {
params = m.Out
}
argNames := make([]string, len(params))

for i, p := range params {
name := p.Name
if name == "" || name == "_" {
Expand Down

0 comments on commit e80cb0b

Please sign in to comment.