Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue #298 - go 1.23 go/types alias change #301

Merged
merged 2 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion fixtures/dup_packages/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/maxbrunsfeld/counterfeiter/v6/fixtures/dup_packages

go 1.12
go 1.22
5 changes: 5 additions & 0 deletions fixtures/type_aliases/extra/m.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package extra // import "github.com/maxbrunsfeld/counterfeiter/v6/fixtures/type_aliases/extra"

import "github.com/maxbrunsfeld/counterfeiter/v6/fixtures/type_aliases/extra/primitive"

type M = primitive.M
3 changes: 3 additions & 0 deletions fixtures/type_aliases/extra/primitive/m.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package primitive // import "github.com/maxbrunsfeld/counterfeiter/v6/fixtures/type_aliases/primitive"

type M map[string]interface{}
3 changes: 3 additions & 0 deletions fixtures/type_aliases/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/maxbrunsfeld/counterfeiter/v6/fixtures/type_aliases

go 1.22
14 changes: 14 additions & 0 deletions fixtures/type_aliases/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package type_aliases // import "github.com/maxbrunsfeld/counterfeiter/v6/fixtures/type_aliases"

import (
"context"

"github.com/maxbrunsfeld/counterfeiter/v6/fixtures/type_aliases/extra"
)

//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate

//counterfeiter:generate . WithAliasedType
type WithAliasedType interface {
FindExample(ctx context.Context, filter extra.M) ([]string, error)
}
23 changes: 16 additions & 7 deletions generator/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,10 @@ func (f *Fake) addImportsFor(typ types.Type) {
f.addImportsFor(t.Elem())
case *types.Chan:
f.addImportsFor(t.Elem())
case *types.Alias:
f.addImportsForNamedType(t)
case *types.Named:
if t.Obj() != nil && t.Obj().Pkg() != nil {
typeArgs := t.TypeArgs()
for i := 0; i < typeArgs.Len(); i++ {
f.addImportsFor(typeArgs.At(i))
}
f.Imports.Add(t.Obj().Pkg().Name(), t.Obj().Pkg().Path())
}
f.addImportsForNamedType(t)
case *types.Slice:
f.addImportsFor(t.Elem())
case *types.Array:
Expand All @@ -154,3 +150,16 @@ func (f *Fake) addImportsFor(typ types.Type) {
log.Printf("!!! WARNING: Missing case for type %s\n", reflect.TypeOf(typ).String())
}
}

func (f *Fake) addImportsForNamedType(t interface {
Obj() *types.TypeName
TypeArgs() *types.TypeList
}) {
if t.Obj() != nil && t.Obj().Pkg() != nil {
typeArgs := t.TypeArgs()
for i := 0; i < typeArgs.Len(); i++ {
f.addImportsFor(typeArgs.At(i))
}
f.Imports.Add(t.Obj().Pkg().Name(), t.Obj().Pkg().Path())
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
)

go 1.22.0
go 1.23

toolchain go1.23.1
19 changes: 19 additions & 0 deletions integration/roundtrip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,25 @@ func runTests(t *testing.T, when spec.G, it spec.S) {
})
})

when("generating interfaces using type aliases", func() {
it.Before(func() {
relativeDir = filepath.Join(relativeDir, "type_aliases")
copyDirFunc()
})
it("imports the aliased type, not the underlying type", func() {
cache := &generator.FakeCache{}
pkgPath := "github.com/maxbrunsfeld/counterfeiter/v6/fixtures/type_aliases"
interfaceName := "WithAliasedType"
fakePackageName := "type_aliasesfakes"
f, err := generator.NewFake(generator.InterfaceOrFunction, interfaceName, pkgPath, "Fake"+interfaceName, fakePackageName, "", baseDir, cache)
Expect(err).NotTo(HaveOccurred())
b, err := f.Generate(false)
Expect(err).NotTo(HaveOccurred())
Expect(string(b)).NotTo(ContainSubstring("primitive"))
Expect(string(b)).To(ContainSubstring(`"github.com/maxbrunsfeld/counterfeiter/v6/fixtures/type_aliases/extra"`))
})
})

when(name, func() {
t := func(interfaceName string, filename string, subDir string, files ...string) {
when("working with "+filename, func() {
Expand Down
Loading