pub struct DynamicMessage { /* private fields */ }
Expand description
Represents a dynamic gRPC
message that can be used
with various message types.
Implementations§
Source§impl DynamicMessage
impl DynamicMessage
Sourcepub fn new(message_desc: MessageDescriptor) -> Self
pub fn new(message_desc: MessageDescriptor) -> Self
Create a new DynamicMessage
from a MessageDescriptor
.
Sourcepub fn message_name(&self) -> String
pub fn message_name(&self) -> String
Get the name of the message as a String.
Sourcepub fn descriptor(&self) -> MessageDescriptor
pub fn descriptor(&self) -> MessageDescriptor
Get the message descriptor.
Sourcepub fn to_json(&self) -> Result<String>
pub fn to_json(&self) -> Result<String>
Serialize a DynamicMessage
to a JSON string.
§Errors
- Failed to convert utf8 to String
- Failed to serialize message
Examples found in repository?
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
async fn main() -> Result<()> {
let desc = ProtoDescriptor::new(
vec!["/Users/philippreiter/Rust/wireman/example"],
vec!["grpc_simple/debugger.proto"],
)?;
let service = &desc.get_services()[0];
let method = &desc.get_methods(service)[1];
println!("Service: {:}", service.full_name());
println!("Method: {:}", method.full_name());
let mut req = desc.get_request(&method);
req.set_address("http://localhost:50051");
let resp = do_request(&req).await?;
println!("\nResponse:\n{:}", resp.message.to_json()?);
Ok(())
}
Sourcepub fn apply_template(&mut self)
pub fn apply_template(&mut self)
Apply default values to a DynamicMessage
.
Methods from Deref<Target = DynMessage>§
Sourcepub fn merge_text_format(&mut self, input: &str) -> Result<(), ParseError>
pub fn merge_text_format(&mut self, input: &str) -> Result<(), ParseError>
Merges the given message encoded using the text format into this message.
§Examples
let mut dynamic_message = DynamicMessage::new(message_descriptor);
dynamic_message.merge_text_format("foo: 150").unwrap();
assert_eq!(dynamic_message.get_field_by_name("foo").unwrap().as_ref(), &Value::I32(150));
Sourcepub fn to_text_format(&self) -> String
pub fn to_text_format(&self) -> String
Formats this dynamic message using the protobuf text format, with default options.
§Examples
let dynamic_message = DynamicMessage::decode(message_descriptor, b"\x08\x96\x01\x1a\x02\x10\x42".as_ref()).unwrap();
assert_eq!(dynamic_message.to_text_format(), "foo:150,nested{bar:66}");
Sourcepub fn to_text_format_with_options(&self, options: &FormatOptions) -> String
pub fn to_text_format_with_options(&self, options: &FormatOptions) -> String
Formats this dynamic message using the protobuf text format, with custom options.
§Examples
let dynamic_message = DynamicMessage::decode(message_descriptor, b"\x08\x96\x01\x1a\x02\x10\x42".as_ref()).unwrap();
let options = FormatOptions::new().pretty(true);
assert_eq!(dynamic_message.to_text_format_with_options(&options), "foo: 150\nnested {\n bar: 66\n}");
Sourcepub fn serialize_with_options<S>(
&self,
serializer: S,
options: &SerializeOptions,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
pub fn serialize_with_options<S>(
&self,
serializer: S,
options: &SerializeOptions,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
Serialize this message into serializer
using the encoding specified by options
.
§Examples
let dynamic_message = DynamicMessage::new(message_descriptor);
let mut serializer = serde_json::Serializer::new(vec![]);
let mut options = SerializeOptions::new().skip_default_fields(false);
dynamic_message.serialize_with_options(&mut serializer, &options).unwrap();
assert_eq!(serializer.into_inner(), b"{\"foo\":0}");
Sourcepub fn has_field(&self, field_desc: &FieldDescriptor) -> bool
pub fn has_field(&self, field_desc: &FieldDescriptor) -> bool
Returns true
if this message has the given field set.
If the field type supports distinguishing whether a value has been set (see supports_presence
),
such as for messages, then this method returns true
only if a value has been set. For
other types, such as integers, it returns true
if the value is set to a non-default value.
If this method returns false
, then the field will not be included in the encoded bytes
of this message.
§Examples
This example uses the following message definition:
message MyMessage {
int32 foo = 1;
oneof optional {
int32 bar = 2;
}
}
let foo = message_descriptor.get_field_by_name("foo").unwrap();
let bar = message_descriptor.get_field_by_name("bar").unwrap();
assert!(!foo.supports_presence());
assert!(bar.supports_presence());
let mut dynamic_message = DynamicMessage::new(message_descriptor);
assert!(!dynamic_message.has_field(&foo));
assert!(!dynamic_message.has_field(&bar));
dynamic_message.set_field(&foo, Value::I32(0));
dynamic_message.set_field(&bar, Value::I32(0));
assert!(!dynamic_message.has_field(&foo));
assert!(dynamic_message.has_field(&bar));
dynamic_message.set_field(&foo, Value::I32(5));
dynamic_message.set_field(&bar, Value::I32(6));
assert!(dynamic_message.has_field(&foo));
assert!(dynamic_message.has_field(&bar));
Sourcepub fn get_field(&self, field_desc: &FieldDescriptor) -> Cow<'_, Value>
pub fn get_field(&self, field_desc: &FieldDescriptor) -> Cow<'_, Value>
Gets the value of the given field, or the default value if it is unset.
Sourcepub fn get_field_mut(&mut self, field_desc: &FieldDescriptor) -> &mut Value
pub fn get_field_mut(&mut self, field_desc: &FieldDescriptor) -> &mut Value
Gets a mutable reference to the value ofthe given field. If the field is not set, it is inserted with its default value.
Sourcepub fn set_field(&mut self, field_desc: &FieldDescriptor, value: Value)
pub fn set_field(&mut self, field_desc: &FieldDescriptor, value: Value)
Sets the value of the given field.
§Panics
This method may panic if the value type is not compatible with the field type, as defined
by Value::is_valid_for_field
. Consider using try_set_field()
for a non-panicking version.
Sourcepub fn try_set_field(
&mut self,
field_desc: &FieldDescriptor,
value: Value,
) -> Result<(), SetFieldError>
pub fn try_set_field( &mut self, field_desc: &FieldDescriptor, value: Value, ) -> Result<(), SetFieldError>
Tries to set the value of the given field, returning an error if the value is an invalid type.
§Examples
let mut dynamic_message = DynamicMessage::new(message_descriptor.clone());
let foo = message_descriptor.get_field_by_name("foo").unwrap();
assert_eq!(dynamic_message.try_set_field(&foo, Value::I32(5)), Ok(()));
assert_eq!(dynamic_message.try_set_field(&foo, Value::String("bar".to_owned())), Err(SetFieldError::InvalidType {
field: foo,
value: Value::String("bar".to_owned()),
}));
Sourcepub fn clear_field(&mut self, field_desc: &FieldDescriptor)
pub fn clear_field(&mut self, field_desc: &FieldDescriptor)
Clears the given field.
After calling this method, has_field
will return false for the field,
and it will not be included in the encoded bytes of this message.
Sourcepub fn has_field_by_number(&self, number: u32) -> bool
pub fn has_field_by_number(&self, number: u32) -> bool
Returns true
if this message has a field set with the given number.
See has_field
for more details.
Sourcepub fn get_field_by_number(&self, number: u32) -> Option<Cow<'_, Value>>
pub fn get_field_by_number(&self, number: u32) -> Option<Cow<'_, Value>>
Gets the value of the field with the given number, or the default value if it is unset.
If the message has no field with the given number, None
is returned.
See get_field
for more details.
Sourcepub fn get_field_by_number_mut(&mut self, number: u32) -> Option<&mut Value>
pub fn get_field_by_number_mut(&mut self, number: u32) -> Option<&mut Value>
Gets a mutable reference to the value of the field with the given number. If the field is not set, it is inserted with its default value.
If the message has no field with the given number, None
is returned.
See get_field_mut
for more details.
Sourcepub fn set_field_by_number(&mut self, number: u32, value: Value)
pub fn set_field_by_number(&mut self, number: u32, value: Value)
Sets the value of the field with number number
.
If no field with the given number exists, this method does nothing.
See set_field
for more details.
Sourcepub fn try_set_field_by_number(
&mut self,
number: u32,
value: Value,
) -> Result<(), SetFieldError>
pub fn try_set_field_by_number( &mut self, number: u32, value: Value, ) -> Result<(), SetFieldError>
Tries to set the value of the field with number number
, returning an error if the value is an invalid type or does not exist.
§Examples
let mut dynamic_message = DynamicMessage::new(message_descriptor.clone());
assert_eq!(dynamic_message.try_set_field_by_number(1, Value::I32(5)), Ok(()));
assert_eq!(dynamic_message.try_set_field_by_number(1, Value::String("bar".to_owned())), Err(SetFieldError::InvalidType {
field: message_descriptor.get_field(1).unwrap(),
value: Value::String("bar".to_owned()),
}));
assert_eq!(dynamic_message.try_set_field_by_number(42, Value::I32(5)), Err(SetFieldError::NotFound));
Sourcepub fn clear_field_by_number(&mut self, number: u32)
pub fn clear_field_by_number(&mut self, number: u32)
Clears the field with the given number.
If no field with the given number exists, this method does nothing.
See clear_field
for more details.
Sourcepub fn has_field_by_name(&self, name: &str) -> bool
pub fn has_field_by_name(&self, name: &str) -> bool
Returns true
if this message has a field set with the given name.
See has_field
for more details.
Sourcepub fn get_field_by_name(&self, name: &str) -> Option<Cow<'_, Value>>
pub fn get_field_by_name(&self, name: &str) -> Option<Cow<'_, Value>>
Gets the value of the field with the given name, or the default value if it is unset.
If the message has no field with the given name, None
is returned.
See get_field
for more details.
Sourcepub fn get_field_by_name_mut(&mut self, name: &str) -> Option<&mut Value>
pub fn get_field_by_name_mut(&mut self, name: &str) -> Option<&mut Value>
Gets a mutable reference to the value of the field with the given name. If the field is not set, it is inserted with its default value.
If the message has no field with the given name, None
is returned.
See get_field_mut
for more details.
Sourcepub fn set_field_by_name(&mut self, name: &str, value: Value)
pub fn set_field_by_name(&mut self, name: &str, value: Value)
Sets the value of the field with name name
.
If no field with the given name exists, this method does nothing.
See set_field
for more details.
Sourcepub fn try_set_field_by_name(
&mut self,
name: &str,
value: Value,
) -> Result<(), SetFieldError>
pub fn try_set_field_by_name( &mut self, name: &str, value: Value, ) -> Result<(), SetFieldError>
Tries to set the value of the field with name name
, returning an error if the value is an invalid type or does not exist.
§Examples
let mut dynamic_message = DynamicMessage::new(message_descriptor.clone());
assert_eq!(dynamic_message.try_set_field_by_name("foo", Value::I32(5)), Ok(()));
assert_eq!(dynamic_message.try_set_field_by_name("foo", Value::String("bar".to_owned())), Err(SetFieldError::InvalidType {
field: message_descriptor.get_field_by_name("foo").unwrap(),
value: Value::String("bar".to_owned()),
}));
assert_eq!(dynamic_message.try_set_field_by_name("notfound", Value::I32(5)), Err(SetFieldError::NotFound));
Sourcepub fn clear_field_by_name(&mut self, name: &str)
pub fn clear_field_by_name(&mut self, name: &str)
Clears the field with the given name.
If no field with the given name exists, this method does nothing.
See clear_field
for more details.
Sourcepub fn take_field(&mut self, field_desc: &FieldDescriptor) -> Option<Value>
pub fn take_field(&mut self, field_desc: &FieldDescriptor) -> Option<Value>
Clears the value for the given field, and returns it.
Returns the value if has_field
was true
, or None
otherwise.
Sourcepub fn take_field_by_name(&mut self, name: &str) -> Option<Value>
pub fn take_field_by_name(&mut self, name: &str) -> Option<Value>
Clears the value for the field with the given name, and returns it.
Returns the value if has_field_by_name
was true
, or None
otherwise.
Sourcepub fn take_field_by_number(&mut self, number: u32) -> Option<Value>
pub fn take_field_by_number(&mut self, number: u32) -> Option<Value>
Clears the value for the field with the given number, and returns it.
Returns the value if has_field_by_number
was true
, or None
otherwise.
Sourcepub fn has_extension(&self, extension_desc: &ExtensionDescriptor) -> bool
pub fn has_extension(&self, extension_desc: &ExtensionDescriptor) -> bool
Returns true
if this message has the given extension field set.
See has_field
for more details.
Sourcepub fn get_extension(
&self,
extension_desc: &ExtensionDescriptor,
) -> Cow<'_, Value>
pub fn get_extension( &self, extension_desc: &ExtensionDescriptor, ) -> Cow<'_, Value>
Gets the value of the given extension field, or the default value if it is unset.
See get_field
for more details.
Sourcepub fn get_extension_mut(
&mut self,
extension_desc: &ExtensionDescriptor,
) -> &mut Value
pub fn get_extension_mut( &mut self, extension_desc: &ExtensionDescriptor, ) -> &mut Value
Gets a mutable reference to the value of the given extension field. If the field is not set, it is inserted with its default value.
See get_field_mut
for more details.
Sourcepub fn set_extension(
&mut self,
extension_desc: &ExtensionDescriptor,
value: Value,
)
pub fn set_extension( &mut self, extension_desc: &ExtensionDescriptor, value: Value, )
Sets the value of the given extension field.
See set_field
for more details.
Sourcepub fn clear_extension(&mut self, extension_desc: &ExtensionDescriptor)
pub fn clear_extension(&mut self, extension_desc: &ExtensionDescriptor)
Clears the given extension field.
See clear_field
for more details.
Sourcepub fn take_extension(
&mut self,
extension_desc: &ExtensionDescriptor,
) -> Option<Value>
pub fn take_extension( &mut self, extension_desc: &ExtensionDescriptor, ) -> Option<Value>
Clears the value for the given extension field, and returns it.
Returns the value if has_extension
was true
, or None
otherwise.
Sourcepub fn fields(&self) -> impl Iterator<Item = (FieldDescriptor, &Value)>
pub fn fields(&self) -> impl Iterator<Item = (FieldDescriptor, &Value)>
Gets an iterator over all fields of this message.
The iterator will yield all fields for which has_field
returns true
.
Sourcepub fn fields_mut(
&mut self,
) -> impl Iterator<Item = (FieldDescriptor, &mut Value)>
pub fn fields_mut( &mut self, ) -> impl Iterator<Item = (FieldDescriptor, &mut Value)>
Gets an iterator returning mutable references to all fields of this message.
The iterator will yield all fields for which has_field
returns true
.
Sourcepub fn take_fields(&mut self) -> impl Iterator<Item = (FieldDescriptor, Value)>
pub fn take_fields(&mut self) -> impl Iterator<Item = (FieldDescriptor, Value)>
Clears all fields from the message and returns an iterator yielding the values.
The iterator will yield all fields for which has_field
returns true
.
If the iterator is dropped before completing the iteration, it is unspecified how many fields are removed.
Sourcepub fn extensions(&self) -> impl Iterator<Item = (ExtensionDescriptor, &Value)>
pub fn extensions(&self) -> impl Iterator<Item = (ExtensionDescriptor, &Value)>
Gets an iterator over all extension fields of this message.
The iterator will yield all extension fields for which has_extension
returns true
.
Sourcepub fn extensions_mut(
&mut self,
) -> impl Iterator<Item = (ExtensionDescriptor, &mut Value)>
pub fn extensions_mut( &mut self, ) -> impl Iterator<Item = (ExtensionDescriptor, &mut Value)>
Gets an iterator returning mutable references to all extension fields of this message.
The iterator will yield all extension fields for which has_extension
returns true
.
Sourcepub fn take_extensions(
&mut self,
) -> impl Iterator<Item = (ExtensionDescriptor, Value)>
pub fn take_extensions( &mut self, ) -> impl Iterator<Item = (ExtensionDescriptor, Value)>
Clears all extension fields from the message and returns an iterator yielding the values.
The iterator will yield all extension fields for which has_extension
returns true
.
If the iterator is dropped before completing the iteration, it is unspecified how many fields are removed.
Sourcepub fn unknown_fields(&self) -> impl Iterator<Item = &UnknownField>
pub fn unknown_fields(&self) -> impl Iterator<Item = &UnknownField>
Gets an iterator over unknown fields for this message.
A field is unknown if the message descriptor does not contain a field with the given number. This is often the result of a new field being added to the message definition.
Unknown fields are preserved when decoding and re-encoding a message.
Sourcepub fn take_unknown_fields(&mut self) -> impl Iterator<Item = UnknownField>
pub fn take_unknown_fields(&mut self) -> impl Iterator<Item = UnknownField>
Clears all unknown fields from the message and returns an iterator yielding the values.
If the iterator is dropped before completing the iteration, it is unspecified how many fields are removed.
Sourcepub fn transcode_from<T>(&mut self, value: &T) -> Result<(), DecodeError>where
T: Message,
pub fn transcode_from<T>(&mut self, value: &T) -> Result<(), DecodeError>where
T: Message,
Merge a strongly-typed message into this one.
The message should be compatible with the type specified by
descriptor
, or the merge will likely fail with
a DecodeError
.
Sourcepub fn transcode_to<T>(&self) -> Result<T, DecodeError>
pub fn transcode_to<T>(&self) -> Result<T, DecodeError>
Convert this dynamic message into a strongly typed value.
The message should be compatible with the type specified by
descriptor
, or the conversion will likely fail with
a DecodeError
.
Trait Implementations§
Source§impl Clone for DynamicMessage
impl Clone for DynamicMessage
Source§fn clone(&self) -> DynamicMessage
fn clone(&self) -> DynamicMessage
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for DynamicMessage
impl Debug for DynamicMessage
Source§impl Deref for DynamicMessage
impl Deref for DynamicMessage
Source§type Target = DynamicMessage
type Target = DynamicMessage
Source§fn deref(&self) -> &DynMessage
fn deref(&self) -> &DynMessage
Source§impl DerefMut for DynamicMessage
impl DerefMut for DynamicMessage
Auto Trait Implementations§
impl Freeze for DynamicMessage
impl RefUnwindSafe for DynamicMessage
impl Send for DynamicMessage
impl Sync for DynamicMessage
impl Unpin for DynamicMessage
impl UnwindSafe for DynamicMessage
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request