-
Notifications
You must be signed in to change notification settings - Fork 508
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
getter/setter generated for optional enum fields should use Option #1027
Comments
I agree. Are you willing to do the work? |
@caspermeijn I have the same issues, which make me have to use a trick to convert from Option. I checked the source code and found that it has some strange behavior in https://github.com/tokio-rs/prost/blob/baddf9828596bb7e2b908f5d4997542ebe59a7c3/prost-derive/src/field/scalar.rs#L308-326 and https://github.com/tokio-rs/prost/blob/baddf9828596bb7e2b908f5d4997542ebe59a7c3/prost-derive/src/field/scalar.rs#L354-377 It returns a default value with Kind::Optional. I think that with Kind::Optional, we should return None if it's not set. For example, with pub struct FieldDescriptorProto {
#[prost(string, optional, tag = "1")]
pub name: ::core::option::Option<::prost::alloc::string::String>, Instead of checking if name is Some or None, some other code uses a function from a code macro like: impl OneofField {
fn new(descriptor: OneofDescriptorProto, fields: Vec<Field>, path_index: i32) -> Self {
Self {
descriptor,
fields,
path_index,
}
}
fn rust_name(&self) -> String {
to_snake(self.descriptor.name())
}
} I think we should have another function if we need to get name with a fallback to a default value, like Ps: I just implemented the patch in my fork. Can you take a look? |
Would this PR also address your problem? #1079 It changes the field type to |
#1079 does not address this issue, as I did not change the behavior of prost-derive in other aspects than the value type of the enum fields. |
Currently if you have an enum field marked as
optional
,prost
wraps thei32
field in anOption
as with other scalar values. However, the generated getter in this case still returnsMyEnum
instead ofOption<MyEnum>
, silently converting aNone
into the default value. This burned me recently, and it's also inconsistent with the fact that thei32
fields are wrapped inOption
.The text was updated successfully, but these errors were encountered: