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
Generated enum for oneof message inconsistent with Style Guide #466
Comments
This is frustrating for those of us that implemented unions per the previous recommended technique and style guide: |
@anandolee , do you know why we didn't use CAPITALS_WITH_UNDERSCORES for oneof enum names? |
One option would be to modify the oneof_decl loop in cpp_message.cc
to simply duplicate this printer->Print call for each oneof_decl, but use a ToUpperCase instead of UnderscoresToCamelCase. So the resulting enum would look like
|
Not sure for the reason. Use alias make sense to me 2015-06-05 10:40 GMT-07:00 Feng Xiao notifications@github.com:
|
Ok - to alias the enum's, I simply modified the loop at line 962 of cpp_messages.cc to do a second print, without the "k" in front and
Sound ok? |
I'm curious- will this fix be included in the next protobuf release? |
Sorry for late update. I read the style guide again, macro-style naming is required in .proto file. But the generate code is c++ file which should follow c++ style guide instead of .proto style. For c++ file, enumerators should be named either like constants or like macros. For c++, "the style was to name enum values like macros. This caused problems with name collisions between enum values and macros. Hence, the change to prefer constant-style naming was put in place. New code should prefer constant-style naming if possible. However, there is no reason to change old code to use constant-style names, unless the old names are actually causing a compile-time problem." I think current code which mixed the two style is still not good. However, it makes a little sense to difference set and not set. |
Thank you for the response. I don't totally agree with the distinction that generated C++ code should use kConstantStyle naming, while proto enum names should use MACRO_STYLE. The enums in the proto files are MACRO_STYLE, and result in generated C++ enumerations that are also MACRO_STYLE - they are not converted to google-C++-style-guide kConstantStyle naming. It really doesn't make much sense to have it both ways. |
" I have made my suggested modification in our code " Where did you make the change? On opensource or google internal? @pherl Do you think is it OK to add alias even c++ style prefer to use constant-style to avoid name collisions? |
Work around google proto3 oneof enum naming inconsistency issue. Generate a MACRO_STYLE alias for each oneof enum value, in addition to the kCamelCaseStyle value. For example: oneof oneof_name { int32 foo_int = 4; string foo_string = 9; } Will generate the following: enum OneofNameCase { kFooInt = 4, FOO_INT = 4, kFooString = 9, FOO_STRING = 9, ONEOF_NAME_NOT_SET = 0 }
I just forked and pushed the change: |
Just following up. Didn't see a response from @pherl or @anandolee. |
Hmm, generating two styles seem to diverge the API further. @anandolee could you check how many internal use cases are there? We could probably clean those up and switch to the macro style. |
Will look into it. |
Just looked into internal use cases, where are more than 10k uses already. It will be hard to clean those up and switch to macro style... |
@pherl: why would this diverge the API? the API doesn't change - but the user would have a choice of naming style for oneof C++ generated enum's |
It diverges the style that users would use the API. I'd rather not introduce such variation/inconsistency. |
@pherl: The original point of this issue was that the naming style of the C++ generated oneof enums is inconsistent with everything else in protobuf. |
Having two symbol names for the same value seems problematic to me. Clients could use either of them even in a single project, which hurts the readability. I am fine with going with macro style or const style, however, generating aliasing values that allows two dialects is worse than either of the style. |
I am going to go ahead and close this because we are cleaning up issues and I see that this one has not been updated in a long time. Feel free to reopen it if this is still a problem, though. |
generates the following:
Why does the generated enum for a oneof message break the Style Guide naming rules for enum's?
The enum's should be CAPITALS_WITH_UNDERSCORES. Instead the oneof enum introduces a new kCamelCase style, but isn't even consistent with itself, since it includes ONEOF__NOT_SET.
The result is just a hodgepodge of styles- not very clean.
I guess google can't change this now without breaking current oneof code, but you could modify protoc to generate an additional enum for (or #defines) for each oneof to allow the proper naming style to be used in new code.
The text was updated successfully, but these errors were encountered: