Skip to content

Commit a024924

Browse files
committed
support imported type aliases
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 a024924

File tree

6 files changed

+49
-1
lines changed

6 files changed

+49
-1
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
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

Lines changed: 12 additions & 0 deletions
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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,22 @@ func TestImportedPackageWithSameName(t *testing.T) {
664664
}
665665
}
666666

667+
func TestImportedPackageWithTypeAlias(t *testing.T) {
668+
m, err := New(Config{SrcDir: "testpackages/typealias"})
669+
if err != nil {
670+
t.Fatalf("moq.New: %s", err)
671+
}
672+
var buf bytes.Buffer
673+
err = m.Mock(&buf, "Example")
674+
if err != nil {
675+
t.Errorf("mock error: %s", err)
676+
}
677+
s := buf.String()
678+
if !strings.Contains(s, `a typealiastwo.AliasType`) {
679+
t.Error("missing typealiastwo.AliasType")
680+
}
681+
}
682+
667683
func TestParseError(t *testing.T) {
668684
_, err := New(Config{SrcDir: "testpackages/_parseerror/service"})
669685
if err == nil {
Lines changed: 9 additions & 0 deletions
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) error
9+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package typealiasinternal
2+
3+
// we shouldn't be able to import this type directly, you need to use the alias in the parent package
4+
type MyInternalType struct {
5+
Foo int
6+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package typealiastwo
2+
3+
import "github.com/matryer/moq/pkg/moq/testpackages/typealiastwo/internal/typealiasinternal"
4+
5+
type AliasType = typealiasinternal.MyInternalType

0 commit comments

Comments
 (0)