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

enhancement(codecs)!: Use DescriptorPool from ProtobufDeserializer #901

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

nb-mouse
Copy link

This change allows to recursively handle google.protobuf.Any messages from a shared DescriptorPool loaded via decoding.protobuf.desc_file at best effort.

@nb-mouse nb-mouse changed the title enhancement(codecs): Use DescriptorPool from ProtobufDeserializer enhancement(codecs)!: Use DescriptorPool from ProtobufDeserializer Jun 21, 2024
@jszwedko jszwedko requested a review from pront June 21, 2024 19:25
let mut obj_map = ObjectMap::new();
for field_desc in v.descriptor().fields() {
if v.has_field(&field_desc) {
let field_value = v.get_field(&field_desc);
let out = proto_to_value(field_value.as_ref(), Some(&field_desc))?;
let out = proto_to_value(descriptor_pool, field_value.as_ref(), Some(&field_desc))?;
Copy link
Author

Choose a reason for hiding this comment

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

There is a possible improvement if fields in descriptor is ordered and type_url comes before value field then logic from above can be moved here.

@@ -99,6 +130,7 @@ pub(crate) fn parse_proto(descriptor: &MessageDescriptor, value: Value) -> Resol
let dynamic_message = DynamicMessage::decode(descriptor.clone(), bytes)
.map_err(|error| format!("Error parsing protobuf: {:?}", error))?;
Ok(proto_to_value(
None,
Copy link
Author

Choose a reason for hiding this comment

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

If we declare a singleton method to retrieve global configuration with loaded descriptor pool, we can use it here (when specified)

@pront
Copy link
Contributor

pront commented Jun 28, 2024

Hi @nb-mouse, I was unavailable the past few days. I will take a look shortly.

Comment on lines +49 to +55
if let Some(descriptor_pool) = descriptor_pool {
if let Some(type_url_cow) = v.get_field_by_name("type_url") {
if let Some(value_cow) = v.get_field_by_name("value") {
if let prost_reflect::Value::String(type_url) = &*type_url_cow {
if let prost_reflect::Value::Bytes(value) = &*value_cow {
let type_name = type_url.trim_start_matches("type.googleapis.com/");
match get_message_descriptor_from_pool(descriptor_pool, type_name) {
Copy link
Contributor

@pront pront Jul 2, 2024

Choose a reason for hiding this comment

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

This is a lot of nesting, can you explain the motivation and the use case for this? I am also worried about the hardcoded values in here.

Copy link
Author

Choose a reason for hiding this comment

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

This was a naive implementation for checking the presence of fields.

The FieldDescriptor actually have a type_name. If we can check if the given field of this type, we can decode it straight away:

    field {
      name: "anydata"
      number: 3
      label: LABEL_OPTIONAL
      type: TYPE_MESSAGE
      type_name: ".google.protobuf.Any"
      json_name: "anydata"
    }

@jszwedko jszwedko requested a review from pront August 23, 2024 17:27
@pront pront marked this pull request as draft September 13, 2024 16:42
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.

2 participants