Crate prost_reflect

Source
Expand description

This crate provides support for dynamic protobuf messages. These are useful when the protobuf type definition is not known ahead of time.

The main entry points into the API of this crate are:

§Example - decoding

DynamicMessage does not implement Default since it needs a message descriptor to function. To decode a protobuf byte stream into an instance of this type, use DynamicMessage::decode to create a default value for the MessageDescriptor instance and merge into it:

use prost::Message;
use prost_types::FileDescriptorSet;
use prost_reflect::{DynamicMessage, DescriptorPool, Value};

let pool = DescriptorPool::decode(include_bytes!("file_descriptor_set.bin").as_ref()).unwrap();
let message_descriptor = pool.get_message_by_name("package.MyMessage").unwrap();

let dynamic_message = DynamicMessage::decode(message_descriptor, b"\x08\x96\x01".as_ref()).unwrap();

assert_eq!(dynamic_message.get_field_by_name("foo").unwrap().as_ref(), &Value::I32(150));

§Example - JSON mapping

When the serde feature is enabled, DynamicMessage can be deserialized to and from the canonical JSON mapping defined for protobuf messages.

use prost::Message;
use prost_reflect::{DynamicMessage, DescriptorPool, Value};
use serde_json::de::Deserializer;

let pool = DescriptorPool::decode(include_bytes!("file_descriptor_set.bin").as_ref()).unwrap();
let message_descriptor = pool.get_message_by_name("package.MyMessage").unwrap();

let json = r#"{ "foo": 150 }"#;
let mut deserializer = Deserializer::from_str(json);
let dynamic_message = DynamicMessage::deserialize(message_descriptor, &mut deserializer).unwrap();
deserializer.end().unwrap();

assert_eq!(dynamic_message.get_field_by_name("foo").unwrap().as_ref(), &Value::I32(150));

§Implementing ReflectMessage

The ReflectMessage trait provides a .descriptor() method to get type information for a message. It is implemented for DynamicMessage and the well-known-types provided by prost-types.

When the derive feature is enabled, it can be derived for Message implementations. The derive macro takes the following parameters:

NameValue
descriptor_poolAn expression that resolves to a DescriptorPool containing the message type. The descriptor should be cached to avoid re-building it. Either this or file_descriptor_pool_bytes must be set
file_descriptor_pool_bytesAn expression that resolves to an implementation of Buf containing an encoded file descriptor set. This will be automatically added to the global descriptor pool the first time ReflectMessage::descriptor() is called.
message_nameThe full name of the message, used to look it up within DescriptorPool.
use prost::Message;
use prost_reflect::{DescriptorPool, ReflectMessage};
use once_cell::sync::Lazy;

static DESCRIPTOR_POOL: Lazy<DescriptorPool>
    = Lazy::new(|| DescriptorPool::decode(include_bytes!("file_descriptor_set.bin").as_ref()).unwrap());

#[derive(Message, ReflectMessage)]
#[prost_reflect(descriptor_pool = "DESCRIPTOR_POOL", message_name = "package.MyMessage")]
pub struct MyMessage {}

let message = MyMessage {};
assert_eq!(message.descriptor().full_name(), "package.MyMessage");

If you are using prost-build, the prost-reflect-build crate provides helpers to generate ReflectMessage implementations:

prost_reflect_build::Builder::new()
    .compile_protos(&["src/package.proto"], &["src"])
    .unwrap();

Re-exports§

pub use prost;
pub use prost::bytes;
pub use prost_types;

Modules§

text_format
Parsing and formatting for the protobuf text format.

Structs§

DescriptorError
An error that may occur while creating a DescriptorPool.
DescriptorPool
A DescriptorPool is a collection of related descriptors. Typically it will be created from a FileDescriptorSet output by the protobuf compiler (see DescriptorPool::from_file_descriptor_set) but it may also be built up by adding files individually.
DeserializeOptions
Options to control deserialization of messages.
DynamicMessage
DynamicMessage provides encoding, decoding and reflection of a protobuf message.
EnumDescriptor
A protobuf enum type.
EnumValueDescriptor
A value in a protobuf enum type.
ExtensionDescriptor
A protobuf extension field definition.
FieldDescriptor
A protobuf message definition.
FileDescriptor
A single source file containing protobuf messages and services.
MessageDescriptor
A protobuf message definition.
MethodDescriptor
A method definition for a ServiceDescriptor.
OneofDescriptor
A oneof field in a protobuf message.
SerializeOptions
Options to control serialization of messages.
ServiceDescriptor
A protobuf service definition.
UnknownField
An unknown field found when deserializing a protobuf message.

Enums§

Cardinality
Cardinality determines whether a field is optional, required, or repeated.
Kind
The type of a protobuf message field.
MapKey
A dynamically-typed key for a protobuf map.
SetFieldError
Error type returned by DynamicMessage::try_set_field().
Syntax
The syntax of a proto file.
Value
A dynamically-typed protobuf value.

Traits§

ReflectMessage
Trait for message types that support reflection.

Derive Macros§

ReflectMessagederive
A derive macro for the ReflectMessage trait.