feat: support generic interface generation #175
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This addresses issue #173 regarding mock generation for interfaces with generic types. It works (almost) with the full scope of what is possible to define with golang 1.18. The test file shows the usage of interfaces with multiple type parameters,
any
constraints, constraints with embedded interface and basic type constraints, as well as aliasing of a generic interface.Limitation:
An Interface with a generic type that has both a type constraint and additional interface functions defined cannot be generated accurately. In order to ensure the correct interface implementation, the mock needs to be cast to the interface with a specific type parameter. Example:
Consider this type parameter:
It is not even guaranteed that an implementation exists, so templating cannot simply pick one. These are the options to handle this case:
Constraint
interfaceI don't think skipping the interface check without the user specifying it would be the right way to go and option 3 is a more involved implementation. This PR should cover the vast majority of use cases, so I chose to not implement this edge case for now and discuss it before going down that route.