From a0e7ed18c2171d82f452f094a20e1606dc20c74a Mon Sep 17 00:00:00 2001 From: eyasy1217 Date: Wed, 10 Jul 2024 22:58:50 +0900 Subject: [PATCH] support import shadowing --- .../tests/import_shadowing/foo/foo.go | 3 + .../internal/tests/import_shadowing/import.go | 11 ++++ .../internal/tests/import_shadowing/mock.go | 59 +++++++++++++++++++ mockgen/mockgen.go | 14 +++++ 4 files changed, 87 insertions(+) create mode 100644 mockgen/internal/tests/import_shadowing/foo/foo.go create mode 100644 mockgen/internal/tests/import_shadowing/import.go create mode 100644 mockgen/internal/tests/import_shadowing/mock.go diff --git a/mockgen/internal/tests/import_shadowing/foo/foo.go b/mockgen/internal/tests/import_shadowing/foo/foo.go new file mode 100644 index 0000000..5dc6dca --- /dev/null +++ b/mockgen/internal/tests/import_shadowing/foo/foo.go @@ -0,0 +1,3 @@ +package foo + +type MyInt int diff --git a/mockgen/internal/tests/import_shadowing/import.go b/mockgen/internal/tests/import_shadowing/import.go new file mode 100644 index 0000000..01344da --- /dev/null +++ b/mockgen/internal/tests/import_shadowing/import.go @@ -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 +} diff --git a/mockgen/internal/tests/import_shadowing/mock.go b/mockgen/internal/tests/import_shadowing/mock.go new file mode 100644 index 0000000..8c694a7 --- /dev/null +++ b/mockgen/internal/tests/import_shadowing/mock.go @@ -0,0 +1,59 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: import.go +// +// Generated by this command: +// +// mockgen -destination mock.go -source import.go -package import_shadowing . Bar +// + +// Package import_shadowing is a generated GoMock package. +package import_shadowing + +import ( + reflect "reflect" + + gomock "go.uber.org/mock/gomock" + fooPkg "go.uber.org/mock/mockgen/internal/tests/import_shadowing/foo" +) + +// MockBar is a mock of Bar interface. +type MockBar struct { + ctrl *gomock.Controller + recorder *MockBarMockRecorder +} + +// MockBarMockRecorder is the mock recorder for MockBar. +type MockBarMockRecorder struct { + mock *MockBar +} + +// NewMockBar creates a new mock instance. +func NewMockBar(ctrl *gomock.Controller) *MockBar { + mock := &MockBar{ctrl: ctrl} + mock.recorder = &MockBarMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockBar) EXPECT() *MockBarMockRecorder { + return m.recorder +} + +// ISGOMOCK indicates that this struct is a gomock mock. +func (m *MockBar) ISGOMOCK() struct{} { + return struct{}{} +} + +// Hoge mocks base method. +func (m *MockBar) Hoge(foo string) fooPkg.MyInt { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Hoge", foo) + ret0, _ := ret[0].(fooPkg.MyInt) + return ret0 +} + +// Hoge indicates an expected call of Hoge. +func (mr *MockBarMockRecorder) Hoge(foo any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Hoge", reflect.TypeOf((*MockBar)(nil).Hoge), foo) +} diff --git a/mockgen/mockgen.go b/mockgen/mockgen.go index 5d57868..3d2fc00 100644 --- a/mockgen/mockgen.go +++ b/mockgen/mockgen.go @@ -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 { @@ -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