Skip to content

Commit 54a423e

Browse files
authored
test(wkt): natural names for intXX fields (#2852)
The tests used `int` instead of `int32` and `long` instead of `int64` to workaround problems in name serialization for oneof fields. I fixed the serialization problems, but neglected to update the tests.
1 parent 7cf8cb3 commit 54a423e

File tree

3 files changed

+66
-70
lines changed

3 files changed

+66
-70
lines changed

src/wkt/tests/common/src/generated/mod.rs

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,51 +1322,51 @@ impl MessageWithComplexOneOf {
13221322
}
13231323

13241324
/// The value of [complex][crate::generated::MessageWithComplexOneOf::complex]
1325-
/// if it holds a `Int`, `None` if the field is not set or
1325+
/// if it holds a `Int32`, `None` if the field is not set or
13261326
/// holds a different branch.
1327-
pub fn int(&self) -> std::option::Option<&i32> {
1327+
pub fn int32(&self) -> std::option::Option<&i32> {
13281328
#[allow(unreachable_patterns)]
13291329
self.complex.as_ref().and_then(|v| match v {
1330-
crate::generated::message_with_complex_one_of::Complex::Int(v) => {
1330+
crate::generated::message_with_complex_one_of::Complex::Int32(v) => {
13311331
std::option::Option::Some(v)
13321332
}
13331333
_ => std::option::Option::None,
13341334
})
13351335
}
13361336

13371337
/// Sets the value of [complex][crate::generated::MessageWithComplexOneOf::complex]
1338-
/// to hold a `Int`.
1338+
/// to hold a `Int32`.
13391339
///
13401340
/// Note that all the setters affecting `complex` are
13411341
/// mutually exclusive.
1342-
pub fn set_int<T: std::convert::Into<i32>>(mut self, v: T) -> Self {
1342+
pub fn set_int32<T: std::convert::Into<i32>>(mut self, v: T) -> Self {
13431343
self.complex = std::option::Option::Some(
1344-
crate::generated::message_with_complex_one_of::Complex::Int(v.into()),
1344+
crate::generated::message_with_complex_one_of::Complex::Int32(v.into()),
13451345
);
13461346
self
13471347
}
13481348

13491349
/// The value of [complex][crate::generated::MessageWithComplexOneOf::complex]
1350-
/// if it holds a `Long`, `None` if the field is not set or
1350+
/// if it holds a `Int64`, `None` if the field is not set or
13511351
/// holds a different branch.
1352-
pub fn long(&self) -> std::option::Option<&i64> {
1352+
pub fn int64(&self) -> std::option::Option<&i64> {
13531353
#[allow(unreachable_patterns)]
13541354
self.complex.as_ref().and_then(|v| match v {
1355-
crate::generated::message_with_complex_one_of::Complex::Long(v) => {
1355+
crate::generated::message_with_complex_one_of::Complex::Int64(v) => {
13561356
std::option::Option::Some(v)
13571357
}
13581358
_ => std::option::Option::None,
13591359
})
13601360
}
13611361

13621362
/// Sets the value of [complex][crate::generated::MessageWithComplexOneOf::complex]
1363-
/// to hold a `Long`.
1363+
/// to hold a `Int64`.
13641364
///
13651365
/// Note that all the setters affecting `complex` are
13661366
/// mutually exclusive.
1367-
pub fn set_long<T: std::convert::Into<i64>>(mut self, v: T) -> Self {
1367+
pub fn set_int64<T: std::convert::Into<i64>>(mut self, v: T) -> Self {
13681368
self.complex = std::option::Option::Some(
1369-
crate::generated::message_with_complex_one_of::Complex::Long(v.into()),
1369+
crate::generated::message_with_complex_one_of::Complex::Int64(v.into()),
13701370
);
13711371
self
13721372
}
@@ -1540,8 +1540,8 @@ impl<'de> serde::de::Deserialize<'de> for MessageWithComplexOneOf {
15401540
__string_value,
15411541
__float_value,
15421542
__double_value,
1543-
__int,
1544-
__long,
1543+
__int32,
1544+
__int64,
15451545
__enum,
15461546
__inner,
15471547
__duration,
@@ -1578,8 +1578,8 @@ impl<'de> serde::de::Deserialize<'de> for MessageWithComplexOneOf {
15781578
"float_value" => Ok(__FieldTag::__float_value),
15791579
"doubleValue" => Ok(__FieldTag::__double_value),
15801580
"double_value" => Ok(__FieldTag::__double_value),
1581-
"int" => Ok(__FieldTag::__int),
1582-
"long" => Ok(__FieldTag::__long),
1581+
"int32" => Ok(__FieldTag::__int32),
1582+
"int64" => Ok(__FieldTag::__int64),
15831583
"enum" => Ok(__FieldTag::__enum),
15841584
"inner" => Ok(__FieldTag::__inner),
15851585
"duration" => Ok(__FieldTag::__duration),
@@ -1749,10 +1749,10 @@ impl<'de> serde::de::Deserialize<'de> for MessageWithComplexOneOf {
17491749
),
17501750
);
17511751
}
1752-
__FieldTag::__int => {
1753-
if !fields.insert(__FieldTag::__int) {
1752+
__FieldTag::__int32 => {
1753+
if !fields.insert(__FieldTag::__int32) {
17541754
return std::result::Result::Err(A::Error::duplicate_field(
1755-
"multiple values for int",
1755+
"multiple values for int32",
17561756
));
17571757
}
17581758
struct __With(std::option::Option<i32>);
@@ -1768,19 +1768,19 @@ impl<'de> serde::de::Deserialize<'de> for MessageWithComplexOneOf {
17681768
}
17691769
if result.complex.is_some() {
17701770
return std::result::Result::Err(A::Error::duplicate_field(
1771-
"multiple values for `complex`, a oneof with full ID .google.rust.sdk.test.MessageWithComplexOneOf.int, latest field was int",
1771+
"multiple values for `complex`, a oneof with full ID .google.rust.sdk.test.MessageWithComplexOneOf.int32, latest field was int32",
17721772
));
17731773
}
17741774
result.complex = std::option::Option::Some(
1775-
crate::generated::message_with_complex_one_of::Complex::Int(
1775+
crate::generated::message_with_complex_one_of::Complex::Int32(
17761776
map.next_value::<__With>()?.0.unwrap_or_default(),
17771777
),
17781778
);
17791779
}
1780-
__FieldTag::__long => {
1781-
if !fields.insert(__FieldTag::__long) {
1780+
__FieldTag::__int64 => {
1781+
if !fields.insert(__FieldTag::__int64) {
17821782
return std::result::Result::Err(A::Error::duplicate_field(
1783-
"multiple values for long",
1783+
"multiple values for int64",
17841784
));
17851785
}
17861786
struct __With(std::option::Option<i64>);
@@ -1796,11 +1796,11 @@ impl<'de> serde::de::Deserialize<'de> for MessageWithComplexOneOf {
17961796
}
17971797
if result.complex.is_some() {
17981798
return std::result::Result::Err(A::Error::duplicate_field(
1799-
"multiple values for `complex`, a oneof with full ID .google.rust.sdk.test.MessageWithComplexOneOf.long, latest field was long",
1799+
"multiple values for `complex`, a oneof with full ID .google.rust.sdk.test.MessageWithComplexOneOf.int64, latest field was int64",
18001800
));
18011801
}
18021802
result.complex = std::option::Option::Some(
1803-
crate::generated::message_with_complex_one_of::Complex::Long(
1803+
crate::generated::message_with_complex_one_of::Complex::Int64(
18041804
map.next_value::<__With>()?.0.unwrap_or_default(),
18051805
),
18061806
);
@@ -1980,7 +1980,7 @@ impl serde::ser::Serialize for MessageWithComplexOneOf {
19801980
}
19811981
state.serialize_entry("doubleValue", &__With(value))?;
19821982
}
1983-
if let Some(value) = self.int() {
1983+
if let Some(value) = self.int32() {
19841984
struct __With<'a>(&'a i32);
19851985
impl<'a> serde::ser::Serialize for __With<'a> {
19861986
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
@@ -1990,9 +1990,9 @@ impl serde::ser::Serialize for MessageWithComplexOneOf {
19901990
serde_with::As::<wkt::internal::I32>::serialize(self.0, serializer)
19911991
}
19921992
}
1993-
state.serialize_entry("int", &__With(value))?;
1993+
state.serialize_entry("int32", &__With(value))?;
19941994
}
1995-
if let Some(value) = self.long() {
1995+
if let Some(value) = self.int64() {
19961996
struct __With<'a>(&'a i64);
19971997
impl<'a> serde::ser::Serialize for __With<'a> {
19981998
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
@@ -2002,7 +2002,7 @@ impl serde::ser::Serialize for MessageWithComplexOneOf {
20022002
serde_with::As::<wkt::internal::I64>::serialize(self.0, serializer)
20032003
}
20042004
}
2005-
state.serialize_entry("long", &__With(value))?;
2005+
state.serialize_entry("int64", &__With(value))?;
20062006
}
20072007
if let Some(value) = self.r#enum() {
20082008
state.serialize_entry("enum", value)?;
@@ -2329,10 +2329,8 @@ pub mod message_with_complex_one_of {
23292329
StringValue(std::string::String),
23302330
FloatValue(f32),
23312331
DoubleValue(f64),
2332-
/// TODO(#2382) - the natural name does not work.
2333-
Int(i32),
2334-
/// TODO(#2382) - the natural name does not work.
2335-
Long(i64),
2332+
Int32(i32),
2333+
Int64(i64),
23362334
Enum(crate::generated::message_with_complex_one_of::TestEnum),
23372335
Inner(std::boxed::Box<crate::generated::message_with_complex_one_of::Inner>),
23382336
Duration(std::boxed::Box<wkt::Duration>),

src/wkt/tests/complex_oneofs.rs

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ mod tests {
4242
#[test_case(MessageWithComplexOneOf::new().set_double_value(1.5), json!({"doubleValue": 1.5}))]
4343
#[test_case(MessageWithComplexOneOf::new().set_double_value(2.5), json!({"doubleValue": 2.5}))]
4444
#[test_case(MessageWithComplexOneOf::new().set_double_value(3.0), json!({"doubleValue": 3.0}))]
45-
#[test_case(MessageWithComplexOneOf::new().set_int(0), json!({"int": 0}))]
46-
#[test_case(MessageWithComplexOneOf::new().set_int(1), json!({"int": 1}))]
47-
#[test_case(MessageWithComplexOneOf::new().set_int(2), json!({"int": 2}))]
48-
#[test_case(MessageWithComplexOneOf::new().set_int(3), json!({"int": 3}))]
49-
#[test_case(MessageWithComplexOneOf::new().set_long(0), json!({"long": "0"}))]
50-
#[test_case(MessageWithComplexOneOf::new().set_long(1), json!({"long": "1"}))]
51-
#[test_case(MessageWithComplexOneOf::new().set_long(2), json!({"long": "2"}))]
52-
#[test_case(MessageWithComplexOneOf::new().set_long(3), json!({"long": "3"}))]
45+
#[test_case(MessageWithComplexOneOf::new().set_int32(0), json!({"int32": 0}))]
46+
#[test_case(MessageWithComplexOneOf::new().set_int32(1), json!({"int32": 1}))]
47+
#[test_case(MessageWithComplexOneOf::new().set_int32(2), json!({"int32": 2}))]
48+
#[test_case(MessageWithComplexOneOf::new().set_int32(3), json!({"int32": 3}))]
49+
#[test_case(MessageWithComplexOneOf::new().set_int64(0), json!({"int64": "0"}))]
50+
#[test_case(MessageWithComplexOneOf::new().set_int64(1), json!({"int64": "1"}))]
51+
#[test_case(MessageWithComplexOneOf::new().set_int64(2), json!({"int64": "2"}))]
52+
#[test_case(MessageWithComplexOneOf::new().set_int64(3), json!({"int64": "3"}))]
5353
#[test_case(MessageWithComplexOneOf::new().set_enum(TestEnum::default()), json!({"enum": 0}))]
5454
#[test_case(MessageWithComplexOneOf::new().set_inner(Inner::default().set_strings(["a", "b"])), json!({"inner": {"strings": ["a", "b"]}}))]
5555
#[test_case(MessageWithComplexOneOf::new().set_duration(Duration::clamp(-1, -750_000_000)), json!({"duration": "-1.75s"}))]
@@ -76,14 +76,14 @@ mod tests {
7676
#[test_case(MessageWithComplexOneOf::new().set_double_value(1.5), json!({"doubleValue": "1.5"}))]
7777
#[test_case(MessageWithComplexOneOf::new().set_double_value(2.5), json!({"doubleValue": 2.5}))]
7878
#[test_case(MessageWithComplexOneOf::new().set_double_value(3.0), json!({"doubleValue": 3}))]
79-
#[test_case(MessageWithComplexOneOf::new().set_int(0), json!({"int": 0}))]
80-
#[test_case(MessageWithComplexOneOf::new().set_int(1), json!({"int": "1"}))]
81-
#[test_case(MessageWithComplexOneOf::new().set_int(2), json!({"int": 2.0}))]
82-
#[test_case(MessageWithComplexOneOf::new().set_int(3), json!({"int": 3e0}))]
83-
#[test_case(MessageWithComplexOneOf::new().set_long(0), json!({"long": 0}))]
84-
#[test_case(MessageWithComplexOneOf::new().set_long(1), json!({"long": "1"}))]
85-
#[test_case(MessageWithComplexOneOf::new().set_long(2), json!({"long": 2.0}))]
86-
#[test_case(MessageWithComplexOneOf::new().set_long(3), json!({"long": 3e0}))]
79+
#[test_case(MessageWithComplexOneOf::new().set_int32(0), json!({"int32": 0}))]
80+
#[test_case(MessageWithComplexOneOf::new().set_int32(1), json!({"int32": "1"}))]
81+
#[test_case(MessageWithComplexOneOf::new().set_int32(2), json!({"int32": 2.0}))]
82+
#[test_case(MessageWithComplexOneOf::new().set_int32(3), json!({"int32": 3e0}))]
83+
#[test_case(MessageWithComplexOneOf::new().set_int64(0), json!({"int64": 0}))]
84+
#[test_case(MessageWithComplexOneOf::new().set_int64(1), json!({"int64": "1"}))]
85+
#[test_case(MessageWithComplexOneOf::new().set_int64(2), json!({"int64": 2.0}))]
86+
#[test_case(MessageWithComplexOneOf::new().set_int64(3), json!({"int64": 3e0}))]
8787
#[test_case(MessageWithComplexOneOf::new().set_enum(TestEnum::default()), json!({"enum": 0}))]
8888
#[test_case(MessageWithComplexOneOf::new().set_inner(Inner::default().set_strings(["a", "b"])), json!({"inner": {"strings": ["a", "b"]}}))]
8989
#[test_case(MessageWithComplexOneOf::new().set_duration(Duration::clamp(-1, -750_000_000)), json!({"duration": "-1.75s"}))]
@@ -107,8 +107,8 @@ mod tests {
107107
#[test_case(r#"{"string_value": null}"#, MessageWithComplexOneOf::new().set_string_value(""))]
108108
#[test_case(r#"{"float_value": null}"#, MessageWithComplexOneOf::new().set_float_value(0_f32))]
109109
#[test_case(r#"{"double_value": null}"#, MessageWithComplexOneOf::new().set_double_value(0_f64))]
110-
#[test_case(r#"{"int": null}"#, MessageWithComplexOneOf::new().set_int(0))]
111-
#[test_case(r#"{"long": null}"#, MessageWithComplexOneOf::new().set_long(0_i64))]
110+
#[test_case(r#"{"int32": null}"#, MessageWithComplexOneOf::new().set_int32(0))]
111+
#[test_case(r#"{"int64": null}"#, MessageWithComplexOneOf::new().set_int64(0_i64))]
112112
#[test_case(r#"{"enum": null}"#, MessageWithComplexOneOf::new().set_enum(TestEnum::default()))]
113113
#[test_case(r#"{"inner": null}"#, MessageWithComplexOneOf::new().set_inner(Inner::default()))]
114114
#[test_case(r#"{"duration": null}"#, MessageWithComplexOneOf::new().set_duration(Duration::default()))]
@@ -131,10 +131,10 @@ mod tests {
131131
#[test_case(r#"{"floatValue": 0, "floatValue": 0}"#)]
132132
#[test_case(r#"{"null": null, "doubleValue": 0}"#)]
133133
#[test_case(r#"{"doubleValue": 0, "doubleValue": 0}"#)]
134-
#[test_case(r#"{"null": null, "int": 0}"#)]
135-
#[test_case(r#"{"int": 0, "int": 0}"#)]
136-
#[test_case(r#"{"null": null, "long": 0}"#)]
137-
#[test_case(r#"{"long": 0, "long": 0}"#)]
134+
#[test_case(r#"{"null": null, "int32": 0}"#)]
135+
#[test_case(r#"{"int32": 0, "int32": 0}"#)]
136+
#[test_case(r#"{"null": null, "int64": 0}"#)]
137+
#[test_case(r#"{"int64": 0, "int64": 0}"#)]
138138
#[test_case(r#"{"null": null, "enum": "BLACK"}"#)]
139139
#[test_case(r#"{"enum": "BLACK", "enum": "BLACK"}"#)]
140140
#[test_case(r#"{"null": null, "inner": {}}"#)]
@@ -226,25 +226,25 @@ mod tests {
226226
Ok(())
227227
}
228228

229-
#[test_case(json!({"int": 0}), 0)]
230-
#[test_case(json!({"int": "0"}), 0; "0 as str")]
231-
#[test_case(json!({"int": 1}), 1)]
232-
#[test_case(json!({"int": "1"}), 1; "1 as str")]
229+
#[test_case(json!({"int32": 0}), 0)]
230+
#[test_case(json!({"int32": "0"}), 0; "0 as str")]
231+
#[test_case(json!({"int32": 1}), 1)]
232+
#[test_case(json!({"int32": "1"}), 1; "1 as str")]
233233
fn test_int(input: Value, want: i32) -> Result {
234234
let got = serde_json::from_value::<MessageWithComplexOneOf>(input)?;
235-
assert_eq!(got.int(), Some(&want), "{got:?}");
235+
assert_eq!(got.int32(), Some(&want), "{got:?}");
236236
let roundtrip = serde_json::from_value(serde_json::to_value(&got)?)?;
237237
assert_eq!(got, roundtrip);
238238
Ok(())
239239
}
240240

241-
#[test_case(json!({"long": 0}), 0)]
242-
#[test_case(json!({"long": "0"}), 0; "0 as str")]
243-
#[test_case(json!({"long": 1}), 1)]
244-
#[test_case(json!({"long": "1"}), 1; "1 as str")]
241+
#[test_case(json!({"int64": 0}), 0)]
242+
#[test_case(json!({"int64": "0"}), 0; "0 as str")]
243+
#[test_case(json!({"int64": 1}), 1)]
244+
#[test_case(json!({"int64": "1"}), 1; "1 as str")]
245245
fn test_long(input: Value, want: i64) -> Result {
246246
let got = serde_json::from_value::<MessageWithComplexOneOf>(input)?;
247-
assert_eq!(got.long(), Some(&want), "{got:?}");
247+
assert_eq!(got.int64(), Some(&want), "{got:?}");
248248
let roundtrip = serde_json::from_value(serde_json::to_value(&got)?)?;
249249
assert_eq!(got, roundtrip);
250250
Ok(())

src/wkt/tests/protos/oneofs.proto

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,8 @@ message MessageWithComplexOneOf {
5151
string string_value = 4;
5252
float float_value = 5;
5353
double double_value = 6;
54-
// TODO(#2382) - the natural name does not work.
55-
int32 int = 7;
56-
// TODO(#2382) - the natural name does not work.
57-
int64 long = 8;
54+
int32 int32 = 7;
55+
int64 int64 = 8;
5856
TestEnum enum = 9;
5957
Inner inner = 10;
6058
google.protobuf.Duration duration = 11;

0 commit comments

Comments
 (0)