Skip to content

Commit

Permalink
add support for group in oneof; add field_number to unknown_fields in…
Browse files Browse the repository at this point in the history
…stead of tag
  • Loading branch information
kristopherbullinger committed Oct 2, 2024
1 parent 6927801 commit c44f053
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
41 changes: 35 additions & 6 deletions protobuf-codegen/src/gen/field/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -970,13 +970,42 @@ impl<'a> FieldGen<'a> {
} else {
typed
};
let variant_path = o.variant_path(&self.proto_field.message.scope.rust_path_to_file());
if o.elem.proto_type() == Type::TYPE_GROUP {
let proto_path = protobuf_crate_path(&self.customize);
w.write_line(&format!(
"let end_tag = {proto_path}::rt::set_wire_type_to_end_group(tag);"
));
let value_type = &self
.elem()
.rust_storage_elem_type(
&self
.proto_field
.message
.scope
.file_and_mod(self.customize.clone()),
)
.to_code(&self.customize);

w.write_line(&format!(
"self.{} = ::std::option::Option::Some({}({}));",
o.oneof_field_name,
o.variant_path(&self.proto_field.message.scope.rust_path_to_file()),
maybe_boxed.value
));
w.write_line(&format!(
"let mut {} = {}::default();",
o.oneof_field_name, value_type,
));
w.write_line(&format!(
"{}.merge_delimited(is, end_tag)?;",
o.oneof_field_name,
));

w.write_line(&format!(
"self.{} = ::std::option::Option::Some({}({}));",
o.oneof_field_name, variant_path, o.oneof_field_name,
));
} else {
w.write_line(&format!(
"self.{} = ::std::option::Option::Some({}({}));",
o.oneof_field_name, variant_path, maybe_boxed.value
));
}
})
}

Expand Down
15 changes: 14 additions & 1 deletion protobuf-codegen/src/gen/oneof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,19 @@ impl<'a> OneofGen<'a> {
.collect()
}

pub fn variants(&'a self) -> impl Iterator<Item = OneofVariantGen<'a>> {
self.oneof.variants().into_iter().map(|v| {
let field = self
.message
.fields
.iter()
.filter(|f| f.proto_field.name() == v.field.name())
.next()
.expect(&format!("field not found by name: {}", v.field.name()));
OneofVariantGen::parse(self, v, field, self.message.root_scope)
})
}

pub fn full_storage_type(&self) -> RustType {
RustType::Option(Box::new(RustType::Oneof(
self.type_name_relative(
Expand Down Expand Up @@ -262,7 +275,7 @@ impl<'a> OneofGen<'a> {
}
write_protoc_insertion_point_for_oneof(w, &self.customize.for_elem, &self.oneof.oneof);
w.pub_enum(&self.oneof.rust_name().ident.to_string(), |w| {
for variant in self.variants_except_group() {
for variant in self.variants() {
write_protoc_insertion_point_for_oneof_field(
w,
&self.customize.for_children,
Expand Down
2 changes: 1 addition & 1 deletion protobuf/src/rt/unknown_or_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub fn read_unknown(
) -> crate::Result<()> {
let (field_number, wire_type) = Tag::new(tag)?.unpack();
let unknown = is.read_unknown_with_tag_unpacked(field_number, wire_type)?;
unknown_fields.add_value(tag, unknown);
unknown_fields.add_value(field_number, unknown);
Ok(())
}

Expand Down

0 comments on commit c44f053

Please sign in to comment.