Skip to content

Commit

Permalink
Use Go 1.23, support imported type aliases (#225)
Browse files Browse the repository at this point in the history
Go 1.23 changed how go/types handles type aliases, which breaks code
generation if you use a package that has an alias type (example:
https://pkg.go.dev/github.com/golang/protobuf@v1.5.3/ptypes/timestamp#Timestamp
). These were getting generated as just the unqualified name, without
the package. If you used a previous version of Go, it meant that if that
aliased type was in an internal package we'd try to import the internal
package instead, resulting in a compilation error.

Add a switch to handle that case, and bump the go.mod version to support
using types.Alias.
  • Loading branch information
reillywatson authored Aug 24, 2024
1 parent efa43c6 commit 5dfcd56
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/matryer/moq

go 1.22.5
go 1.23

require (
github.com/pmezard/go-difflib v1.0.0
Expand Down
12 changes: 12 additions & 0 deletions internal/registry/method_scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ func (m MethodScope) populateImports(t types.Type, imports map[string]*Package)
}
}

case *types.Alias:
if pkg := t.Obj().Pkg(); pkg != nil {
imports[stripVendorPath(pkg.Path())] = m.registry.AddImport(pkg)
}
// The imports of a Type with a TypeList must be added to the imports list
// For example: Foo[otherpackage.Bar] , must have otherpackage imported
if targs := t.TypeArgs(); targs != nil {
for i := 0; i < targs.Len(); i++ {
m.populateImports(targs.At(i), imports)
}
}

case *types.Array:
m.populateImports(t.Elem(), imports)

Expand Down
6 changes: 6 additions & 0 deletions pkg/moq/moq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,12 @@ func TestMockGolden(t *testing.T) {
interfaces: []string{"Magician"},
goldenFile: filepath.Join("testpackages/rangenum", "rangenum_moq.golden.go"),
},
{
name: "TypeAlias",
cfg: Config{SrcDir: "testpackages/typealias"},
interfaces: []string{"Example"},
goldenFile: filepath.Join("testpackages/typealias", "typealias_moq.golden.go"),
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
Expand Down
9 changes: 9 additions & 0 deletions pkg/moq/testpackages/typealias/typealias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package typealias

import (
"github.com/matryer/moq/pkg/moq/testpackages/typealiastwo"
)

type Example interface {

Check failure on line 7 in pkg/moq/testpackages/typealias/typealias.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, oldstable)

exported type Example should have comment or be unexported

Check failure on line 7 in pkg/moq/testpackages/typealias/typealias.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, stable)

exported type Example should have comment or be unexported

Check failure on line 7 in pkg/moq/testpackages/typealias/typealias.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, oldstable)

exported type Example should have comment or be unexported

Check failure on line 7 in pkg/moq/testpackages/typealias/typealias.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, stable)

exported type Example should have comment or be unexported
Do(a typealiastwo.AliasType, b typealiastwo.GenericAliasType) error
}
81 changes: 81 additions & 0 deletions pkg/moq/testpackages/typealias/typealias_moq.golden.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package typealiasinternal

// we shouldn't be able to import these types directly, you need to use the alias in the parent package

Check failure on line 3 in pkg/moq/testpackages/typealiastwo/internal/typealiasinternal/typealiasinternal.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, oldstable)

comment on exported type MyInternalType should be of the form "MyInternalType ..." (with optional leading article)

Check failure on line 3 in pkg/moq/testpackages/typealiastwo/internal/typealiasinternal/typealiasinternal.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, stable)

comment on exported type MyInternalType should be of the form "MyInternalType ..." (with optional leading article)

Check failure on line 3 in pkg/moq/testpackages/typealiastwo/internal/typealiasinternal/typealiasinternal.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, oldstable)

comment on exported type MyInternalType should be of the form "MyInternalType ..." (with optional leading article)

Check failure on line 3 in pkg/moq/testpackages/typealiastwo/internal/typealiasinternal/typealiasinternal.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, stable)

comment on exported type MyInternalType should be of the form "MyInternalType ..." (with optional leading article)
type MyInternalType struct {
Foo int
}

type MyGenericType[T any] struct {

Check failure on line 8 in pkg/moq/testpackages/typealiastwo/internal/typealiasinternal/typealiasinternal.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, oldstable)

exported type MyGenericType should have comment or be unexported

Check failure on line 8 in pkg/moq/testpackages/typealiastwo/internal/typealiasinternal/typealiasinternal.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, stable)

exported type MyGenericType should have comment or be unexported

Check failure on line 8 in pkg/moq/testpackages/typealiastwo/internal/typealiasinternal/typealiasinternal.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, oldstable)

exported type MyGenericType should have comment or be unexported

Check failure on line 8 in pkg/moq/testpackages/typealiastwo/internal/typealiasinternal/typealiasinternal.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, stable)

exported type MyGenericType should have comment or be unexported
A T
}
7 changes: 7 additions & 0 deletions pkg/moq/testpackages/typealiastwo/typealiastwo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package typealiastwo

import "github.com/matryer/moq/pkg/moq/testpackages/typealiastwo/internal/typealiasinternal"

type AliasType = typealiasinternal.MyInternalType

Check failure on line 5 in pkg/moq/testpackages/typealiastwo/typealiastwo.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, oldstable)

exported type AliasType should have comment or be unexported

Check failure on line 5 in pkg/moq/testpackages/typealiastwo/typealiastwo.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, stable)

exported type AliasType should have comment or be unexported

Check failure on line 5 in pkg/moq/testpackages/typealiastwo/typealiastwo.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, oldstable)

exported type AliasType should have comment or be unexported

Check failure on line 5 in pkg/moq/testpackages/typealiastwo/typealiastwo.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, stable)

exported type AliasType should have comment or be unexported

type GenericAliasType = typealiasinternal.MyGenericType[int]

Check failure on line 7 in pkg/moq/testpackages/typealiastwo/typealiastwo.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, oldstable)

exported type GenericAliasType should have comment or be unexported

Check failure on line 7 in pkg/moq/testpackages/typealiastwo/typealiastwo.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, stable)

exported type GenericAliasType should have comment or be unexported

Check failure on line 7 in pkg/moq/testpackages/typealiastwo/typealiastwo.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, oldstable)

exported type GenericAliasType should have comment or be unexported

Check failure on line 7 in pkg/moq/testpackages/typealiastwo/typealiastwo.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, stable)

exported type GenericAliasType should have comment or be unexported

0 comments on commit 5dfcd56

Please sign in to comment.