-
Notifications
You must be signed in to change notification settings - Fork 607
gomock generics support enhancement #663
base: main
Are you sure you want to change the base?
gomock generics support enhancement #663
Conversation
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Thanks for improving the support for Generics @bradleygore. I see that the new @codyoss We are eagerly waiting for full support of Go 1.18 in the next release of Thanks! |
@sodul - I went ahead and consolidated down the functionality to retrieve interface model definition (whether imported or same pkg). PR updated. |
Hey @codyoss - anything I can do to help this PR move forward? |
Thanks for the PR I will take a look at this today! |
First off thanks for taking a stab at getting this all implemented. I have not looked too in depth yet but I thought I would provide some early feedback.
Thanks again and I am happy to work with you to push this along. Please let me know if you have any questions. |
Also, I totally agree about how un-approachable some of these files are to edit. In the future I would love to do some internal refactors to improve readability. If you have motivation for that(in a separate PR) we are always accept contributions :) |
Also also just noticed #669 that is doing some of the same work. The impl works for embedding with interfaces where all parameters are type parameters but not when they are instantiated types. |
Hey @codyoss - I will work on this. I noticed the different 1.18 file and honestly didn't even recognize that the |
Hey @codyoss - I went ahead and pushed an update that splits out the generic-specific stuff to the
And both outputted a binary file in the specified location without error. Can you enable the workflow to run on this, or do you spot anything else I need to do first? Update: I also downloaded Update2: Pushed an update that I think solves the issue for parsing in/out params that reference generic types through embedded iface (and even pointers to them - see |
I think everything has been addressed and this is ready to run through CI tests 😄 Is there a way to run the test suite locally? I see in the |
@bradleygore I just did this a few hours ago. Since the code is expected to build with Go 1.15 I did the following from the root of the repository:
The only dependency is for you to have docker running. My laptop is not backward compatible with Go 1.15 (M1 CPU) so the docker approach worked just fine, and at native speed since it used the ARM image. See https://github.com/golang/mock/blob/main/.github/workflows/test.yaml for how it runs the tests. |
Hey @codyoss - I am able to run the CI workflow locally using https://github.com/nektos/act tool. Everything is now passing here, so hoping it will in CI as well 😄 |
Just checking in to see if anything further needed from me on this PR. It doesn't run the workflow without approval for each run it seems. As noted, I did run the workflow locally and everything passed there, so I'm hopeful it will here as well. |
Hey @codyoss - is there anything further needed from me on this? |
@eduardsmetanin I'm not a maintainer, it all in the hands of @codyoss :-). |
Hi, thank you for doing this PR. It's very useful. I was trying it out in my own fork and found something, not sure the root cause of it. Let's say I have an interface like
and I instantiate it in another interface like
The mock file for RealThing interface has correct mock for FindOne method which replaces E with SomeStruct but the FindAll still uses E type in mocks. Is this a real issue or I did something wrong? |
Just getting back to this today sorry for the delay. I am looking over this and #669 as they are both doing roughly the same thing with different impls |
skip rewriting output files if unchanged (golang#626)
@clsx524 - thanks for finding an unhandled case! I reworked the parsing of the generic in/out parameters on methods to be much more complete, and added new test cases for these. Please try again and let me know what you think 😄 |
@bradleygore Thanks for your work! I had a try and your change definitely fixed the issue I mentioned. I noticed it probably has some issue to handle vararg syntax in method like
Option would be some random interface. Could you help double check this scenario? |
@clsx524 - nice find! I just updated it to handle that. I hope that it is handling all of the scenarios now 😅 |
@codyoss - yeah, I didn't realize that someone else was also tackling this. Here are some main differences I can see:
Not saying any of these are right/wrong/better/worse - just in reviewing these PRs these are the core differentiators I see. If the other PR gets merged in, I would still advocate for checking out some of the cleanup I've done in |
Thanks for the update. I can confirm it fixed the issues I mentioned. |
Hey @codyoss can you re-review this one? It's been out here a while, and we need a resolution for this to be able to start using generics. |
Related Issue(s)
The related issues can be found in the
1.7.0 Milestone
doc: https://github.com/golang/mock/milestone/5, specifically issues #643 and #649Goals
Hiccups/Hangups
Navigation and Data Flow
There were some parts that I found challenging to navigate - especially this being my first dive into this codebase. Much respect to the work that's been done - not trying to criticize - but some helpful comments would make it a bit easier for contributors. I tried to add useful comments to what I've added in this PR without being overly verbose - tough to balance b/c some of this stuff is complex.
Build/Test Processes
I didn't see instructions for linting or adding of tests, etc... so I just tried to do what the github workflow does, which is run
./ci/test.sh
.I am unable to get that to run nor am I able to do
go generate ./...
to re-generate all of the mocks in thegomock/internal/tests/...
dirs. I get the following errors (even onmain
branch):Are we supposed to run
go generate ./...
from the root (or anywhere else)?Proving Grounds
Though I had some hangups trying to prove this out, I was able to prove out reasonably enough that this works via the following mechanisms:
Ran against my test repo
I was able to successfully run this against the
CustomWorker
interface in my repo for testing gomock w/ generics: https://github.com/bradleygore/gomock-generics-issue/blob/master/workers/custom.goI did this using a debug entry in VSCode (so I could debug it and troubleshoot as needed) of:
and it successfully generate this file (which looks right to me):
Mock CustomWorker
gomock generics test
I added a new interface to
mockgen/internal/tests/generics/generics.go
of:and used the following VSCode debug entry to execute these updates against it:
and this is the resulting output:
mock_generics_test.go