-
Notifications
You must be signed in to change notification settings - Fork 121
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
Conversation
* 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
# 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
@JacobOaks |
@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 I think the changes to do this aren't too bad, we would just have a separate case in
What are your thoughts? |
I think, that alias for us is the same thing as a
|
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? |
I don't have a strong opinion on a name. "package mode" or "types mode" is fine. |
* tests * help message * README.md
I choose package mode. Check last commit changes please: 69210e9 If everything is OK, i'm waiting for a merge 🙂 |
I realized that I had made a mistake. @JacobOaks What do you think about this? //UPD: flags cannot be removed from help, so i added "DEPRECATED" to them usages |
Hey @tulzke, for For build flags, what do you think about maintaining backwards compatibility and passing them to |
@JacobOaks |
README.md
Outdated
- `-build_flags`: (reflect mode only) Flags passed verbatim to `go build`. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we keep these lines now since we are keeping the flag?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, i forgot to do this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @tulzke for all your hard work on this PR! LGTM.
Hi @JacobOaks |
… `--exec_only` Change uber-go#207 replaced reflect mode with package mode. In doing so, we no longer had to create separate reflect programs that would encode the resulting package into a gob for the rest of mockgen to read from. Because of that, we removed the `--exec_only` flag. However, this flag was actually used to pass in programs that created gob encodings in other ways, allowing customization of the package loading and encoding portion of mockgen. As an alternative, this adds a new flag to mockgen `--model_gob`, which allows mockgen users to pass a path to a gob encoding of a `model.Package`, so that custom ways of loading package information can still be supported.
… `--exec_only` (#214) Change #207 replaced reflect mode with package mode. In doing so, we no longer had to create separate reflect programs that would encode the resulting package into a gob for the rest of mockgen to read from. Because of that, we removed the `--exec_only` flag. However, this flag was actually used to pass in programs that created gob encodings in other ways, allowing customization of the package loading and encoding portion of mockgen. As an alternative, this adds a new flag to mockgen `--model_gob`, which allows mockgen users to pass a path to a gob encoding of a `model.Package`, so that custom programs for encoding package information can still be used in conjunction with mockgen.
…eting-plugin-hetzner!135) This MR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [go.uber.org/mock](https://github.com/uber/mock) | require | minor | `v0.4.0` -> `v0.5.0` | --- ### Release Notes <details> <summary>uber/mock (go.uber.org/mock)</summary> ### [`v0.5.0`](https://github.com/uber-go/mock/releases/tag/v0.5.0) [Compare Source](uber-go/mock@v0.4.0...v0.5.0) #### 0.5.0 (15 Oct 2024) ##### Added - [#​153][]: Add `--write_command_comment` flag to specify whether to include `Generated by this command` comment. - [#​191][]: Add `--build_constraint` flag to add `//go:build` directives to generated mocks - [#​214][]: Add gob mode to support custom package loading techniques in place of --exec_only ##### Changed - [#​181][]: Made mockgen faster by changing flags passed to `go list`. - [#​183][]: Made `Cond` matcher generic. - [#​204][]: Removed `ISGOMOCK()` from generated mocks. - [#​207][]: Deprecated reflect mode and replaced it with the new package mode. ##### Fixed - [#​144][]: Fix a deadlock that can happen when mocking an interface that matches `fmt.Stringer`. - [#​168][]: Fix an issue where the "generated by" comment was being included in the package comment of generated mocks. [#​144]: uber-go/mock#144 [#​153]: uber-go/mock#153 [#​168]: uber-go/mock#168 [#​181]: uber-go/mock#181 [#​183]: uber-go/mock#183 [#​191]: uber-go/mock#191 [#​204]: uber-go/mock#204 [#​207]: uber-go/mock#207 [#​214]: uber-go/mock#214 Thanks to [@​tulzke](https://github.com/tulzke) [@​JacobOaks](https://github.com/JacobOaks) [@​ARR4N](https://github.com/ARR4N) [@​sashamelentyev](https://github.com/sashamelentyev) [@​sywhang](https://github.com/sywhang) [@​fasmat](https://github.com/fasmat) [@​eyasy1217](https://github.com/eyasy1217) [@​ghouscht](https://github.com/ghouscht) [@​tie](https://github.com/tie) [@​Neo2308](https://github.com/Neo2308) [@​carson-brill](https://github.com/carson-brill) [@​alexandear](https://github.com/alexandear) [@​sodul](https://github.com/sodul) [@​nbgraham](https://github.com/nbgraham) for their contributions this release. </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever MR is behind base branch, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this MR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box --- This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOC4xMjcuMCIsInVwZGF0ZWRJblZlciI6IjM4LjEyNy4wIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->
v0.5.0 included uber-go#207, which replaced reflect mode with package mode. One issue with package mode that came up (ref: uber-go#216) was that generated mocks for interfaces that referred to alias types were referring to the aliases' underlying names instead. e.g., source: ```go import "github.com/tikv/client-go/v2/internal/apicodec" ... type Codec = apicodec.Codec type Foo interface{ Bar() Codec } ``` mock: ```go func (m *MockFoo) Bar() apicodec.Codec { // This is a problem, since apicodec is an internal package. // ... } ``` While technically this problem is solved in Go 1.23 with explicit alias types representation, (indeed, if you run mockgen on the example in the linked issue with `GODEBUG=gotypesalias=1`, you get the expected behavior) since we support the last two versions, we can't bump `go.mod` to 1.23 yet. This leaves us with the old behavior, where `go/types` does not track alias types. You can tell if an object is an alias, but not a type itself, and there is no way to retrieve the object of interest at the point where we are recursively parsing method types. This PR works around this issue (temporarily) by using syntax information to find all references to aliases in the source package. When we find one, we record it in a mapping of underlying type -> alias name. Later, while we parse the type tree, we replace any underlying types in the mapping with their alias names. The unexpected side effect of this is that _all_ references to the underlying type in the generated mocks will be replaced with the alias, even if the source used the underlying name. This is fine because: * If the alias is in the mapping, it was used at least once, which means its accessible. * From a type-checking perspective, aliases and their underlying types are equivalent. With this PR, the mocks get generated correctly now: ```go func (m *MockFoo) Bar() Codec { // ... } ``` Once we can bump `go.mod` to 1.23, we should definitely remove this, since the new type alias type nodes solve this problem automatically.
v0.5.0 included #207, which replaced reflect mode with package mode. One issue with package mode that came up (ref: #216) was that generated mocks for interfaces that referred to alias types were referring to the aliases' underlying names instead. e.g., some package: ```go package somgpkg import "somepkg/internal/apicodec" ... type Codec = apicodec.Codec ``` mockgen input: ```go type Foo interface{ Bar() somepkg.Codec } ``` mock: ```go func (m *MockFoo) Bar() apicodec.Codec { // This is a problem, since apicodec is an internal package. // ... } ``` While technically this problem is solved in Go 1.23 with explicit alias types representation, (indeed, if you run mockgen on the example in the linked issue with `GODEBUG=gotypesalias=1`, you get the expected behavior) since we support the last two versions, we can't bump `go.mod` to 1.23 yet. This leaves us with the old behavior, where `go/types` does not track alias types. You can tell if an object is an alias, but not a type itself, and there is no way to retrieve the object of interest at the point where we are recursively parsing method types. This PR works around this issue (temporarily) by using syntax information to find all references to aliases in the source package. When we find one, we record it in a mapping of underlying type -> alias name. Later, while we parse the type tree, we replace any underlying types in the mapping with their alias names. The unexpected side effect of this is that _all_ references to the underlying type in the generated mocks will be replaced with the alias, even if the source used the underlying name. This is fine because: * If the alias is in the mapping, it was used at least once, which means its accessible. * From a type-checking perspective, aliases and their underlying types are equivalent. The nice exception to the side effect is when we explicitly request mock generation for an alias type, since at that point we are dealing with the object, not the type. With this PR, the mocks get generated correctly now: ```go func (m *MockFoo) Bar() Codec { // ... } ``` Once we can bump `go.mod` to 1.23, we should definitely remove this, since the new type alias type nodes solve this problem automatically.
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