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

View File

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

View File

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

View File

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