-
Notifications
You must be signed in to change notification settings - Fork 224
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
proto: Add Any
Protobuf type and improve Duration
and Timestamp
Protobuf types
#1452
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f861448
proto: Expose well-known types `Any`, `Duration` and `Timestamp`
romac 965b255
Add proper JSON serialization for `Duration`
romac cd3b9f0
Add non-compliant JSON serialization for `Any`
romac c18fc5f
Fix no_std compatibility
romac ade3519
Add changelog entries
romac a63416c
Add `json-schema` feature flag to enable derivation of `schemars::Jso…
romac ae69d18
Add conversion from and into `core::time::Duration` for `google::prot…
romac fa38ea8
Add conversion from and into `std::time::SystemTime` for `google::pro…
romac e0b13b1
Add `borsh` and `parity-scale-codec` features with corresponding deri…
romac 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
- `[tendermint-proto]` Add `Any` type under `tendermint_proto::google::protobuf::Any` ([#1445](https://github.com/informalsystems/tendermint-rs/issues/1445)) |
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,2 @@ | ||
- `[tendermint-proto]` Implement `prost::Name` for `tendermint_proto::google::protobuf::{Duration, Timestamp}` ([#1452](https://github.com/informalsystems/tendermint-rs/pull/1452/)) | ||
|
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 @@ | ||
- `[tendermint-proto]` Improve ProtoJSON serialization of `tendermint_proto::google::protobuf::{Duration, Timestamp}` ([#1452](https://github.com/informalsystems/tendermint-rs/pull/1452/)) |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
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,336 @@ | ||
// Original code from <https://github.com/influxdata/pbjson/blob/main/pbjson-types/src/any.rs> | ||
// Copyright 2022 Dan Burkert & Tokio Contributors | ||
|
||
use prost::{DecodeError, EncodeError, Message, Name}; | ||
use subtle_encoding::base64; | ||
|
||
use crate::prelude::*; | ||
|
||
use super::type_url::{type_url_for, TypeUrl}; | ||
use super::PACKAGE; | ||
|
||
/// `Any` contains an arbitrary serialized protocol buffer message along with a | ||
/// URL that describes the type of the serialized message. | ||
/// | ||
/// Protobuf library provides support to pack/unpack Any values in the form | ||
/// of utility functions or additional generated methods of the Any type. | ||
/// | ||
/// # Example | ||
/// | ||
/// Pack and unpack a message in Rust: | ||
/// | ||
/// ```rust,ignore | ||
/// let foo1 = Foo { ... }; | ||
/// let any = Any::from_msg(&foo1)?; | ||
/// let foo2 = any.to_msg::<Foo>()?; | ||
/// assert_eq!(foo1, foo2); | ||
/// ``` | ||
/// | ||
/// The pack methods provided by protobuf library will by default use | ||
/// 'type.googleapis.com/full.type.name' as the type URL and the unpack | ||
/// methods only use the fully qualified type name after the last '/' | ||
/// in the type URL, for example "foo.bar.com/x/y.z" will yield type | ||
/// name "y.z". | ||
/// | ||
/// # JSON | ||
/// | ||
/// JSON serialization of Any cannot be made compatible with the specification. | ||
/// See <https://github.com/influxdata/pbjson/issues/2> for more information. | ||
/// | ||
/// At the moment, an `Any` struct will be serialized as a JSON object with two fields: | ||
/// - `typeUrl` (string): the type URL of the message | ||
/// - `value` (string): the base64-encoded serialized message | ||
/// | ||
/// For example: | ||
/// ```json | ||
/// { | ||
/// "typeUrl": "type.googleapis.com/google.protobuf.Duration", | ||
/// "value": "Cg0KB2NvcnA=" | ||
/// } | ||
/// ``` | ||
#[derive(Clone, PartialEq, Eq, ::prost::Message)] | ||
#[cfg_attr(feature = "json-schema", derive(::schemars::JsonSchema))] | ||
pub struct Any { | ||
/// A URL/resource name that uniquely identifies the type of the serialized | ||
/// protocol buffer message. This string must contain at least | ||
/// one "/" character. The last segment of the URL's path must represent | ||
/// the fully qualified name of the type (as in | ||
/// `path/google.protobuf.Duration`). The name should be in a canonical form | ||
/// (e.g., leading "." is not accepted). | ||
/// | ||
/// In practice, teams usually precompile into the binary all types that they | ||
/// expect it to use in the context of Any. However, for URLs which use the | ||
/// scheme `http`, `https`, or no scheme, one can optionally set up a type | ||
/// server that maps type URLs to message definitions as follows: | ||
/// | ||
/// * If no scheme is provided, `https` is assumed. | ||
/// * An HTTP GET on the URL must yield a \[google.protobuf.Type\]\[\] | ||
/// value in binary format, or produce an error. | ||
/// * Applications are allowed to cache lookup results based on the | ||
/// URL, or have them precompiled into a binary to avoid any | ||
/// lookup. Therefore, binary compatibility needs to be preserved | ||
/// on changes to types. (Use versioned type names to manage | ||
/// breaking changes.) | ||
/// | ||
/// Note: this functionality is not currently available in the official | ||
/// protobuf release, and it is not used for type URLs beginning with | ||
/// type.googleapis.com. | ||
/// | ||
/// Schemes other than `http`, `https` (or the empty scheme) might be | ||
/// used with implementation specific semantics. | ||
#[prost(string, tag = "1")] | ||
pub type_url: ::prost::alloc::string::String, | ||
/// Must be a valid serialized protocol buffer of the above specified type. | ||
#[prost(bytes = "vec", tag = "2")] | ||
pub value: ::prost::alloc::vec::Vec<u8>, | ||
} | ||
|
||
impl Any { | ||
/// Serialize the given message type `M` as [`Any`]. | ||
pub fn from_msg<M>(msg: &M) -> Result<Self, EncodeError> | ||
where | ||
M: Name, | ||
{ | ||
let type_url = M::type_url(); | ||
let mut value = Vec::new(); | ||
Message::encode(msg, &mut value)?; | ||
Ok(Any { type_url, value }) | ||
} | ||
|
||
/// Decode the given message type `M` from [`Any`], validating that it has | ||
/// the expected type URL. | ||
pub fn to_msg<M>(&self) -> Result<M, DecodeError> | ||
where | ||
M: Default + Name + Sized, | ||
{ | ||
let expected_type_url = M::type_url(); | ||
|
||
if let (Some(expected), Some(actual)) = ( | ||
TypeUrl::new(&expected_type_url), | ||
TypeUrl::new(&self.type_url), | ||
) { | ||
if expected == actual { | ||
return M::decode(self.value.as_slice()); | ||
} | ||
} | ||
|
||
let mut err = DecodeError::new(format!( | ||
"expected type URL: \"{}\" (got: \"{}\")", | ||
expected_type_url, &self.type_url | ||
)); | ||
err.push("unexpected type URL", "type_url"); | ||
Err(err) | ||
} | ||
} | ||
|
||
impl Name for Any { | ||
const PACKAGE: &'static str = PACKAGE; | ||
const NAME: &'static str = "Any"; | ||
|
||
fn type_url() -> String { | ||
type_url_for::<Self>() | ||
} | ||
} | ||
|
||
impl serde::Serialize for Any { | ||
fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error> | ||
where | ||
S: serde::Serializer, | ||
{ | ||
use serde::ser::SerializeStruct; | ||
let mut len = 0; | ||
if !self.type_url.is_empty() { | ||
len += 1; | ||
} | ||
if !self.value.is_empty() { | ||
len += 1; | ||
} | ||
let mut struct_ser = serializer.serialize_struct("google.protobuf.Any", len)?; | ||
if !self.type_url.is_empty() { | ||
struct_ser.serialize_field("typeUrl", &self.type_url)?; | ||
} | ||
if !self.value.is_empty() { | ||
// NOTE: A base64 string is always valid UTF-8. | ||
struct_ser.serialize_field( | ||
"value", | ||
&String::from_utf8_lossy(&base64::encode(&self.value)), | ||
)?; | ||
} | ||
struct_ser.end() | ||
} | ||
} | ||
impl<'de> serde::Deserialize<'de> for Any { | ||
fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error> | ||
where | ||
D: serde::Deserializer<'de>, | ||
{ | ||
const FIELDS: &[&str] = &["type_url", "typeUrl", "value"]; | ||
|
||
#[allow(clippy::enum_variant_names)] | ||
enum GeneratedField { | ||
TypeUrl, | ||
Value, | ||
} | ||
impl<'de> serde::Deserialize<'de> for GeneratedField { | ||
fn deserialize<D>(deserializer: D) -> core::result::Result<GeneratedField, D::Error> | ||
where | ||
D: serde::Deserializer<'de>, | ||
{ | ||
struct GeneratedVisitor; | ||
|
||
impl<'de> serde::de::Visitor<'de> for GeneratedVisitor { | ||
type Value = GeneratedField; | ||
|
||
fn expecting( | ||
&self, | ||
formatter: &mut core::fmt::Formatter<'_>, | ||
) -> core::fmt::Result { | ||
write!(formatter, "expected one of: {:?}", &FIELDS) | ||
} | ||
|
||
#[allow(unused_variables)] | ||
fn visit_str<E>(self, value: &str) -> core::result::Result<GeneratedField, E> | ||
where | ||
E: serde::de::Error, | ||
{ | ||
match value { | ||
"typeUrl" | "type_url" => Ok(GeneratedField::TypeUrl), | ||
"value" => Ok(GeneratedField::Value), | ||
_ => Err(serde::de::Error::unknown_field(value, FIELDS)), | ||
} | ||
} | ||
} | ||
deserializer.deserialize_identifier(GeneratedVisitor) | ||
} | ||
} | ||
struct GeneratedVisitor; | ||
impl<'de> serde::de::Visitor<'de> for GeneratedVisitor { | ||
type Value = Any; | ||
|
||
fn expecting(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { | ||
formatter.write_str("struct google.protobuf.Any") | ||
} | ||
|
||
fn visit_map<V>(self, mut map_: V) -> core::result::Result<Any, V::Error> | ||
where | ||
V: serde::de::MapAccess<'de>, | ||
{ | ||
let mut type_url__ = None; | ||
let mut value__ = None; | ||
while let Some(k) = map_.next_key()? { | ||
match k { | ||
GeneratedField::TypeUrl => { | ||
if type_url__.is_some() { | ||
return Err(serde::de::Error::duplicate_field("typeUrl")); | ||
} | ||
type_url__ = Some(map_.next_value()?); | ||
}, | ||
GeneratedField::Value => { | ||
if value__.is_some() { | ||
return Err(serde::de::Error::duplicate_field("value")); | ||
} | ||
let b64_str = map_.next_value::<String>()?; | ||
let value = base64::decode(b64_str.as_bytes()).map_err(|e| { | ||
serde::de::Error::custom(format!("base64 decode error: {e}")) | ||
})?; | ||
value__ = Some(value); | ||
}, | ||
} | ||
} | ||
Ok(Any { | ||
type_url: type_url__.unwrap_or_default(), | ||
value: value__.unwrap_or_default(), | ||
}) | ||
} | ||
} | ||
deserializer.deserialize_struct("google.protobuf.Any", FIELDS, GeneratedVisitor) | ||
} | ||
} | ||
|
||
#[cfg(any(feature = "borsh", feature = "parity-scale-codec"))] | ||
mod sealed { | ||
use super::Any; | ||
|
||
use alloc::string::String; | ||
use alloc::vec::Vec; | ||
|
||
#[cfg_attr( | ||
feature = "parity-scale-codec", | ||
derive( | ||
parity_scale_codec::Encode, | ||
parity_scale_codec::Decode, | ||
scale_info::TypeInfo | ||
) | ||
)] | ||
#[cfg_attr( | ||
feature = "borsh", | ||
derive(borsh::BorshSerialize, borsh::BorshDeserialize) | ||
)] | ||
struct InnerAny { | ||
pub type_url: String, | ||
pub value: Vec<u8>, | ||
} | ||
|
||
#[cfg(feature = "borsh")] | ||
impl borsh::BorshSerialize for Any { | ||
fn serialize<W: borsh::io::Write>(&self, writer: &mut W) -> borsh::io::Result<()> { | ||
let inner_any = InnerAny { | ||
type_url: self.type_url.clone(), | ||
value: self.value.clone(), | ||
}; | ||
|
||
borsh::BorshSerialize::serialize(&inner_any, writer) | ||
} | ||
} | ||
|
||
#[cfg(feature = "borsh")] | ||
impl borsh::BorshDeserialize for Any { | ||
fn deserialize_reader<R: borsh::io::Read>(reader: &mut R) -> borsh::io::Result<Self> { | ||
let inner_any = InnerAny::deserialize_reader(reader)?; | ||
|
||
Ok(Any { | ||
type_url: inner_any.type_url, | ||
value: inner_any.value, | ||
}) | ||
} | ||
} | ||
|
||
#[cfg(feature = "parity-scale-codec")] | ||
impl parity_scale_codec::Encode for Any { | ||
fn encode_to<T: parity_scale_codec::Output + ?Sized>(&self, writer: &mut T) { | ||
let inner_any = InnerAny { | ||
type_url: self.type_url.clone(), | ||
value: self.value.clone(), | ||
}; | ||
inner_any.encode_to(writer); | ||
} | ||
} | ||
#[cfg(feature = "parity-scale-codec")] | ||
impl parity_scale_codec::Decode for Any { | ||
fn decode<I: parity_scale_codec::Input>( | ||
input: &mut I, | ||
) -> Result<Self, parity_scale_codec::Error> { | ||
let inner_any = InnerAny::decode(input)?; | ||
Ok(Any { | ||
type_url: inner_any.type_url.clone(), | ||
value: inner_any.value, | ||
}) | ||
} | ||
} | ||
|
||
#[cfg(feature = "parity-scale-codec")] | ||
impl scale_info::TypeInfo for Any { | ||
type Identity = Self; | ||
|
||
fn type_info() -> scale_info::Type { | ||
scale_info::Type::builder() | ||
.path(scale_info::Path::new("Any", "ibc_proto::google::protobuf")) | ||
// i128 is chosen before we represent the timestamp is nanoseconds, which is represented as a i128 by Time | ||
.composite( | ||
scale_info::build::Fields::named() | ||
.field(|f| f.ty::<String>().name("type_url").type_name("String")) | ||
.field(|f| f.ty::<Vec<u8>>().name("value").type_name("Vec<u8>")), | ||
) | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.
I'm getting a bunch of build failures trying to use this feature:
(repeat several dozen instances of that error re:
std
)I think you forgot an
extern crate std
and it seems like CI probably isn't testing it.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.
Opened an issue to track this: #1454