Skip to content
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

Merged
merged 4 commits into from
Mar 20, 2023
Merged

Field presence #247

merged 4 commits into from
Mar 20, 2023

Conversation

snproj
Copy link
Collaborator

@snproj snproj commented Feb 27, 2023

Continuation of #243

Since I no longer have access to the ghpr-asia fork that I made that PR with.

  • Some minor formatting changes rolled back to reduce the diff
  • Custom default impls should now only be generated when there are custom default fields
  • Added --generate-getters to generate getters for Proto2 fields with custom defaults

Minor notes:

  • Forgot to mention in previous PR message, but custom default fields for strings and bytes must now begin and end with either single or double inverted commas (previously not enforced). I had to enforce this to predictably generate default const fields from user input.
  • The minor change from #[derive(Debug, Default, PartialEq, Clone)] to #[derive(Default, Debug, PartialEq, Clone)] is there because Default is now being inserted programmatically into the list of derives, so it was best to simply append to the start

Future action

  • Proto2 and Proto3 parsing will be made stricter; originally this was part of this PR (since the whole parsing system got restructured anyway), but it's probably better to separate it out. The changes are:
    • Proto3 enums must begin with index 0
    • Proto3 cannot have required fields
    • Proto3 cannot have custom defaults

@snproj snproj mentioned this pull request Feb 27, 2023
Copy link

@thomaseizinger thomaseizinger left a 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.

Comment on lines 122 to 154
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,
}
}
}

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?

Copy link
Collaborator Author

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

Comment on lines 123 to 125
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))?; }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary indentation?

Comment on lines 222 to 224
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)?;

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));

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?

Copy link
Collaborator Author

@snproj snproj Mar 5, 2023

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

@snproj
Copy link
Collaborator Author

snproj commented Mar 5, 2023

Made the changes as per above, also removed some extra parentheses in the generated get_size()es that previous versions of this PR added in.

Edit: cleaning up diff

@snproj snproj marked this pull request as draft March 5, 2023 23:46
@snproj snproj marked this pull request as ready for review March 6, 2023 01:56
@snproj
Copy link
Collaborator Author

snproj commented Mar 6, 2023

I've spent a few hours trying to get the diff as small as possible for the get_size() and write_message() parts, but the way that the code generation has changed in this PR means that eliminating all the differences is hard without a bunch of error-prone rewriting (that will eventually be reverted anyway when I make the PR to clean it up, since I already have the code for that). I don't know that it's impossible, but I didn't want to delay too long haha

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 &&*&**&. This is the main source of diff, since I decided to rewrite the control flow to avoid these chains of references and dereferences; which makes it hard to artificially add them back in and shrink the diff.

Hopefully it's ok 🙏

Copy link

@thomaseizinger thomaseizinger left a 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!

@thomaseizinger
Copy link

I've spent a few hours trying to get the diff as small as possible for the get_size() and write_message() parts, but the way that the code generation has changed in this PR means that eliminating all the differences is hard without a bunch of error-prone rewriting (that will eventually be reverted anyway when I make the PR to clean it up, since I already have the code for that). I don't know that it's impossible, but I didn't want to delay too long haha

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 &&*&**&. This is the main source of diff, since I decided to rewrite the control flow to avoid these chains of references and dereferences; which makes it hard to artificially add them back in and shrink the diff.

Hopefully it's ok pray

Yeah that is fine. There is a means to an end when it comes to reducing diff :)

@thomaseizinger
Copy link

libp2p/rust-libp2p#3455 uses this branch and it works well, green light from our end!

@snproj snproj merged commit 2f37d5a into tafia:master Mar 20, 2023
@d-roak
Copy link

d-roak commented May 1, 2023

Hi @snproj!

Can you do a version bump in the near future to include this change?

Thanks 🙏

@mxinden
Copy link

mxinden commented May 15, 2023

Friendly ping. Could you cut a new release with this patch?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants