Skip to content

Commit

Permalink
Fix bug with type aliases to structs
Browse files Browse the repository at this point in the history
@max-melentyev found in #843 that type aliases to structs were being
rendered as a struct literal. The problem was that we need the type of the
right hand side of the alias definition, not the underlying type itself.
  • Loading branch information
LandonTClipp committed Nov 26, 2024
1 parent 55e1e90 commit 3cef7fd
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,10 @@ packages:
mockname: InterfaceWithUnresolvedAlias
- resolve-type-alias: True
mockname: InterfaceWithResolvedAlias
Interface2:
configs:
- resolve-type-alias: False
mockname: Interface2WithUnresolvedAlias
- resolve-type-alias: True
mockname: Interface2WithResolvedAlias

7 changes: 7 additions & 0 deletions pkg/fixtures/type_alias/interface.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package type_alias

import "github.com/vektra/mockery/v2/pkg/fixtures/type_alias/subpkg"

type Type = int
type S = subpkg.S

type Interface1 interface {
Foo() Type
}

type Interface2 interface {
F(Type, S, subpkg.S)
}
5 changes: 5 additions & 0 deletions pkg/fixtures/type_alias/interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ func TestTypeAlias(t *testing.T) {
filepath: "./mock_InterfaceWithUnresolvedAlias_test.go",
expectedRegex: `func \((_?[a-zA-Z]*)+ \*InterfaceWithUnresolvedAlias\) Foo\(\) type_alias.Type {`,
},
{
name: "Alias to type with underlying struct with resolve-type-alias: True",
filepath: "./mock_Interface2WithResolvedAlias_test.go",
expectedRegex: `func \(_m \*Interface2WithResolvedAlias\) F\(_a0 int, _a1 subpkg.S, _a2 subpkg.S\) {`,
},
} {
t.Run(tt.name, func(t *testing.T) {
regex, err := regexp.Compile(tt.expectedRegex)
Expand Down
70 changes: 70 additions & 0 deletions pkg/fixtures/type_alias/mock_Interface2WithResolvedAlias_test.go

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

71 changes: 71 additions & 0 deletions pkg/fixtures/type_alias/mock_Interface2WithUnresolvedAlias_test.go

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

5 changes: 5 additions & 0 deletions pkg/fixtures/type_alias/subpkg/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package subpkg

type S struct {
A int
}
3 changes: 1 addition & 2 deletions pkg/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,8 +547,7 @@ func (g *Generator) renderType(ctx context.Context, typ types.Type) string {
"url": logging.DocsURL("/deprecations/#resolve-type-alias"),
},
)
log.Debug().Msg("resolving type alias to underlying type")
return g.renderType(ctx, t.Underlying())
return g.renderType(ctx, t.Rhs())
}
log.Debug().Msg("not resolving type alias to underlying type")
return g.renderNamedType(ctx, t)
Expand Down

0 comments on commit 3cef7fd

Please sign in to comment.