-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
protoc-gen-go: option to specify additional "used names" for generated messages #1320
Comments
Field name conflict resolution, as currently implemented, was a mistake. It doesn't scale with the addition of new reserved fields, it has many confusing edge cases, and it is not discoverable at all. We don't plan on ever adding new "used names"--to the contrary, we'd like to remove the ones that exist. I don't think we want to add a mechanism for extending it. |
Sure @neild, I had figured as much after reading the comment I linked + others that cite historic reasons for certain behavior. Question though, can you explain what you mean by discoverable?
I don't think it's particularly relevant, but the above leaves me with the impression that reserved "used names" (with names that aren't dynamically generated / need to remain fixed) would have been better grouped behind a single method, returning a type that could be extended later. The main reason I've requested this specific solution is because I couldn't think of anything else that wouldn't potentially introduce breaking changes to existing users, since it's clear that modifying any of the "used names" has the potential to break usage of existing generated code. An option to add additional "used names" is the sort of thing I'd expect to be at least deprecated, if not outright removed, if the way that the Go names where generated was changed. This is predicated on the idea that it's necessary to generate additional methods to achieve my goals, so I'll try explain those in more detail below, but feel free to ignore it. I will no doubt struggle to be succinct. Broadly speaking, my goal is to generate code, to perform operations with behavior that is message specific. To do this, there are two problems I need to solve:
// ShallowClone returns a shallow copy of the receiver or nil if it's nil.
func (x *User) ShallowClone() (c *User) The above is an example of one of the methods I am currently generating. I've added the I've considered other solutions to support this use case, but they all have what I'd consider to be significant caveats. The closest to palatable would be something that behaves like |
We would like to one day remove the stateful name derivation logic as it is subtle and breaks the property where re-arranging fields in a protobuf message should be a safe operation. How we get to a world where we no longer have the "used names" logic is still to be determined, but you can imagine that it go something like:
Given that our intention is to hopefully one day remove the "used names" logic, I don't think we will accept a feature that allows injecting names into that logic. I'm inclined to close this issue in favor of the pre-existing #555 issue. |
Thanks for the link @dsnet. Your proposal seems sensible to me, and is pretty much what I expected. It was made quite a long time ago, has it gained any momentum, recently? I'd be curious to hear your thoughts on how naming collisions might be resolved, in cases where it might be problematic to have the plugin generating the message (e.g. To be clear, I wouldn't expect The hypothetical problem case I am imagining is one where source is generated for |
Yes, that would have been a better way to go. You can see this in the new There are other approaches that could have worked as well: Generate field names with a consistent prefix (e.g., It would be nice to have the chance to design the generated API afresh, but the cost of incompatibility is high. If you're adding methods to generated types and want to avoid conflicts, I'd suggest including an underscore in the method names, since no generated field or method ever contains an underscore. For example, |
That's... really good to know, thanks @neild. Since I'm only concerned with method name collisions, I think that resolves all of my concerns with a very simple change. If it's not already, documenting it somewhere and making it as obvious as possible (as part of a sort of "plugin 101", in the protogen package comment, maybe?) would probably be a good idea. I'll close this issue then, thank you both for your help. |
I've implemented a plugin to generate
ShallowCopy
andShallowClone
methods for all of my messages. While I deliberately picked names that are unlikely to collide, I'd have preferred to useCopy
andClone
, for example. I'd also like to be able to avoid naming collisions, in general, in order to address a wider range of use cases.In consideration of the risks outlined in the implementation, I would like to propose an "opt-in" mechanism, whereby additional "used names" may be specified, via an additional plugin option.
The text was updated successfully, but these errors were encountered: