Skip to content

Commit

Permalink
use set_is_ref and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
cataggar committed Sep 25, 2023
1 parent 4bd6a17 commit 2825e79
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
37 changes: 29 additions & 8 deletions services/autorust/codegen/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,83 +168,102 @@ impl TypeNameCode {
Ok(type_name_code)
}

pub fn new_ref(type_name: &TypeName) -> Result<Self> {
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());
Expand Down Expand Up @@ -300,6 +319,7 @@ impl TypeNameCode {
pub fn is_optional(&self) -> bool {
self.optional
}

pub fn is_union(&self) -> bool {
self.union
}
Expand Down Expand Up @@ -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(())
}
Expand Down
3 changes: 2 additions & 1 deletion services/autorust/codegen/src/codegen_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,8 @@ fn create_function_params(parameters: &[&WebParameter]) -> crate::Result<TokenSt
let body_tp = quote! { Json<#tp> };
params.push(quote! { #name: #body_tp });
} else {
let mut tp = TypeNameCode::new_ref(&param.type_name()?)?;
let mut tp = TypeNameCode::new(&param.type_name()?)?;
tp.set_is_ref(true);
tp = tp.optional(!param.required());
params.push(quote! { #name: #tp });
}
Expand Down

0 comments on commit 2825e79

Please sign in to comment.