-
Notifications
You must be signed in to change notification settings - Fork 88
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
Field presence #247
Field presence #247
Conversation
37a0a16
to
cae3446
Compare
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.
Nice work!
I only reviewed the generated code as I don't have any knowledge on how the parsing system works.
impl<'a> Default for FooMessage<'a> { | ||
fn default() -> Self { | ||
Self { | ||
f_int32: None, | ||
f_int64: None, | ||
f_uint32: None, | ||
f_uint64: None, | ||
f_sint32: None, | ||
f_sint64: None, | ||
f_bool: None, | ||
f_FooEnum: None, | ||
f_fixed64: None, | ||
f_sfixed64: None, | ||
f_fixed32: None, | ||
f_sfixed32: None, | ||
f_double: None, | ||
f_float: None, | ||
f_bytes: None, | ||
f_string: None, | ||
f_self_message: None, | ||
f_bar_message: None, | ||
f_repeated_int32: Vec::new(), | ||
f_repeated_packed_int32: Vec::new(), | ||
f_repeated_packed_float: PackedFixed::from(Vec::new()), | ||
f_imported: None, | ||
f_baz: None, | ||
f_nested: None, | ||
f_nested_enum: None, | ||
f_map: HashMap::new(), | ||
test_oneof: mod_FooMessage::OneOftest_oneof::None, | ||
} | ||
} | ||
} |
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 this one not derived?
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.
Oversight in the impl Default
system; have fixed it😅 thanks for catching
w.write_packed_fixed_with_tag(18, &self.nums)?; | ||
w.write_with_tag(26, |w| w.write_message(&self.message))?; | ||
for s in &self.messages { w.write_with_tag(34, |w| w.write_message(s))?; } |
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.
Unnecessary indentation?
w.write_packed_with_tag(154, &self.f_repeated_int32, |w, &m| w.write_int32(*&m), &|&m| sizeof_varint((m) as u64))?; | ||
w.write_packed_with_tag(162, &self.f_repeated_packed_int32, |w, &m| w.write_int32(*&m), &|&m| sizeof_varint((m) as u64))?; | ||
w.write_packed_fixed_with_tag(170, &self.f_repeated_packed_float)?; |
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.
Here too? Can we avoid this indentation?
assert_eq!(b"cde\n33".to_vec(), d.bytes_field.to_vec()); | ||
assert!(EnumForDefaultValue::TWO.eq(&d.enum_field)); | ||
let d = TestDefaultValuesOptional::from_reader(&mut reader, bytes).unwrap(); | ||
assert_eq!(1.0, d.double_field.unwrap_or(TestDefaultValuesOptional::DEFAULT_double_field)); |
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.
Shouldn't this assert be that it is None
and a separate assert that DEFAULT_double_field == 1.0
?
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.
Yep, I think I originally wanted to test exactly what the user would write (i.e. obj.field.unwrap_or(Class::DEFAULT_field)
) so I just wrote it in verbatim
Have changed it though since your version makes more sense haha
Made the changes as per above, also removed some extra parentheses in the generated Edit: cleaning up diff |
I've spent a few hours trying to get the diff as small as possible for the If you're curious, the previous code had control flow in various parts that determine when to add a dereference into the code, which had built up over time into generating a bunch of Hopefully it's ok 🙏 |
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.
LGTM! (Only reviewed generated code)
I'll ping a PR on our repo that needs these changes and have them try them out!
Yeah that is fine. There is a means to an end when it comes to reducing diff :) |
libp2p/rust-libp2p#3455 uses this branch and it works well, green light from our end! |
Hi @snproj! Can you do a version bump in the near future to include this change? Thanks 🙏 |
Friendly ping. Could you cut a new release with this patch? |
Continuation of #243
Since I no longer have access to the
ghpr-asia
fork that I made that PR with.impl
s should now only be generated when there are custom default fields--generate-getters
to generate getters for Proto2 fields with custom defaultsMinor notes:
const
fields from user input.#[derive(Debug, Default, PartialEq, Clone)]
to#[derive(Default, Debug, PartialEq, Clone)]
is there becauseDefault
is now being inserted programmatically into the list of derives, so it was best to simply append to the startFuture action
0
required
fields