Skip to content

Commit 2acbf75

Browse files
authored
cleanup(internal/sidekick/rust): qualify FromProto calls (#7909)
Generated FromProto impls, and the LRO Any conversion in the gRPC transport, passed v.cnv() to functions generic over Into<FieldType>, so the target type was only inferred when the prost type had exactly one FromProto impl. Adding a second impl for a shared prost type, such as prost_types::Timestamp, in gax-internal can brake generated gRPC crates. Call gaxi::prost::FromProto::<FieldType>::cnv explicitly. Boxed recursive fields now dereference the box first, and enum fields (i32 in prost) convert with the enum's From<i32> impl, matching the existing repeated enum branch. For googleapis/google-cloud-rust#7284
1 parent 915e4f0 commit 2acbf75

4 files changed

Lines changed: 93 additions & 21 deletions

File tree

‎internal/sidekick/rust/generate_convert_test.go‎

Lines changed: 75 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -205,15 +205,57 @@ impl gaxi::prost::FromProto<crate::model::ComplexMessage> for ComplexMessage {
205205
fn cnv(self) -> std::result::Result<crate::model::ComplexMessage, gaxi::prost::ConvertError> {
206206
Ok(
207207
crate::model::ComplexMessage::new()
208-
.set_or_clear_opt_child(self.opt_child.map(|v| v.cnv()).transpose()?)
209-
.set_or_clear_parent(self.parent.map(|v| v.cnv()).transpose()?)
210-
.set_tags(self.tags.into_iter().map(|v| v.cnv())
208+
.set_or_clear_opt_child(self.opt_child.map(gaxi::prost::FromProto::<crate::model::complex_message::Child>::cnv).transpose()?)
209+
.set_or_clear_parent(self.parent.map(|v| gaxi::prost::FromProto::<crate::model::ComplexMessage>::cnv(*v)).transpose()?)
210+
.set_tags(self.tags.into_iter().map(gaxi::prost::FromProto::<std::string::String>::cnv)
211211
.collect::<std::result::Result<std::vec::Vec<_>, _>>()?)
212212
.set_labels(self.labels.into_iter()
213213
.map(|(k, v)| {
214-
gaxi::prost::pair_transpose(k.cnv(), v.cnv())
214+
gaxi::prost::pair_transpose(gaxi::prost::FromProto::<std::string::String>::cnv(k), gaxi::prost::FromProto::<i32>::cnv(v))
215+
}).collect::<std::result::Result<std::collections::HashMap<_, _>, _>>()?)
216+
.set_choice(self.choice.map(gaxi::prost::FromProto::<crate::model::complex_message::Choice>::cnv).transpose()?)
217+
)
218+
}
219+
}`,
220+
},
221+
{
222+
name: "message with optional enum and enum-valued map",
223+
makeMessage: func() (*api.Message, []string) {
224+
enum := api.NewTestEnum("State").WithValues(api.NewTestEnumValue("STATE_ACTIVE", 1))
225+
msg := api.NewTestMessage("EnumMessage").WithPackage("test.v1").WithEnums(enum)
226+
mapType := api.NewTestMapWithFields("$Map",
227+
api.NewTestField("key").WithType(api.TypezString),
228+
api.NewTestField("value").WithEnumType(enum),
229+
)
230+
msg.WithFields(
231+
api.NewTestField("opt_state").WithEnumType(enum).WithOptional(),
232+
api.NewTestField("states").WithMapType(mapType),
233+
)
234+
return msg, nil
235+
},
236+
want: `impl gaxi::prost::ToProto<EnumMessage> for crate::model::EnumMessage {
237+
type Output = EnumMessage;
238+
fn to_proto(self) -> std::result::Result<EnumMessage, gaxi::prost::ConvertError> {
239+
Ok(Self::Output {
240+
opt_state: self.opt_state.map(gaxi::prost::ToProto::<enum_message::State>::to_proto).transpose()?,
241+
states: self.states
242+
.into_iter()
243+
.map(|(k, v)| {
244+
gaxi::prost::pair_transpose(gaxi::prost::ToProto::<std::string::String>::to_proto(k), gaxi::prost::ToProto::<enum_message::State>::to_proto(v))
245+
}).collect::<std::result::Result<std::collections::HashMap<_, _>, _>>()?,
246+
})
247+
}
248+
}
249+
250+
impl gaxi::prost::FromProto<crate::model::EnumMessage> for EnumMessage {
251+
fn cnv(self) -> std::result::Result<crate::model::EnumMessage, gaxi::prost::ConvertError> {
252+
Ok(
253+
crate::model::EnumMessage::new()
254+
.set_or_clear_opt_state(self.opt_state.map(crate::model::enum_message::State::from))
255+
.set_states(self.states.into_iter()
256+
.map(|(k, v)| {
257+
gaxi::prost::pair_transpose(gaxi::prost::FromProto::<std::string::String>::cnv(k), Ok(crate::model::enum_message::State::from(v)))
215258
}).collect::<std::result::Result<std::collections::HashMap<_, _>, _>>()?)
216-
.set_choice(self.choice.map(|v| v.cnv()).transpose()?)
217259
)
218260
}
219261
}`,
@@ -293,8 +335,10 @@ func TestGenerateConvertOneOf(t *testing.T) {
293335
if err != nil {
294336
t.Fatal(err)
295337
}
338+
contentStr := string(contents)
296339

297-
wantToProto := `impl gaxi::prost::ToProto<type_schema::Schema> for crate::model::type_schema::Schema {
340+
t.Run("ToProto", func(t *testing.T) {
341+
want := `impl gaxi::prost::ToProto<type_schema::Schema> for crate::model::type_schema::Schema {
298342
type Output = type_schema::Schema;
299343
fn to_proto(self) -> std::result::Result<Self::Output, gaxi::prost::ConvertError> {
300344
match self {
@@ -304,10 +348,28 @@ func TestGenerateConvertOneOf(t *testing.T) {
304348
}
305349
}
306350
}`
307-
gotToProto := extractBlock(t, string(contents), "impl gaxi::prost::ToProto<type_schema::Schema>", "\n }\n }\n}")
308-
if diff := cmp.Diff(wantToProto, gotToProto); diff != "" {
309-
t.Errorf("mismatch ToProto (-want +got):\n%s", diff)
310-
}
351+
got := extractBlock(t, contentStr, "impl gaxi::prost::ToProto<type_schema::Schema>", "\n }\n }\n}")
352+
if diff := cmp.Diff(want, got); diff != "" {
353+
t.Errorf("mismatch (-want +got):\n%s", diff)
354+
}
355+
})
356+
357+
t.Run("FromProto", func(t *testing.T) {
358+
want := `impl gaxi::prost::FromProto<crate::model::type_schema::Schema> for type_schema::Schema {
359+
fn cnv(self) -> std::result::Result<crate::model::type_schema::Schema, gaxi::prost::ConvertError> {
360+
use crate::model::type_schema::Schema as T;
361+
match self {
362+
Self::InlineSchema(v) => Ok(T::InlineSchema(std::boxed::Box::new(gaxi::prost::FromProto::<crate::model::InlineSchema>::cnv(*v)?))),
363+
Self::ReferenceSchema(v) => Ok(T::ReferenceSchema(std::boxed::Box::new(gaxi::prost::FromProto::<crate::model::ReferenceSchema>::cnv(v)?))),
364+
Self::Name(v) => Ok(T::Name(gaxi::prost::FromProto::<std::string::String>::cnv(v)?)),
365+
}
366+
}
367+
}`
368+
got := extractBlock(t, contentStr, "impl gaxi::prost::FromProto<crate::model::type_schema::Schema>", "\n }\n }\n}")
369+
if diff := cmp.Diff(want, got); diff != "" {
370+
t.Errorf("mismatch (-want +got):\n%s", diff)
371+
}
372+
})
311373
}
312374

313375
func TestGenerateConvertAcronyms(t *testing.T) {
@@ -613,9 +675,9 @@ func TestGenerateConvertOneOf_ReservedRawIdentifiers(t *testing.T) {
613675
fn cnv(self) -> std::result::Result<crate::model::primary_product_data_source::DefaultRule, gaxi::prost::ConvertError> {
614676
use crate::model::primary_product_data_source::DefaultRule as T;
615677
match self {
616-
Self::Self_(v) => Ok(T::Self_(v.cnv()?)),
617-
Self::Crate(v) => Ok(T::Crate(v.cnv()?)),
618-
Self::Super(v) => Ok(T::Super(v.cnv()?)),
678+
Self::Self_(v) => Ok(T::Self_(gaxi::prost::FromProto::<bool>::cnv(v)?)),
679+
Self::Crate(v) => Ok(T::Crate(gaxi::prost::FromProto::<std::string::String>::cnv(v)?)),
680+
Self::Super(v) => Ok(T::Super(gaxi::prost::FromProto::<i32>::cnv(v)?)),
619681
}
620682
}
621683
}`

‎internal/sidekick/rust/templates/convert-prost/message.gotmpl‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,13 @@ impl gaxi::prost::FromProto<{{ .Codec.QualifiedName }}> for {{ .Codec.ProstRelat
8080
{{- if not .SkipProtoConversion }}
8181
{{- if .Singular }}
8282
{{- if .Optional }}
83-
.set_or_clear_{{ .Codec.SetterName }}(self.{{ .Codec.FieldName }}.map(|v| v.cnv()).transpose()?)
83+
{{- if .IsEnum }}
84+
.set_or_clear_{{ .Codec.SetterName }}(self.{{ .Codec.FieldName }}.map({{ .Codec.PrimitiveFieldType }}::from))
85+
{{- else if .Codec.MapToBoxed }}
86+
.set_or_clear_{{ .Codec.SetterName }}(self.{{ .Codec.FieldName }}.map(|v| gaxi::prost::FromProto::<{{ .Codec.PrimitiveFieldType }}>::cnv(*v)).transpose()?)
87+
{{- else }}
88+
.set_or_clear_{{ .Codec.SetterName }}(self.{{ .Codec.FieldName }}.map(gaxi::prost::FromProto::<{{ .Codec.PrimitiveFieldType }}>::cnv).transpose()?)
89+
{{- end }}
8490
{{- else if .Proto2Optional }}
8591
.set_{{ .Codec.SetterName }}(self.{{ .Codec.FieldName }}.unwrap_or_default())
8692
{{- else }}
@@ -90,19 +96,23 @@ impl gaxi::prost::FromProto<{{ .Codec.QualifiedName }}> for {{ .Codec.ProstRelat
9096
{{- if .IsEnum }}
9197
.set_{{ .Codec.SetterName }}(self.{{ .Codec.FieldName }}.into_iter().map({{ .Codec.PrimitiveFieldType }}::from))
9298
{{- else }}
93-
.set_{{ .Codec.SetterName }}(self.{{ .Codec.FieldName }}.into_iter().map(|v| v.cnv())
99+
.set_{{ .Codec.SetterName }}(self.{{ .Codec.FieldName }}.into_iter().map(gaxi::prost::FromProto::<{{ .Codec.PrimitiveFieldType }}>::cnv)
94100
.collect::<std::result::Result<std::vec::Vec<_>, _>>()?)
95101
{{- end }}
96102
{{- else if .Map }}
97103
.set_{{ .Codec.SetterName }}(self.{{ .Codec.FieldName }}.into_iter()
98104
.map(|(k, v)| {
99-
gaxi::prost::pair_transpose(k.cnv(), v.cnv())
105+
{{- if .Codec.ValueField.IsEnum }}
106+
gaxi::prost::pair_transpose(gaxi::prost::FromProto::<{{ .Codec.KeyType }}>::cnv(k), Ok({{ .Codec.ValueType }}::from(v)))
107+
{{- else }}
108+
gaxi::prost::pair_transpose(gaxi::prost::FromProto::<{{ .Codec.KeyType }}>::cnv(k), gaxi::prost::FromProto::<{{ .Codec.ValueType }}>::cnv(v))
109+
{{- end }}
100110
}).collect::<std::result::Result<std::collections::HashMap<_, _>, _>>()?)
101111
{{- end }}
102112
{{- end }}
103113
{{- end }}
104114
{{- range .OneOfs }}
105-
.set_{{ .Codec.SetterName }}(self.{{ .Codec.FieldName }}.map(|v| v.cnv()).transpose()?)
115+
.set_{{ .Codec.SetterName }}(self.{{ .Codec.FieldName }}.map(gaxi::prost::FromProto::<{{ .Codec.QualifiedName }}>::cnv).transpose()?)
106116
{{- end }}
107117
)
108118
}

‎internal/sidekick/rust/templates/convert-prost/oneof.gotmpl‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ impl gaxi::prost::FromProto<{{ .Codec.QualifiedName }}> for {{ .Codec.ProstRelat
4141
{{- else if .IsEnum }}
4242
Self::{{ .Codec.ProstBranchName }}(v) => Ok(T::{{ .Codec.BranchName }}(v.into())),
4343
{{- else if .Codec.IsBoxed }}
44-
Self::{{ .Codec.ProstBranchName }}(v) => Ok(T::{{ .Codec.BranchName }}(std::boxed::Box::new(v.cnv()?))),
44+
Self::{{ .Codec.ProstBranchName }}(v) => Ok(T::{{ .Codec.BranchName }}(std::boxed::Box::new(gaxi::prost::FromProto::<{{ .Codec.PrimitiveFieldType }}>::cnv({{- if .Codec.MapToBoxed }}*v{{- else }}v{{- end }})?))),
4545
{{- else }}
46-
Self::{{ .Codec.ProstBranchName }}(v) => Ok(T::{{ .Codec.BranchName }}(v.cnv()?)),
46+
Self::{{ .Codec.ProstBranchName }}(v) => Ok(T::{{ .Codec.BranchName }}(gaxi::prost::FromProto::<{{ .Codec.PrimitiveFieldType }}>::cnv(v)?)),
4747
{{- end }}
4848
{{- end }}
4949
}

‎internal/sidekick/rust/templates/grpc-client/transport.rs.gotmpl‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,8 @@ pub(crate) fn lro_any_from_prost(
356356
{{- range .Codec.LROTypes }}
357357
"type.googleapis.com/{{ .Codec.SourceFQN }}" => value
358358
.to_msg::<crate::{{ .Codec.PackageModuleName }}::{{ .Codec.ProstRelativeName }}>()
359-
.map_err(ConvertError::other)?
360-
.cnv()
359+
.map_err(ConvertError::other)
360+
.and_then(FromProto::<{{ .Codec.QualifiedName }}>::cnv)
361361
.and_then(|our_msg| wkt::Any::from_msg(&our_msg).map_err(ConvertError::other)),
362362
{{- end }}
363363
{{- end }}

0 commit comments

Comments
 (0)