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

deprecated reflect mode has been replaced with package mode #207

Open
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

tulzke
Copy link
Contributor

@tulzke tulzke commented Sep 27, 2024

re-open of: #198

resolves #175
resolves #197
resolves #128

It is impossible to create an mock for a generic interface via reflect mode, because it is impossible to compile a generic type without instantiation.
This PR replaces the reflect mod for parsing using go/types.

All exists mocks have been regenerated and the tests have been passed. But since this radically changes the behavior of reflect mode, I would be grateful if there are those who want to add additional test cases that I did not provide.

We can also come up with another name instead of import mode.

benefits:
generation mocks for generic interfaces
generation mocks for aliases to interfaces
correct names for method arguments

tulzke and others added 12 commits August 9, 2024 14:34
* generation mocks for generic interfaces
* generation mocks for aliases to interfaces
* correct names for method arguments
* added detailed parsing error
* error more succinct
* removed call of iface.Complete()
* renamed checkInterfaceIsNotConstraint -> isConstraint
* reduced nesting at parse named segment
* add_generate_directive test fixed after I accidentally changed it early
* go updated up to 1.23 for correct aliases support
* loading of package was changed from fake package to direct load by pattern
* types.Alias added to same case as types.Named
* added test case for interface containing aliases in parameters and returns
# Conflicts:
#	mockgen/reflect.go
* changed errors formatting with less details
* regenerated mocks
* fixed type conversion for support go1.22
* simplified loops by using range
* removed unused method "generateSalt"
* renamed method of test interface: GiveBirth -> AddHumans
@tulzke
Copy link
Contributor Author

tulzke commented Sep 27, 2024

@JacobOaks
What do you think about this problem?
#198 (comment)

@JacobOaks
Copy link
Contributor

@tulzke - thanks for re-opening this and apologies again for the git mistakes on the other PR.

I think for now it makes sense to just use the unaliased names until we're able to update go.mod to 1.23. From the perspective of type-checking, types and their aliases are interchangeable so there shouldn't be any missing functionality/bugs introduced by just uniformly using the unaliased names for now. This also keeps the generated code consistent between the two versions. Once 1.24 is out and we can upgrade go.mod to 1.23, we can definitely then switch over to using the alias names.

I think the changes to do this aren't too bad, we would just have a separate case in parseType like so:

case *types.Alias:
    return p.parseType(types.Unalias(t))

What are your thoughts?

@tulzke
Copy link
Contributor Author

tulzke commented Sep 27, 2024

we would just have a separate case

I think, that alias for us is the same thing as a *types.Named. We need only his name and package.

types.Unalias(t) returns original type, so in mocks we will see original types. It is not completely correct behavior.

@tulzke
Copy link
Contributor Author

tulzke commented Sep 27, 2024

So, if we have agreed on the functionality, can we think about the names? What is the name of the new mode to replace reflect?
Maybe Package mode? Source mode and Package mode. That sounds good.

@JacobOaks
Copy link
Contributor

I don't have a strong opinion on a name. "package mode" or "types mode" is fine.

* tests
* help message
* README.md
@tulzke
Copy link
Contributor Author

tulzke commented Sep 27, 2024

I don't have a strong opinion on a name. "package mode" or "types mode" is fine.

I choose package mode. Check last commit changes please: 69210e9

If everything is OK, i'm waiting for a merge 🙂

@tulzke
Copy link
Contributor Author

tulzke commented Sep 27, 2024

I realized that I had made a mistake.
I deleted 3 unused flags, and this may break the existing generation methods. Since now these flags have no effect, I think they can be removed from the readme and help, and when using write a WARNING message to the console.

@JacobOaks What do you think about this?

//UPD: flags cannot be removed from help, so i added "DEPRECATED" to them usages

@tulzke tulzke changed the title deprecated reflect mode has been replaced with import mode deprecated reflect mode has been replaced with package mode Sep 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants