diff --git a/.gitignore b/.gitignore index 3c0f7a1..9348633 100644 --- a/.gitignore +++ b/.gitignore @@ -30,4 +30,5 @@ _testmain.go /bin /.gh_token -.idea \ No newline at end of file +.idea +.vscode diff --git a/go.mod b/go.mod index 0078d61..44a5b86 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( ) require ( + github.com/gofrs/uuid/v5 v5.3.0 github.com/kr/text v0.2.0 // indirect golang.org/x/mod v0.8.0 // indirect golang.org/x/sys v0.15.0 // indirect diff --git a/go.sum b/go.sum index 4bc120e..197fc67 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gofrs/uuid/v5 v5.3.0 h1:m0mUMr+oVYUdxpMLgSYCZiXe7PuVPnI94+OMeVBNedk= +github.com/gofrs/uuid/v5 v5.3.0/go.mod h1:CDOjlDMVAtN56jqyRUZh58JT31Tiw7/oQyEXZV+9bD8= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= diff --git a/internal/types/interface.go b/internal/types/interface.go index 90bc459..50e0422 100644 --- a/internal/types/interface.go +++ b/internal/types/interface.go @@ -97,17 +97,27 @@ func isExportedInterfaceAlias(typeSpec *ast.TypeSpec, fileImports []*ast.ImportS return false } + name := selector.Sel.Name srcPkgPath := findSourcePackage(ident, fileImports) - srcAst, err := getPackageAst(srcPkgPath) + srcPackageAst, err := getPackageAst(srcPkgPath) if err != nil { return false } - typeSpec, imports := findTypeSpecInPackage(srcAst, selector.Sel.Name) + for _, f := range srcPackageAst.Files { + if f == nil { + continue + } + types := findAllTypeSpecsInFile(f) + typeSpec, found := findTypeByName(types, name) + if found { + // we have to check recursively because checked typed might be + // another alias to other interface + return isInterface(typeSpec, f.Imports) + } + } - // we have to check recursively because checked typed might be - // another alias to other interface - return isInterface(typeSpec, imports) + return false } func getPackageAst(packagePath string) (*ast.Package, error) { @@ -125,21 +135,6 @@ func getPackageAst(packagePath string) (*ast.Package, error) { return srcAst, nil } -func findTypeSpecInPackage(p *ast.Package, name string) (typeSpec *ast.TypeSpec, imports []*ast.ImportSpec) { - for _, f := range p.Files { - if f == nil { - continue - } - types := findAllTypeSpecsInFile(f) - typeSpec, found := findTypeByName(types, name) - if found { - return typeSpec, f.Imports - } - } - - return -} - func findTypeByName(types []*ast.TypeSpec, name string) (*ast.TypeSpec, bool) { for _, ts := range types { if ts.Name.Name == name { @@ -161,12 +156,22 @@ func findSourcePackage(ident *ast.Ident, imports []*ast.ImportSpec) string { continue } - slash := strings.LastIndex(cleanPath, "/") - if ident.Name == cleanPath[slash+1:] { + // try last segment, like in "github.com/my/package/identName" + lastSlash := strings.LastIndex(cleanPath, "/") + if ident.Name == cleanPath[lastSlash+1:] { return cleanPath } + + // try prev segment, like in "github.com/my/package/identName/v5" + if cleanPath[lastSlash+1] == 'v' { + prevSlash := strings.LastIndex(cleanPath[:lastSlash], "/") + if ident.Name == cleanPath[prevSlash+1:lastSlash] { + return cleanPath + } + } } + // todo: should not reach here? return "" } diff --git a/tests/types.go b/tests/types.go index c4a37d1..925f1b6 100644 --- a/tests/types.go +++ b/tests/types.go @@ -5,10 +5,14 @@ import ( "context" "io" + "github.com/gofrs/uuid/v5" "google.golang.org/protobuf/proto" ) type ( + // Alias for package with version + gen = uuid.Gen + //Formatter interface is used to test code generated by minimock Formatter interface { formatter //to check if variadic functions are supported