correct builder error message for Option types (#327)

This commit is contained in:
Adam Leventhal 2023-02-05 20:31:40 -08:00 committed by GitHub
parent c26af3bd95
commit 1b942d1146
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 738 additions and 600 deletions

View File

@ -1401,18 +1401,26 @@ impl Generator {
OperationParameterType::Type(type_id) => {
let ty = self.type_space.get_type(type_id)?;
let details = ty.details();
let err_msg = format!("conversion to `{}` for {} failed", ty.name(), param.name);
match &details {
typify::TypeDetails::Option(ref opt_id) => {
typify::TypeDetails::Option(opt_id) => {
// TODO currently we explicitly turn optional
// parameters into Option types; we could probably
// defer this to the code generation step to avoid the
// special handling here.
let typ = self.type_space.get_type(opt_id)?.ident();
// parameters into Option types; we could
// probably defer this to the code generation
// step to avoid the special handling here.
let inner_type =
self.type_space.get_type(opt_id)?;
let typ = inner_type.ident();
let err_msg = format!(
"conversion to `{}` for {} failed",
inner_type.name(),
param.name,
);
Ok(quote! {
pub fn #param_name<V>(mut self, value: V)
-> Self
where V: std::convert::TryInto<#typ>
pub fn #param_name<V>(
mut self,
value: V,
) -> Self
where V: std::convert::TryInto<#typ>,
{
self.#param_name = value.try_into()
.map(Some)
@ -1423,8 +1431,16 @@ impl Generator {
}
_ => {
let typ = ty.ident();
let err_msg = format!(
"conversion to `{}` for {} failed",
ty.name(),
param.name,
);
Ok(quote! {
pub fn #param_name<V>(mut self, value: V) -> Self
pub fn #param_name<V>(
mut self,
value: V,
) -> Self
where V: std::convert::TryInto<#typ>,
{
self.#param_name = value.try_into()
@ -1437,8 +1453,10 @@ impl Generator {
}
OperationParameterType::RawBody => {
let err_msg =
format!("conversion to `reqwest::Body` for {} failed", param.name);
let err_msg = format!(
"conversion to `reqwest::Body` for {} failed",
param.name,
);
Ok(quote! {
pub fn #param_name<B>(mut self, value: B) -> Self

View File

@ -1856,7 +1856,7 @@ pub mod builder {
self.minseq = value
.try_into()
.map(Some)
.map_err(|_| "conversion to `Option < u32 >` for minseq failed".to_string());
.map_err(|_| "conversion to `u32` for minseq failed".to_string());
self
}

View File

@ -1856,7 +1856,7 @@ pub mod builder {
self.minseq = value
.try_into()
.map(Some)
.map_err(|_| "conversion to `Option < u32 >` for minseq failed".to_string());
.map_err(|_| "conversion to `u32` for minseq failed".to_string());
self
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -112,7 +112,7 @@ pub mod builder {
self.key = value
.try_into()
.map(Some)
.map_err(|_| "conversion to `Option < bool >` for key failed".to_string());
.map_err(|_| "conversion to `bool` for key failed".to_string());
self
}
@ -123,7 +123,7 @@ pub mod builder {
self.unique_key = value
.try_into()
.map(Some)
.map_err(|_| "conversion to `Option < String >` for unique_key failed".to_string());
.map_err(|_| "conversion to `String` for unique_key failed".to_string());
self
}

View File

@ -112,7 +112,7 @@ pub mod builder {
self.key = value
.try_into()
.map(Some)
.map_err(|_| "conversion to `Option < bool >` for key failed".to_string());
.map_err(|_| "conversion to `bool` for key failed".to_string());
self
}
@ -123,7 +123,7 @@ pub mod builder {
self.unique_key = value
.try_into()
.map(Some)
.map_err(|_| "conversion to `Option < String >` for unique_key failed".to_string());
.map_err(|_| "conversion to `String` for unique_key failed".to_string());
self
}