-
Notifications
You must be signed in to change notification settings - Fork 504
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
fix getter/setter generated for optional enum fields should use option #1060
base: master
Are you sure you want to change the base?
Conversation
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.
Why is the name_fallback
getter nessesary? You could do .name().unwrap_or_default()
to get the same result, right? I think that will result in more idiomatic code.
msg.privacy_level_4_fallback(), | ||
default_enum_value::PrivacyLevel::PrivacyLevelprivacyLevelFour |
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.
I would turn this around to simplify the code.
msg.privacy_level_4_fallback(), | |
default_enum_value::PrivacyLevel::PrivacyLevelprivacyLevelFour | |
msg.privacy_level_4(), | |
Some(default_enum_value::PrivacyLevel::PrivacyLevelprivacyLevelFour) |
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.
I would like to see new test cases for an optional protobuf enum field.
match self.#ident { | ||
#match_some | ||
::core::option::Option::None => #default, | ||
} | ||
} | ||
|
||
#[doc=#get_doc] | ||
pub fn #get(&self) -> ::core::option::Option<#ty> { |
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.
I think this should only return an Option<T>
for optional protobuf fields. I think it should remain the original code for non-optional fields
If we use unwrap_or_default, I don't think it can work with some custom default enum like this:
Java implementation use |
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.
This PR should only change the generated code for optional enums. The generated code to non-optional enums should be unchanged.
I think optional enums should have two getters: fn #get(&self) -> Option<#ty>
and fn #get _or_default(&self) -> #ty
. I believe that will cover your usecase and be more correct.
This PR fixed mismatch between getter/setter generated for optional enum fields and enum value i32 as described in #1027