Skip to content

Commit 5dfcd56

Browse files
authored
Use Go 1.23, support imported type aliases (#225)
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/[email protected]/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.
1 parent efa43c6 commit 5dfcd56

File tree

7 files changed

+126
-1
lines changed

7 files changed

+126
-1
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/matryer/moq
22

3-
go 1.22.5
3+
go 1.23
44

55
require (
66
github.com/pmezard/go-difflib v1.0.0

internal/registry/method_scope.go

+12
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,18 @@ func (m MethodScope) populateImports(t types.Type, imports map[string]*Package)
9090
}
9191
}
9292

93+
case *types.Alias:
94+
if pkg := t.Obj().Pkg(); pkg != nil {
95+
imports[stripVendorPath(pkg.Path())] = m.registry.AddImport(pkg)
96+
}
97+
// The imports of a Type with a TypeList must be added to the imports list
98+
// For example: Foo[otherpackage.Bar] , must have otherpackage imported
99+
if targs := t.TypeArgs(); targs != nil {
100+
for i := 0; i < targs.Len(); i++ {
101+
m.populateImports(targs.At(i), imports)
102+
}
103+
}
104+
93105
case *types.Array:
94106
m.populateImports(t.Elem(), imports)
95107

pkg/moq/moq_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,12 @@ func TestMockGolden(t *testing.T) {
412412
interfaces: []string{"Magician"},
413413
goldenFile: filepath.Join("testpackages/rangenum", "rangenum_moq.golden.go"),
414414
},
415+
{
416+
name: "TypeAlias",
417+
cfg: Config{SrcDir: "testpackages/typealias"},
418+
interfaces: []string{"Example"},
419+
goldenFile: filepath.Join("testpackages/typealias", "typealias_moq.golden.go"),
420+
},
415421
}
416422
for _, tc := range cases {
417423
t.Run(tc.name, func(t *testing.T) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package typealias
2+
3+
import (
4+
"github.com/matryer/moq/pkg/moq/testpackages/typealiastwo"
5+
)
6+
7+
type Example interface {
8+
Do(a typealiastwo.AliasType, b typealiastwo.GenericAliasType) error
9+
}

pkg/moq/testpackages/typealias/typealias_moq.golden.go

+81
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package typealiasinternal
2+
3+
// we shouldn't be able to import these types directly, you need to use the alias in the parent package
4+
type MyInternalType struct {
5+
Foo int
6+
}
7+
8+
type MyGenericType[T any] struct {
9+
A T
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package typealiastwo
2+
3+
import "github.com/matryer/moq/pkg/moq/testpackages/typealiastwo/internal/typealiasinternal"
4+
5+
type AliasType = typealiasinternal.MyInternalType
6+
7+
type GenericAliasType = typealiasinternal.MyGenericType[int]

0 commit comments

Comments
 (0)