Skip to content

Commit e7deb3c

Browse files
feat: impl Name for Protobuf well-known wrapper types (#1174)
Co-authored-by: Kelvin Hu <[email protected]>
1 parent 6d3a7ab commit e7deb3c

File tree

1 file changed

+214
-3
lines changed

1 file changed

+214
-3
lines changed

prost/src/types.rs

Lines changed: 214 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
//! the `prost-types` crate in order to avoid a cyclic dependency between `prost` and
66
//! `prost-build`.
77
8-
#[cfg(not(feature = "std"))]
8+
use alloc::format;
99
use alloc::string::String;
10-
#[cfg(not(feature = "std"))]
1110
use alloc::vec::Vec;
1211

1312
use ::bytes::{Buf, BufMut, Bytes};
@@ -17,7 +16,7 @@ use crate::{
1716
encoding::{
1817
bool, bytes, double, float, int32, int64, skip_field, string, uint32, uint64, DecodeContext,
1918
},
20-
DecodeError, Message,
19+
DecodeError, Message, Name,
2120
};
2221

2322
/// `google.protobuf.BoolValue`
@@ -52,6 +51,16 @@ impl Message for bool {
5251
}
5352
}
5453

54+
/// `google.protobuf.BoolValue`
55+
impl Name for bool {
56+
const NAME: &'static str = "BoolValue";
57+
const PACKAGE: &'static str = "google.protobuf";
58+
59+
fn type_url() -> String {
60+
googleapis_type_url_for::<Self>()
61+
}
62+
}
63+
5564
/// `google.protobuf.UInt32Value`
5665
impl Message for u32 {
5766
fn encode_raw(&self, buf: &mut impl BufMut) {
@@ -84,6 +93,16 @@ impl Message for u32 {
8493
}
8594
}
8695

96+
/// `google.protobuf.UInt32Value`
97+
impl Name for u32 {
98+
const NAME: &'static str = "UInt32Value";
99+
const PACKAGE: &'static str = "google.protobuf";
100+
101+
fn type_url() -> String {
102+
googleapis_type_url_for::<Self>()
103+
}
104+
}
105+
87106
/// `google.protobuf.UInt64Value`
88107
impl Message for u64 {
89108
fn encode_raw(&self, buf: &mut impl BufMut) {
@@ -116,6 +135,16 @@ impl Message for u64 {
116135
}
117136
}
118137

138+
/// `google.protobuf.UInt64Value`
139+
impl Name for u64 {
140+
const NAME: &'static str = "UInt64Value";
141+
const PACKAGE: &'static str = "google.protobuf";
142+
143+
fn type_url() -> String {
144+
googleapis_type_url_for::<Self>()
145+
}
146+
}
147+
119148
/// `google.protobuf.Int32Value`
120149
impl Message for i32 {
121150
fn encode_raw(&self, buf: &mut impl BufMut) {
@@ -148,6 +177,16 @@ impl Message for i32 {
148177
}
149178
}
150179

180+
/// `google.protobuf.Int32Value`
181+
impl Name for i32 {
182+
const NAME: &'static str = "Int32Value";
183+
const PACKAGE: &'static str = "google.protobuf";
184+
185+
fn type_url() -> String {
186+
googleapis_type_url_for::<Self>()
187+
}
188+
}
189+
151190
/// `google.protobuf.Int64Value`
152191
impl Message for i64 {
153192
fn encode_raw(&self, buf: &mut impl BufMut) {
@@ -180,6 +219,16 @@ impl Message for i64 {
180219
}
181220
}
182221

222+
/// `google.protobuf.Int64Value`
223+
impl Name for i64 {
224+
const NAME: &'static str = "Int64Value";
225+
const PACKAGE: &'static str = "google.protobuf";
226+
227+
fn type_url() -> String {
228+
googleapis_type_url_for::<Self>()
229+
}
230+
}
231+
183232
/// `google.protobuf.FloatValue`
184233
impl Message for f32 {
185234
fn encode_raw(&self, buf: &mut impl BufMut) {
@@ -212,6 +261,16 @@ impl Message for f32 {
212261
}
213262
}
214263

264+
/// `google.protobuf.FloatValue`
265+
impl Name for f32 {
266+
const NAME: &'static str = "FloatValue";
267+
const PACKAGE: &'static str = "google.protobuf";
268+
269+
fn type_url() -> String {
270+
googleapis_type_url_for::<Self>()
271+
}
272+
}
273+
215274
/// `google.protobuf.DoubleValue`
216275
impl Message for f64 {
217276
fn encode_raw(&self, buf: &mut impl BufMut) {
@@ -244,6 +303,16 @@ impl Message for f64 {
244303
}
245304
}
246305

306+
/// `google.protobuf.DoubleValue`
307+
impl Name for f64 {
308+
const NAME: &'static str = "DoubleValue";
309+
const PACKAGE: &'static str = "google.protobuf";
310+
311+
fn type_url() -> String {
312+
googleapis_type_url_for::<Self>()
313+
}
314+
}
315+
247316
/// `google.protobuf.StringValue`
248317
impl Message for String {
249318
fn encode_raw(&self, buf: &mut impl BufMut) {
@@ -276,6 +345,16 @@ impl Message for String {
276345
}
277346
}
278347

348+
/// `google.protobuf.StringValue`
349+
impl Name for String {
350+
const NAME: &'static str = "StringValue";
351+
const PACKAGE: &'static str = "google.protobuf";
352+
353+
fn type_url() -> String {
354+
googleapis_type_url_for::<Self>()
355+
}
356+
}
357+
279358
/// `google.protobuf.BytesValue`
280359
impl Message for Vec<u8> {
281360
fn encode_raw(&self, buf: &mut impl BufMut) {
@@ -308,6 +387,16 @@ impl Message for Vec<u8> {
308387
}
309388
}
310389

390+
/// `google.protobuf.BytesValue`
391+
impl Name for Vec<u8> {
392+
const NAME: &'static str = "BytesValue";
393+
const PACKAGE: &'static str = "google.protobuf";
394+
395+
fn type_url() -> String {
396+
googleapis_type_url_for::<Self>()
397+
}
398+
}
399+
311400
/// `google.protobuf.BytesValue`
312401
impl Message for Bytes {
313402
fn encode_raw(&self, buf: &mut impl BufMut) {
@@ -340,6 +429,16 @@ impl Message for Bytes {
340429
}
341430
}
342431

432+
/// `google.protobuf.BytesValue`
433+
impl Name for Bytes {
434+
const NAME: &'static str = "BytesValue";
435+
const PACKAGE: &'static str = "google.protobuf";
436+
437+
fn type_url() -> String {
438+
googleapis_type_url_for::<Self>()
439+
}
440+
}
441+
343442
/// `google.protobuf.Empty`
344443
impl Message for () {
345444
fn encode_raw(&self, _buf: &mut impl BufMut) {}
@@ -357,3 +456,115 @@ impl Message for () {
357456
}
358457
fn clear(&mut self) {}
359458
}
459+
460+
/// `google.protobuf.Empty`
461+
impl Name for () {
462+
const NAME: &'static str = "Empty";
463+
const PACKAGE: &'static str = "google.protobuf";
464+
465+
fn type_url() -> String {
466+
googleapis_type_url_for::<Self>()
467+
}
468+
}
469+
470+
/// Compute the type URL for the given `google.protobuf` type, using `type.googleapis.com` as the
471+
/// authority for the URL.
472+
fn googleapis_type_url_for<T: Name>() -> String {
473+
format!("type.googleapis.com/{}.{}", T::PACKAGE, T::NAME)
474+
}
475+
476+
#[cfg(test)]
477+
mod tests {
478+
use super::*;
479+
480+
#[test]
481+
fn test_impl_name() {
482+
assert_eq!("BoolValue", bool::NAME);
483+
assert_eq!("google.protobuf", bool::PACKAGE);
484+
assert_eq!("google.protobuf.BoolValue", bool::full_name());
485+
assert_eq!(
486+
"type.googleapis.com/google.protobuf.BoolValue",
487+
bool::type_url()
488+
);
489+
490+
assert_eq!("UInt32Value", u32::NAME);
491+
assert_eq!("google.protobuf", u32::PACKAGE);
492+
assert_eq!("google.protobuf.UInt32Value", u32::full_name());
493+
assert_eq!(
494+
"type.googleapis.com/google.protobuf.UInt32Value",
495+
u32::type_url()
496+
);
497+
498+
assert_eq!("UInt64Value", u64::NAME);
499+
assert_eq!("google.protobuf", u64::PACKAGE);
500+
assert_eq!("google.protobuf.UInt64Value", u64::full_name());
501+
assert_eq!(
502+
"type.googleapis.com/google.protobuf.UInt64Value",
503+
u64::type_url()
504+
);
505+
506+
assert_eq!("Int32Value", i32::NAME);
507+
assert_eq!("google.protobuf", i32::PACKAGE);
508+
assert_eq!("google.protobuf.Int32Value", i32::full_name());
509+
assert_eq!(
510+
"type.googleapis.com/google.protobuf.Int32Value",
511+
i32::type_url()
512+
);
513+
514+
assert_eq!("Int64Value", i64::NAME);
515+
assert_eq!("google.protobuf", i64::PACKAGE);
516+
assert_eq!("google.protobuf.Int64Value", i64::full_name());
517+
assert_eq!(
518+
"type.googleapis.com/google.protobuf.Int64Value",
519+
i64::type_url()
520+
);
521+
522+
assert_eq!("FloatValue", f32::NAME);
523+
assert_eq!("google.protobuf", f32::PACKAGE);
524+
assert_eq!("google.protobuf.FloatValue", f32::full_name());
525+
assert_eq!(
526+
"type.googleapis.com/google.protobuf.FloatValue",
527+
f32::type_url()
528+
);
529+
530+
assert_eq!("DoubleValue", f64::NAME);
531+
assert_eq!("google.protobuf", f64::PACKAGE);
532+
assert_eq!("google.protobuf.DoubleValue", f64::full_name());
533+
assert_eq!(
534+
"type.googleapis.com/google.protobuf.DoubleValue",
535+
f64::type_url()
536+
);
537+
538+
assert_eq!("StringValue", String::NAME);
539+
assert_eq!("google.protobuf", String::PACKAGE);
540+
assert_eq!("google.protobuf.StringValue", String::full_name());
541+
assert_eq!(
542+
"type.googleapis.com/google.protobuf.StringValue",
543+
String::type_url()
544+
);
545+
546+
assert_eq!("BytesValue", Vec::<u8>::NAME);
547+
assert_eq!("google.protobuf", Vec::<u8>::PACKAGE);
548+
assert_eq!("google.protobuf.BytesValue", Vec::<u8>::full_name());
549+
assert_eq!(
550+
"type.googleapis.com/google.protobuf.BytesValue",
551+
Vec::<u8>::type_url()
552+
);
553+
554+
assert_eq!("BytesValue", Bytes::NAME);
555+
assert_eq!("google.protobuf", Bytes::PACKAGE);
556+
assert_eq!("google.protobuf.BytesValue", Bytes::full_name());
557+
assert_eq!(
558+
"type.googleapis.com/google.protobuf.BytesValue",
559+
Bytes::type_url()
560+
);
561+
562+
assert_eq!("Empty", <()>::NAME);
563+
assert_eq!("google.protobuf", <()>::PACKAGE);
564+
assert_eq!("google.protobuf.Empty", <()>::full_name());
565+
assert_eq!(
566+
"type.googleapis.com/google.protobuf.Empty",
567+
<()>::type_url()
568+
);
569+
}
570+
}

0 commit comments

Comments
 (0)