Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 54 additions & 12 deletions progenitor-impl/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,24 +445,56 @@ impl Generator {
// There should be no conflicting path or query parameters.
assert!(!args.has_arg(&arg_name));

let parser = clap_arg(&arg_name, volitionality, &param.description, &arg_type);
// An array parameter is a flag that may be repeated, with each
// occurrence parsed as one item.
let item_type = param
.typ
.list_item_type(typespace)
.map(|item_type_id| typespace.get_type(&item_type_id));

let parser = match &item_type {
Some(item_type) => clap_arg(
&arg_name,
volitionality,
&param.description,
item_type,
true,
),
None => clap_arg(
&arg_name,
volitionality,
&param.description,
&arg_type,
false,
),
};

let arg_fn_name = sanitize(&param.name, Case::Snake);
let arg_fn = format_ident!("{}", arg_fn_name);
let OperationParameterType::Type(arg_type_id) = &param.typ else {
panic!()
};
let arg_type = typespace.get_type(arg_type_id);
let arg_type_name = arg_type.ident_in(crate::TYPES_MOD);

let consumer = quote! {
if let Some(value) =
matches.get_one::<#arg_type_name>(#arg_name)
{
// clone here in case the arg type doesn't impl
// From<&T>
request = request.#arg_fn(value.clone());
let consumer = match &item_type {
Some(item_type) => {
let item_type_name = item_type.ident_in(crate::TYPES_MOD);
quote! {
if let Some(values) =
matches.get_many::<#item_type_name>(#arg_name)
{
request = request.#arg_fn(
values.cloned().collect::<#arg_type_name>()
);
}
}
}
None => quote! {
if let Some(value) =
matches.get_one::<#arg_type_name>(#arg_name)
{
// clone here in case the arg type doesn't impl
// From<&T>
request = request.#arg_fn(value.clone());
}
},
};

args.add_arg(arg_name, CliArg { parser, consumer })
Expand Down Expand Up @@ -625,6 +657,7 @@ impl Generator {
volitionality,
&description.map(str::to_string),
&prop_type,
false,
);

let prop_fn = format_ident!("{}", sanitize(name, Case::Snake));
Expand Down Expand Up @@ -666,12 +699,20 @@ enum Volitionality {
RequiredIfNoBody,
}

/// Build the clap argument for a flag. With `multiple`, `arg_type` is the item
/// type and the flag may be repeated, once per item.
fn clap_arg(
arg_name: &str,
volitionality: Volitionality,
description: &Option<String>,
arg_type: &typify::typespace::view::Type<'_, TypeId>,
multiple: bool,
) -> TokenStream {
let action = multiple.then(|| {
quote! {
.action(::clap::ArgAction::Append)
}
});
let help = description.as_ref().map(|description| {
quote! {
.help(#description)
Expand Down Expand Up @@ -733,6 +774,7 @@ fn clap_arg(
::clap::Arg::new(#arg_name)
.long(#arg_name)
.value_parser(#value_parser)
#action
#required
#help
}
Expand Down
8 changes: 6 additions & 2 deletions progenitor-impl/src/httpmock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
method::{
BodyContentType, HttpMethod, OperationParameter, OperationParameterKind,
OperationParameterType, OperationResponse, OperationResponseKind, OperationResponseStatus,
header_value_string,
},
util::{Case, sanitize},
};
Expand Down Expand Up @@ -177,6 +178,9 @@ impl Generator {
};

let name_ident = format_ident!("{}", name);
let header_value =
header_value_string(quote! { value }, typ.list_item_type(typespace).is_some());

let (required, handler) = match kind {
OperationParameterKind::Path => {
let re_fmt = method.path.as_wildcard_param(api_name);
Expand All @@ -199,7 +203,7 @@ impl Generator {
OperationParameterKind::Header(true) => (
true,
quote! {
Self(self.0.header(#api_name, value.to_string()))
Self(self.0.header(#api_name, #header_value))
},
),

Expand All @@ -222,7 +226,7 @@ impl Generator {
if let Some(value) = value.into() {
Self(self.0.header(
#api_name,
value.to_string()
#header_value
))
} else {
Self(self.0.header_missing(#api_name))
Expand Down
46 changes: 44 additions & 2 deletions progenitor-impl/src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,26 @@ pub enum OperationParameterType {
RawBody,
}

impl OperationParameterType {
/// The item type when this is an array or a set. Header and query
/// parameters of these types are sent, mocked and parsed from the command
/// line one item at a time.
pub(crate) fn list_item_type(
&self,
typespace: &typespace::Typespace<TypeId>,
) -> Option<TypeId> {
let OperationParameterType::Type(type_id) = self else {
return None;
};
match typespace.get_type(type_id).details() {
typespace::view::TypeDetails::Vec(item) | typespace::view::TypeDetails::Set(item) => {
Some(item)
}
_ => None,
}
}
}

#[derive(Debug, PartialEq, Eq)]
pub enum OperationParameterKind {
Path,
Expand Down Expand Up @@ -802,19 +822,22 @@ impl Generator {
OperationParameterKind::Header(required) => {
let hn = &param.api_name;
let hn_ident = format_ident!("{}", &param.name);
let is_list = param.typ.list_item_type(self.typespace()).is_some();
let res = if *required {
let value = header_value_string(quote! { #hn_ident }, is_list);
quote! {
header_map.append(
#hn,
#hn_ident.to_string().try_into()?
#value.try_into()?
);
}
} else {
let value = header_value_string(quote! { value }, is_list);
quote! {
if let Some(value) = #hn_ident {
header_map.append(
#hn,
value.to_string().try_into()?
#value.try_into()?
);
}
}
Expand Down Expand Up @@ -2140,6 +2163,25 @@ impl Construction {
}
}

/// The string a header parameter is sent as. OpenAPI 3.0.3 gives header
/// parameters only the simple style, which joins the items of an array with
/// commas, so `["a", "b"]` is sent as `a,b`. Each item is converted with a
/// method call, as a scalar header is, so an item type that only derefs to
/// `String` works too.
pub(crate) fn header_value_string(value: TokenStream, is_list: bool) -> TokenStream {
if is_list {
quote! {
#value
.iter()
.map(|v| v.to_string())
.collect::<::std::vec::Vec<_>>()
.join(",")
}
} else {
quote! { #value.to_string() }
}
}

fn make_doc_comment(method: &OperationMethod) -> String {
let mut buf = String::new();

Expand Down
5 changes: 5 additions & 0 deletions progenitor-impl/tests/output/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ pub mod nexus_builder_tagged;
pub mod nexus_cli;
pub mod nexus_httpmock;
pub mod nexus_positional;
pub mod param_arrays_builder;
pub mod param_arrays_builder_tagged;
pub mod param_arrays_cli;
//pub mod param_arrays_httpmock;
pub mod param_arrays_positional;
pub mod param_collision_builder;
pub mod param_collision_builder_tagged;
pub mod param_collision_cli;
Expand Down
Loading