uber-go / uber-go/mock

archive mode does not support type aliases

Open
#263 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Go
Stars
3.4k
Forks
177
PR merge metrics
No merged PRs in 30d

Description

Actual behavior

type MyThing = string

hits can't yet turn ... into a model.Type

Expected behavior

It should mock the interface

To Reproduce

type MyThing = string, then add it to an interface that's mocked in archive mode (think Bazel)

Additional Information

HEAD of everything.

Handling this is implemented in package_mode.go, but not in archive.go. The code at https://github.com/uber-go/mock/blob/main/mockgen/model/model_gotypes.go#L77 handles types.Named but not types.Alias. Should it do both?

diff --git a/mockgen/model/model_gotypes.go b/mockgen/model/model_gotypes.go
index 4596c3d..2703c9b 100644
--- a/mockgen/model/model_gotypes.go
+++ b/mockgen/model/model_gotypes.go
@@ -85,6 +85,17 @@ func typeFromGoTypesType(t types.Type) (Type, error) {
                }, nil
        }

+       if t, ok := t.(*types.Alias); ok {
+               tn := t.Obj()
+               if tn.Pkg() == nil {
+                       return PredeclaredType(tn.Name()), nil
+               }
+               return &NamedType{
+                       Package: tn.Pkg().Path(),
+                       Type:    tn.Name(),
+               }, nil
+       }
+
        // only unnamed or predeclared types after here

        // Lots of types have element types. Let's do the parsing and error checking for all of them.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with mockgen/model/model_gotypes.go, especially typeFromGoTypesType, and compare the existing handling there with archive.go and package_mode.go. Reproduce the type alias in an interface mocked in archive mode, then verify that archive mode accepts it and generates the mock instead of reporting that it cannot convert the type.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
testing
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.