Skip to content

Commit

Permalink
Merge pull request #124 from alnpokrovsky/fix
Browse files Browse the repository at this point in the history
Fix package with version alias detection
  • Loading branch information
zcolleen authored Nov 5, 2024
2 parents 3e9ef54 + 379788d commit bda9c15
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 23 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ _testmain.go
/bin
/.gh_token

.idea
.idea
.vscode
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
49 changes: 27 additions & 22 deletions internal/types/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand All @@ -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 ""
}

Expand Down
4 changes: 4 additions & 0 deletions tests/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bda9c15

Please sign in to comment.