-
Notifications
You must be signed in to change notification settings - Fork 59
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
nb-mouse
wants to merge
4
commits into
vectordotdev:main
Choose a base branch
from
nb-mouse:protobuf-descriptor-pool
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,31 @@ | ||
use prost_reflect::{DescriptorPool, MessageDescriptor}; | ||
use std::path::Path; | ||
|
||
pub fn get_message_descriptor( | ||
pub fn get_message_pool_descriptor( | ||
descriptor_set_path: &Path, | ||
message_type: &str, | ||
) -> std::result::Result<MessageDescriptor, String> { | ||
) -> std::result::Result<DescriptorPool, String> { | ||
let b = std::fs::read(descriptor_set_path).map_err(|e| { | ||
format!("Failed to open protobuf desc file '{descriptor_set_path:?}': {e}",) | ||
})?; | ||
let pool = DescriptorPool::decode(b.as_slice()).map_err(|e| { | ||
format!("Failed to parse protobuf desc file '{descriptor_set_path:?}': {e}") | ||
})?; | ||
DescriptorPool::decode(b.as_slice()) | ||
.map_err(|e| format!("Failed to parse protobuf desc file '{descriptor_set_path:?}': {e}")) | ||
} | ||
|
||
pub fn get_message_descriptor( | ||
descriptor_set_path: &Path, | ||
message_type: &str, | ||
) -> std::result::Result<MessageDescriptor, String> { | ||
let pool = get_message_pool_descriptor(descriptor_set_path)?; | ||
pool.get_message_by_name(message_type).ok_or_else(|| { | ||
format!("The message type '{message_type}' could not be found in '{descriptor_set_path:?}'") | ||
}) | ||
} | ||
|
||
pub fn get_message_descriptor_from_pool( | ||
pool: &DescriptorPool, | ||
message_type: &str, | ||
) -> std::result::Result<MessageDescriptor, String> { | ||
pool.get_message_by_name(message_type).ok_or_else(|| { | ||
format!("The message type '{message_type}' could not be found in the descriptor pool") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
use crate::compiler::prelude::*; | ||
use crate::protobuf::get_message_descriptor_from_pool; | ||
use prost_reflect::ReflectMessage; | ||
use prost_reflect::{DynamicMessage, MessageDescriptor}; | ||
|
||
use tracing::debug; | ||
|
||
pub fn proto_to_value( | ||
descriptor_pool: Option<&prost_reflect::DescriptorPool>, | ||
prost_reflect_value: &prost_reflect::Value, | ||
field_descriptor: Option<&prost_reflect::FieldDescriptor>, | ||
) -> std::result::Result<Value, String> { | ||
|
@@ -42,11 +46,49 @@ pub fn proto_to_value( | |
} | ||
} | ||
prost_reflect::Value::Message(v) => { | ||
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) { | ||
Ok(message_descriptor) => { | ||
match DynamicMessage::decode( | ||
message_descriptor, | ||
value.clone(), | ||
) { | ||
Ok(dynamic_message) => { | ||
return proto_to_value( | ||
Some(descriptor_pool), | ||
&prost_reflect::Value::Message(dynamic_message), | ||
None, | ||
); | ||
} | ||
Err(error) => { | ||
return Err(format!( | ||
"Error parsing embedded protobuf message: {:?}", | ||
error | ||
)); | ||
} | ||
} | ||
} | ||
Err(_) => { | ||
debug!("Message type '{}' not found in the descriptor pool, ignoring message.", type_name); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
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))?; | ||
obj_map.insert(field_desc.name().into(), out); | ||
} | ||
} | ||
|
@@ -55,7 +97,7 @@ pub fn proto_to_value( | |
prost_reflect::Value::List(v) => { | ||
let vec = v | ||
.iter() | ||
.map(|o| proto_to_value(o, field_descriptor)) | ||
.map(|o| proto_to_value(descriptor_pool, o, field_descriptor)) | ||
.collect::<Result<Vec<_>, String>>()?; | ||
Value::from(vec) | ||
} | ||
|
@@ -80,7 +122,7 @@ pub fn proto_to_value( | |
) | ||
})? | ||
.into(), | ||
proto_to_value(kv.1, Some(&message_desc.map_entry_value_field()))?, | ||
proto_to_value(descriptor_pool, kv.1, Some(&message_desc.map_entry_value_field()))?, | ||
)) | ||
}) | ||
.collect::<std::result::Result<ObjectMap, String>>()?, | ||
|
@@ -99,6 +141,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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
||
&prost_reflect::Value::Message(dynamic_message), | ||
None, | ||
)?) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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.
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.
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: