You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 variableret1, _:=ret[1].(errors.DetailedError)
returnret0, 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:
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: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:
In the generated code:
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:The generated imports:
The generated code:
The text was updated successfully, but these errors were encountered: