@@ -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
313375func 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}`
0 commit comments