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

feat: support generic interface generation #175

Merged
merged 1 commit into from
Oct 1, 2022
Merged

Conversation

cgorenflo
Copy link
Contributor

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:

var _ GenericInterface[string] = GenericInterfaceMock[string]

Consider this type parameter:

type Constraint interface{
   ~int32 | string
   DoSomething() error
}

It is not even guaranteed that an implementation exists, so templating cannot simply pick one. These are the options to handle this case:

  1. Fail to generate such a constraint correctly
  2. Remove the check against the interface in this case
  3. Generate a mock for the Constraint interface

I 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.

Allow the generation of mocks for generics as introduced in golang 1.18
@cgorenflo
Copy link
Contributor Author

@matryer let me know how I can help to get this change merged!

Copy link
Owner

@matryer matryer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 thanks @cgorenflo - sorry for the delay

@matryer matryer merged commit 13aa048 into matryer:main Oct 1, 2022
@sudo-suhas
Copy link
Collaborator

Not sure why the build did not run here but this broke support for Go versions older than 1.18. We should indicate the same when we release it @matryer

@sudo-suhas
Copy link
Collaborator

sudo-suhas commented Nov 20, 2022

Released in v0.3.0 🎉

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

Successfully merging this pull request may close these issues.

None yet

3 participants