add special handling for optional query parameters (#468)

This commit is contained in:
Adam Leventhal 2023-05-12 15:04:00 -07:00 committed by GitHub
parent b94123e5ac
commit e7c4c16d9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 2481 additions and 349 deletions

View File

@ -181,9 +181,53 @@ impl Generator {
Self(self.0.path_matches(re))
}
}
OperationParameterKind::Query(_) => quote! {
OperationParameterKind::Query(true) => quote! {
Self(self.0.query_param(#name, value.to_string()))
},
OperationParameterKind::Query(false) => {
// If the type is a ref, augment it with a lifetime that we'll also use in the function
let (lifetime, arg_type_name) =
if let syn::Type::Reference(mut rr) =
syn::parse2::<syn::Type>(arg_type_name.clone())
.unwrap()
{
rr.lifetime = Some(syn::Lifetime::new(
"'a",
proc_macro2::Span::call_site(),
));
(Some(quote! { 'a, }), rr.to_token_stream())
} else {
(None, arg_type_name)
};
return quote! {
pub fn #name_ident<#lifetime T>(
self,
value: T,
) -> Self
where
T: Into<Option<#arg_type_name>>,
{
if let Some(value) = value.into() {
Self(self.0.query_param(
#name,
value.to_string(),
))
} else {
Self(self.0.matches(|req| {
req.query_params
.as_ref()
.and_then(|qs| {
qs.iter().find(
|(key, _)| key == #name)
})
.is_none()
}))
}
}
};
}
OperationParameterKind::Header(_) => quote! { todo!() },
OperationParameterKind::Body(_) => match typ {
OperationParameterType::Type(_) => quote! {

View File

@ -203,8 +203,20 @@ pub mod operations {
Self(self.0.path_matches(re))
}
pub fn minseq(self, value: u32) -> Self {
pub fn minseq<T>(self, value: T) -> Self
where
T: Into<Option<u32>>,
{
if let Some(value) = value.into() {
Self(self.0.query_param("minseq", value.to_string()))
} else {
Self(self.0.matches(|req| {
req.query_params
.as_ref()
.and_then(|qs| qs.iter().find(|(key, _)| key == "minseq"))
.is_none()
}))
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -18,12 +18,36 @@ pub mod operations {
self.0
}
pub fn key(self, value: bool) -> Self {
pub fn key<T>(self, value: T) -> Self
where
T: Into<Option<bool>>,
{
if let Some(value) = value.into() {
Self(self.0.query_param("key", value.to_string()))
} else {
Self(self.0.matches(|req| {
req.query_params
.as_ref()
.and_then(|qs| qs.iter().find(|(key, _)| key == "key"))
.is_none()
}))
}
}
pub fn unique_key(self, value: &str) -> Self {
pub fn unique_key<'a, T>(self, value: T) -> Self
where
T: Into<Option<&'a str>>,
{
if let Some(value) = value.into() {
Self(self.0.query_param("unique_key", value.to_string()))
} else {
Self(self.0.matches(|req| {
req.query_params
.as_ref()
.and_then(|qs| qs.iter().find(|(key, _)| key == "unique_key"))
.is_none()
}))
}
}
}