Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add An -imports_overrides Flag, To Explicitly Override Imports #227

Open
dan-lugg-lightspeed opened this issue Dec 10, 2024 · 0 comments
Open

Comments

@dan-lugg-lightspeed
Copy link

dan-lugg-lightspeed commented Dec 10, 2024

Requested feature

As per a closed issue (golang/mock#436) from the archived golang/mock repository, it seems the -imports flag should/would alias the provided imports in the generated code. It was indicated that that is not the intent of this flag, from @codyoss:

The imports flag is meant to help generate mocks and not rename imports. It should only be used in cases where mockgen can not detect that a required import is need.

It would, however, be handy if the capability discussed in that issue were available, and could be exposed through a new flag, named -imports_overrides, or similar.

Why the feature is needed

The specific use-case for this capability (which brought me to the original issue) is that in some circumstances, packages are imported in such a way that there's ambiguity between generated variable fields and types in assertions. For example:

  • In the generated imports:

    import (
        item "github.com/my/project/item"
    )
  • In the generated code:

    func (m *MockSomethingInterface) DoSomething(item item.Item) (item.Item, errors.DetailedError) {
        m.ctrl.T.Helper()
        ret := m.ctrl.Call(m, "DoSomething", item)
        ret0, _ := ret[0].(item.Item) // <-- The issue is here; item is both a package and generated variable
        ret1, _ := ret[1].(errors.DetailedError)
        return ret0, ret1
    }

This can be solved with rename/refactoring of the project, such that as in the above example the package name is items (or anything else), however this is not always feasible in inherited projects.

Proposed solution

The proposed solution is the addition of an -imports_overrides flag, that takes the same value as the current -imports flag. During code generation, if a package is matched in the overrides map, it is aliased with the provided alias, and that alias is used in the generated code throughout. For example, using the above snippet:

  • The issued mockgen command:

    $ mockgen -source=something.go -imports_overrides item_override=github.com/my/project/item
  • The generated imports:

    import (
        item_override "github.com/my/project/item"
    )
  • The generated code:

    func (m *MockSomethingInterface) DoSomething(item item_override.Item) (item_override.Item, errors.DetailedError) {
        m.ctrl.T.Helper()
        ret := m.ctrl.Call(m, "DoSomething", item)
        ret0, _ := ret[0].(item_override.Item) // <-- The issue is resolved
        ret1, _ := ret[1].(errors.DetailedError)
        return ret0, ret1
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant