From 2825e79e435b126750566e77a71dcc40a7496a4e Mon Sep 17 00:00:00 2001 From: Cameron Taggart Date: Mon, 25 Sep 2023 20:40:04 +0200 Subject: [PATCH] use set_is_ref and add test --- services/autorust/codegen/src/codegen.rs | 37 +++++++++++++++---- .../autorust/codegen/src/codegen_routes.rs | 3 +- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/services/autorust/codegen/src/codegen.rs b/services/autorust/codegen/src/codegen.rs index 391c630f2c..1889d40e3b 100644 --- a/services/autorust/codegen/src/codegen.rs +++ b/services/autorust/codegen/src/codegen.rs @@ -168,83 +168,102 @@ impl TypeNameCode { Ok(type_name_code) } - pub fn new_ref(type_name: &TypeName) -> Result { - let mut type_name_code = match type_name { - TypeName::String => TypeNameCode::from(tp_str()), - _ => Self::new(type_name)?, - }; - type_name_code.is_ref = true; - Ok(type_name_code) + pub fn set_is_ref(&mut self, is_ref: bool) { + self.is_ref = is_ref; } pub fn is_string(&self) -> bool { self.type_name == Some(TypeName::String) } + pub fn is_ref(&self) -> bool { self.is_ref } + pub fn is_bytes(&self) -> bool { self.type_name == Some(TypeName::Bytes) } + pub fn set_as_bytes(&mut self) { self.force_value = false; self.type_name = Some(TypeName::Bytes); self.type_path = tp_bytes(); } + pub fn is_value(&self) -> bool { self.type_name == Some(TypeName::Value) } + pub fn is_date_time(&self) -> bool { self.type_name == Some(TypeName::DateTime) } + pub fn is_date_time_rfc1123(&self) -> bool { self.type_name == Some(TypeName::DateTimeRfc1123) } + pub fn is_vec(&self) -> bool { self.vec_count > 0 && !self.force_value } + /// Forces the type to be `serde_json::Value` pub fn force_value(mut self, force_value: bool) -> Self { self.force_value = force_value; self } + pub fn optional(mut self, optional: bool) -> Self { self.optional = optional; self } + pub fn union(&mut self, union: bool) { self.union = union; } + pub fn incr_vec_count(mut self) -> Self { self.vec_count += 1; self } + pub fn impl_into(mut self, impl_into: bool) -> Self { self.impl_into = impl_into; self } + pub fn has_impl_into(&self) -> bool { self.allow_impl_into && self.impl_into } + fn allow_impl_into(mut self, allow_impl_into: bool) -> Self { self.allow_impl_into = allow_impl_into; self } + pub fn boxed(mut self, boxed: bool) -> Self { self.boxed = boxed; self } + pub fn qualify_models(mut self, qualify_models: bool) -> Self { self.qualify_models = qualify_models; self } + fn allow_qualify_models(mut self, allow_qualify_models: bool) -> Self { self.allow_qualify_models = allow_qualify_models; self } + fn type_path(&self) -> TypePath { + if self.is_string() && self.is_ref() { + return tp_str(); + } + self.type_path.clone() + } + fn to_type(&self) -> Type { - let mut tp = self.type_path.clone(); + let mut tp = self.type_path(); if self.union { if let Some(last) = tp.path.segments.last_mut() { last.ident = Ident::new(&format!("{}Union", last.ident), last.ident.span()); @@ -300,6 +319,7 @@ impl TypeNameCode { pub fn is_optional(&self) -> bool { self.optional } + pub fn is_union(&self) -> bool { self.union } @@ -506,6 +526,7 @@ mod tests { #[test] fn test_is_ref() -> Result<()> { let mut tp = TypeNameCode::try_from("farm::Goat")?; + tp.set_is_ref(true); assert_eq!("& farm :: Goat", tp.to_string()); Ok(()) } diff --git a/services/autorust/codegen/src/codegen_routes.rs b/services/autorust/codegen/src/codegen_routes.rs index 61c9f21cb7..971254e2fc 100644 --- a/services/autorust/codegen/src/codegen_routes.rs +++ b/services/autorust/codegen/src/codegen_routes.rs @@ -345,7 +345,8 @@ fn create_function_params(parameters: &[&WebParameter]) -> crate::Result }; params.push(quote! { #name: #body_tp }); } else { - let mut tp = TypeNameCode::new_ref(¶m.type_name()?)?; + let mut tp = TypeNameCode::new(¶m.type_name()?)?; + tp.set_is_ref(true); tp = tp.optional(!param.required()); params.push(quote! { #name: #tp }); }