Skip to content

Commit

Permalink
feat: impl Name for Protobuf well-known wrapper types (#1174)
Browse files Browse the repository at this point in the history
Co-authored-by: Kelvin Hu <[email protected]>
  • Loading branch information
caspermeijn and sillykelvin authored Nov 1, 2024
1 parent 6d3a7ab commit e7deb3c
Showing 1 changed file with 214 additions and 3 deletions.
217 changes: 214 additions & 3 deletions prost/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
//! the `prost-types` crate in order to avoid a cyclic dependency between `prost` and
//! `prost-build`.
#[cfg(not(feature = "std"))]
use alloc::format;
use alloc::string::String;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

use ::bytes::{Buf, BufMut, Bytes};
Expand All @@ -17,7 +16,7 @@ use crate::{
encoding::{
bool, bytes, double, float, int32, int64, skip_field, string, uint32, uint64, DecodeContext,
},
DecodeError, Message,
DecodeError, Message, Name,
};

/// `google.protobuf.BoolValue`
Expand Down Expand Up @@ -52,6 +51,16 @@ impl Message for bool {
}
}

/// `google.protobuf.BoolValue`
impl Name for bool {
const NAME: &'static str = "BoolValue";
const PACKAGE: &'static str = "google.protobuf";

fn type_url() -> String {
googleapis_type_url_for::<Self>()
}
}

/// `google.protobuf.UInt32Value`
impl Message for u32 {
fn encode_raw(&self, buf: &mut impl BufMut) {
Expand Down Expand Up @@ -84,6 +93,16 @@ impl Message for u32 {
}
}

/// `google.protobuf.UInt32Value`
impl Name for u32 {
const NAME: &'static str = "UInt32Value";
const PACKAGE: &'static str = "google.protobuf";

fn type_url() -> String {
googleapis_type_url_for::<Self>()
}
}

/// `google.protobuf.UInt64Value`
impl Message for u64 {
fn encode_raw(&self, buf: &mut impl BufMut) {
Expand Down Expand Up @@ -116,6 +135,16 @@ impl Message for u64 {
}
}

/// `google.protobuf.UInt64Value`
impl Name for u64 {
const NAME: &'static str = "UInt64Value";
const PACKAGE: &'static str = "google.protobuf";

fn type_url() -> String {
googleapis_type_url_for::<Self>()
}
}

/// `google.protobuf.Int32Value`
impl Message for i32 {
fn encode_raw(&self, buf: &mut impl BufMut) {
Expand Down Expand Up @@ -148,6 +177,16 @@ impl Message for i32 {
}
}

/// `google.protobuf.Int32Value`
impl Name for i32 {
const NAME: &'static str = "Int32Value";
const PACKAGE: &'static str = "google.protobuf";

fn type_url() -> String {
googleapis_type_url_for::<Self>()
}
}

/// `google.protobuf.Int64Value`
impl Message for i64 {
fn encode_raw(&self, buf: &mut impl BufMut) {
Expand Down Expand Up @@ -180,6 +219,16 @@ impl Message for i64 {
}
}

/// `google.protobuf.Int64Value`
impl Name for i64 {
const NAME: &'static str = "Int64Value";
const PACKAGE: &'static str = "google.protobuf";

fn type_url() -> String {
googleapis_type_url_for::<Self>()
}
}

/// `google.protobuf.FloatValue`
impl Message for f32 {
fn encode_raw(&self, buf: &mut impl BufMut) {
Expand Down Expand Up @@ -212,6 +261,16 @@ impl Message for f32 {
}
}

/// `google.protobuf.FloatValue`
impl Name for f32 {
const NAME: &'static str = "FloatValue";
const PACKAGE: &'static str = "google.protobuf";

fn type_url() -> String {
googleapis_type_url_for::<Self>()
}
}

/// `google.protobuf.DoubleValue`
impl Message for f64 {
fn encode_raw(&self, buf: &mut impl BufMut) {
Expand Down Expand Up @@ -244,6 +303,16 @@ impl Message for f64 {
}
}

/// `google.protobuf.DoubleValue`
impl Name for f64 {
const NAME: &'static str = "DoubleValue";
const PACKAGE: &'static str = "google.protobuf";

fn type_url() -> String {
googleapis_type_url_for::<Self>()
}
}

/// `google.protobuf.StringValue`
impl Message for String {
fn encode_raw(&self, buf: &mut impl BufMut) {
Expand Down Expand Up @@ -276,6 +345,16 @@ impl Message for String {
}
}

/// `google.protobuf.StringValue`
impl Name for String {
const NAME: &'static str = "StringValue";
const PACKAGE: &'static str = "google.protobuf";

fn type_url() -> String {
googleapis_type_url_for::<Self>()
}
}

/// `google.protobuf.BytesValue`
impl Message for Vec<u8> {
fn encode_raw(&self, buf: &mut impl BufMut) {
Expand Down Expand Up @@ -308,6 +387,16 @@ impl Message for Vec<u8> {
}
}

/// `google.protobuf.BytesValue`
impl Name for Vec<u8> {
const NAME: &'static str = "BytesValue";
const PACKAGE: &'static str = "google.protobuf";

fn type_url() -> String {
googleapis_type_url_for::<Self>()
}
}

/// `google.protobuf.BytesValue`
impl Message for Bytes {
fn encode_raw(&self, buf: &mut impl BufMut) {
Expand Down Expand Up @@ -340,6 +429,16 @@ impl Message for Bytes {
}
}

/// `google.protobuf.BytesValue`
impl Name for Bytes {
const NAME: &'static str = "BytesValue";
const PACKAGE: &'static str = "google.protobuf";

fn type_url() -> String {
googleapis_type_url_for::<Self>()
}
}

/// `google.protobuf.Empty`
impl Message for () {
fn encode_raw(&self, _buf: &mut impl BufMut) {}
Expand All @@ -357,3 +456,115 @@ impl Message for () {
}
fn clear(&mut self) {}
}

/// `google.protobuf.Empty`
impl Name for () {
const NAME: &'static str = "Empty";
const PACKAGE: &'static str = "google.protobuf";

fn type_url() -> String {
googleapis_type_url_for::<Self>()
}
}

/// Compute the type URL for the given `google.protobuf` type, using `type.googleapis.com` as the
/// authority for the URL.
fn googleapis_type_url_for<T: Name>() -> String {
format!("type.googleapis.com/{}.{}", T::PACKAGE, T::NAME)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_impl_name() {
assert_eq!("BoolValue", bool::NAME);
assert_eq!("google.protobuf", bool::PACKAGE);
assert_eq!("google.protobuf.BoolValue", bool::full_name());
assert_eq!(
"type.googleapis.com/google.protobuf.BoolValue",
bool::type_url()
);

assert_eq!("UInt32Value", u32::NAME);
assert_eq!("google.protobuf", u32::PACKAGE);
assert_eq!("google.protobuf.UInt32Value", u32::full_name());
assert_eq!(
"type.googleapis.com/google.protobuf.UInt32Value",
u32::type_url()
);

assert_eq!("UInt64Value", u64::NAME);
assert_eq!("google.protobuf", u64::PACKAGE);
assert_eq!("google.protobuf.UInt64Value", u64::full_name());
assert_eq!(
"type.googleapis.com/google.protobuf.UInt64Value",
u64::type_url()
);

assert_eq!("Int32Value", i32::NAME);
assert_eq!("google.protobuf", i32::PACKAGE);
assert_eq!("google.protobuf.Int32Value", i32::full_name());
assert_eq!(
"type.googleapis.com/google.protobuf.Int32Value",
i32::type_url()
);

assert_eq!("Int64Value", i64::NAME);
assert_eq!("google.protobuf", i64::PACKAGE);
assert_eq!("google.protobuf.Int64Value", i64::full_name());
assert_eq!(
"type.googleapis.com/google.protobuf.Int64Value",
i64::type_url()
);

assert_eq!("FloatValue", f32::NAME);
assert_eq!("google.protobuf", f32::PACKAGE);
assert_eq!("google.protobuf.FloatValue", f32::full_name());
assert_eq!(
"type.googleapis.com/google.protobuf.FloatValue",
f32::type_url()
);

assert_eq!("DoubleValue", f64::NAME);
assert_eq!("google.protobuf", f64::PACKAGE);
assert_eq!("google.protobuf.DoubleValue", f64::full_name());
assert_eq!(
"type.googleapis.com/google.protobuf.DoubleValue",
f64::type_url()
);

assert_eq!("StringValue", String::NAME);
assert_eq!("google.protobuf", String::PACKAGE);
assert_eq!("google.protobuf.StringValue", String::full_name());
assert_eq!(
"type.googleapis.com/google.protobuf.StringValue",
String::type_url()
);

assert_eq!("BytesValue", Vec::<u8>::NAME);
assert_eq!("google.protobuf", Vec::<u8>::PACKAGE);
assert_eq!("google.protobuf.BytesValue", Vec::<u8>::full_name());
assert_eq!(
"type.googleapis.com/google.protobuf.BytesValue",
Vec::<u8>::type_url()
);

assert_eq!("BytesValue", Bytes::NAME);
assert_eq!("google.protobuf", Bytes::PACKAGE);
assert_eq!("google.protobuf.BytesValue", Bytes::full_name());
assert_eq!(
"type.googleapis.com/google.protobuf.BytesValue",
Bytes::type_url()
);

assert_eq!("Empty", <()>::NAME);
assert_eq!("google.protobuf", <()>::PACKAGE);
assert_eq!("google.protobuf.Empty", <()>::full_name());
assert_eq!(
"type.googleapis.com/google.protobuf.Empty",
<()>::type_url()
);
}
}

0 comments on commit e7deb3c

Please sign in to comment.