-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(codecs): add support for protobuf encoding (#18598)
* feat(codecs): add support for protobuf encoding * rename descriptor_set_path to desc_file * fix spelling errors * describe encode_message * factor out common test code * move protobuf test data to one directory * create common protobuf module * add map encoding and test * add enum encoding and test * add timestamp encoding and test * fix spelling again * assert -> assert_eq * remove simple comments * enums are not case-sensitive * add round trip test * spelling (maybe?) * fix clippy errors
- Loading branch information
Showing
50 changed files
with
1,502 additions
and
40 deletions.
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
//! A collection of common utility features used by both encoding and decoding logic. | ||
pub mod protobuf; |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use prost_reflect::{DescriptorPool, MessageDescriptor}; | ||
use std::path::Path; | ||
|
||
/// Load a `MessageDescriptor` from a specific message type from the given descriptor set file. | ||
/// | ||
/// The path should point to the output of `protoc -o <path> ...` | ||
pub fn get_message_descriptor( | ||
descriptor_set_path: &Path, | ||
message_type: &str, | ||
) -> vector_common::Result<MessageDescriptor> { | ||
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}") | ||
})?; | ||
pool.get_message_by_name(message_type).ok_or_else(|| { | ||
format!("The message type '{message_type}' could not be found in '{descriptor_set_path:?}'") | ||
.into() | ||
}) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use std::path::PathBuf; | ||
|
||
#[test] | ||
fn test_get_message_descriptor() { | ||
let path = PathBuf::from(std::env::var_os("CARGO_MANIFEST_DIR").unwrap()) | ||
.join("tests/data/protobuf/test.desc"); | ||
let message_descriptor = get_message_descriptor(&path, "test.Integers").unwrap(); | ||
assert_eq!("Integers", message_descriptor.name()); | ||
assert_eq!(4, message_descriptor.fields().count()); | ||
} | ||
} |
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
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
Oops, something went wrong.