-
Notifications
You must be signed in to change notification settings - Fork 23
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
Getting vs. taking unset fields - inconsistent behavior #61
Comments
The second example also panics if the field is explicitly set to the default value. That is, the following snippet panics as well: let desc = DescriptorPool::global()
.get_message_by_name("google.protobuf.BoolValue")
.unwrap();
let mut msg = DynamicMessage::new(desc);
msg.set_field_by_number(1, Value::Bool(false));
let value = msg.take_field_by_number(1).unwrap();
println!("{value:?}") |
I've always been slightly uneasy about the way the I'd be open to having a Its possible to emulate the behaviour of let desc = DescriptorPool::global()
.get_message_by_name("google.protobuf.BoolValue")
.unwrap();
let field = desc.get_field(1).unwrap();
let mut msg = DynamicMessage::new(desc);
msg.set_field(&field, Value::Bool(false));
// No panic
let value = msg.take_field_by_number(1).unwrap_or_else(|| Value::default_value_for_field(&field));
println!("{value:?}") |
I see. It just felt counterintuitive to me that functions with very similar names have completely different semantics (when it comes to default values). It also is counterintuitive that asking for a value which I just set might result in
I thought about this for a while now. I like the idea to have (I might actually need these functions to imitate the semantics of |
One idea I was toying with was an "entry-style" API for fields, e.g.
It would reduce some of the combinatorial explosion of different methods, and there's less chance for silent breakage like with changing the semantics of I'm not convinced by the skip_default/preserve_default APIs - I think treating default fields as equivalent to unset matches the proto spec most closely |
When calling
get_field_by_number
on an unset scalar field, the default value is returned. When callingtake_field_by_number
, nothing is returned instead. (Tested on current version 0.11.4) More precisely:This snippet prints
Bool(false)
.This snippet panics.
The text was updated successfully, but these errors were encountered: