Skip to content

Commit

Permalink
a body param named default generates non-compiling code (#1019)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahl authored Dec 24, 2024
1 parent bcf29e1 commit cf8d20f
Show file tree
Hide file tree
Showing 15 changed files with 199 additions and 140 deletions.
16 changes: 9 additions & 7 deletions progenitor-impl/src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1478,18 +1478,20 @@ impl Generator {
.map(|param| match &param.typ {
OperationParameterType::Type(type_id) => {
let ty = self.type_space.get_type(type_id)?;
let optional = param.kind.is_optional();
if optional {
Ok(quote! { Ok(None) })
} else if let (OperationParameterKind::Body(_), Some(builder_name)) =
(&param.kind, ty.builder())

// Fill in the appropriate initial value for the
// param_types generated above.
if let (OperationParameterKind::Body(_), Some(_)) = (&param.kind, ty.builder())
{
Ok(quote! { Ok(#builder_name :: default()) })
} else {
Ok(quote! { Ok(::std::default::Default::default()) })
} else if param.kind.is_required() {
let err_msg = format!("{} was not initialized", param.name);
Ok(quote! { Err(#err_msg.to_string()) })
} else {
Ok(quote! { Ok(None) })
}
}

OperationParameterType::RawBody => {
let err_msg = format!("{} was not initialized", param.name);
Ok(quote! { Err(#err_msg.to_string()) })
Expand Down
31 changes: 25 additions & 6 deletions progenitor-impl/tests/output/src/buildomat_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,14 @@ pub mod types {
///{
/// "type": "object",
/// "required": [
/// "default",
/// "name",
/// "script"
/// ],
/// "properties": {
/// "default": {
/// "type": "boolean"
/// },
/// "name": {
/// "type": "string"
/// },
Expand All @@ -307,6 +311,7 @@ pub mod types {
:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug, schemars :: JsonSchema,
)]
pub struct TaskSubmit {
pub default: bool,
pub name: ::std::string::String,
#[serde(default, skip_serializing_if = "::std::vec::Vec::is_empty")]
pub output_rules: ::std::vec::Vec<::std::string::String>,
Expand Down Expand Up @@ -1317,6 +1322,7 @@ pub mod types {

#[derive(Clone, Debug)]
pub struct TaskSubmit {
default: ::std::result::Result<bool, ::std::string::String>,
name: ::std::result::Result<::std::string::String, ::std::string::String>,
output_rules: ::std::result::Result<
::std::vec::Vec<::std::string::String>,
Expand All @@ -1328,6 +1334,7 @@ pub mod types {
impl ::std::default::Default for TaskSubmit {
fn default() -> Self {
Self {
default: Err("no value supplied for default".to_string()),
name: Err("no value supplied for name".to_string()),
output_rules: Ok(Default::default()),
script: Err("no value supplied for script".to_string()),
Expand All @@ -1336,6 +1343,16 @@ pub mod types {
}

impl TaskSubmit {
pub fn default<T>(mut self, value: T) -> Self
where
T: ::std::convert::TryInto<bool>,
T::Error: ::std::fmt::Display,
{
self.default = value
.try_into()
.map_err(|e| format!("error converting supplied value for default: {}", e));
self
}
pub fn name<T>(mut self, value: T) -> Self
where
T: ::std::convert::TryInto<::std::string::String>,
Expand Down Expand Up @@ -1374,6 +1391,7 @@ pub mod types {
value: TaskSubmit,
) -> ::std::result::Result<Self, super::error::ConversionError> {
Ok(Self {
default: value.default?,
name: value.name?,
output_rules: value.output_rules?,
script: value.script?,
Expand All @@ -1384,6 +1402,7 @@ pub mod types {
impl ::std::convert::From<super::TaskSubmit> for TaskSubmit {
fn from(value: super::TaskSubmit) -> Self {
Self {
default: Ok(value.default),
name: Ok(value.name),
output_rules: Ok(value.output_rules),
script: Ok(value.script),
Expand Down Expand Up @@ -2786,7 +2805,7 @@ pub mod builder {
pub fn new(client: &'a super::Client) -> Self {
Self {
client: client,
body: Ok(types::builder::TaskSubmit::default()),
body: Ok(::std::default::Default::default()),
}
}

Expand Down Expand Up @@ -3049,7 +3068,7 @@ pub mod builder {
pub fn new(client: &'a super::Client) -> Self {
Self {
client: client,
body: Ok(types::builder::UserCreate::default()),
body: Ok(::std::default::Default::default()),
}
}

Expand Down Expand Up @@ -3199,7 +3218,7 @@ pub mod builder {
pub fn new(client: &'a super::Client) -> Self {
Self {
client: client,
body: Ok(types::builder::WorkerBootstrap::default()),
body: Ok(::std::default::Default::default()),
}
}

Expand Down Expand Up @@ -3299,7 +3318,7 @@ pub mod builder {
Self {
client: client,
task: Err("task was not initialized".to_string()),
body: Ok(types::builder::WorkerAppendTask::default()),
body: Ok(::std::default::Default::default()),
}
}

Expand Down Expand Up @@ -3445,7 +3464,7 @@ pub mod builder {
Self {
client: client,
task: Err("task was not initialized".to_string()),
body: Ok(types::builder::WorkerCompleteTask::default()),
body: Ok(::std::default::Default::default()),
}
}

Expand Down Expand Up @@ -3519,7 +3538,7 @@ pub mod builder {
Self {
client: client,
task: Err("task was not initialized".to_string()),
body: Ok(types::builder::WorkerAddOutput::default()),
body: Ok(::std::default::Default::default()),
}
}

Expand Down
31 changes: 25 additions & 6 deletions progenitor-impl/tests/output/src/buildomat_builder_tagged.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,14 @@ pub mod types {
///{
/// "type": "object",
/// "required": [
/// "default",
/// "name",
/// "script"
/// ],
/// "properties": {
/// "default": {
/// "type": "boolean"
/// },
/// "name": {
/// "type": "string"
/// },
Expand All @@ -297,6 +301,7 @@ pub mod types {
/// </details>
#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)]
pub struct TaskSubmit {
pub default: bool,
pub name: ::std::string::String,
#[serde(default, skip_serializing_if = "::std::vec::Vec::is_empty")]
pub output_rules: ::std::vec::Vec<::std::string::String>,
Expand Down Expand Up @@ -1277,6 +1282,7 @@ pub mod types {

#[derive(Clone, Debug)]
pub struct TaskSubmit {
default: ::std::result::Result<bool, ::std::string::String>,
name: ::std::result::Result<::std::string::String, ::std::string::String>,
output_rules: ::std::result::Result<
::std::vec::Vec<::std::string::String>,
Expand All @@ -1288,6 +1294,7 @@ pub mod types {
impl ::std::default::Default for TaskSubmit {
fn default() -> Self {
Self {
default: Err("no value supplied for default".to_string()),
name: Err("no value supplied for name".to_string()),
output_rules: Ok(Default::default()),
script: Err("no value supplied for script".to_string()),
Expand All @@ -1296,6 +1303,16 @@ pub mod types {
}

impl TaskSubmit {
pub fn default<T>(mut self, value: T) -> Self
where
T: ::std::convert::TryInto<bool>,
T::Error: ::std::fmt::Display,
{
self.default = value
.try_into()
.map_err(|e| format!("error converting supplied value for default: {}", e));
self
}
pub fn name<T>(mut self, value: T) -> Self
where
T: ::std::convert::TryInto<::std::string::String>,
Expand Down Expand Up @@ -1334,6 +1351,7 @@ pub mod types {
value: TaskSubmit,
) -> ::std::result::Result<Self, super::error::ConversionError> {
Ok(Self {
default: value.default?,
name: value.name?,
output_rules: value.output_rules?,
script: value.script?,
Expand All @@ -1344,6 +1362,7 @@ pub mod types {
impl ::std::convert::From<super::TaskSubmit> for TaskSubmit {
fn from(value: super::TaskSubmit) -> Self {
Self {
default: Ok(value.default),
name: Ok(value.name),
output_rules: Ok(value.output_rules),
script: Ok(value.script),
Expand Down Expand Up @@ -2746,7 +2765,7 @@ pub mod builder {
pub fn new(client: &'a super::Client) -> Self {
Self {
client: client,
body: Ok(types::builder::TaskSubmit::default()),
body: Ok(::std::default::Default::default()),
}
}

Expand Down Expand Up @@ -3009,7 +3028,7 @@ pub mod builder {
pub fn new(client: &'a super::Client) -> Self {
Self {
client: client,
body: Ok(types::builder::UserCreate::default()),
body: Ok(::std::default::Default::default()),
}
}

Expand Down Expand Up @@ -3159,7 +3178,7 @@ pub mod builder {
pub fn new(client: &'a super::Client) -> Self {
Self {
client: client,
body: Ok(types::builder::WorkerBootstrap::default()),
body: Ok(::std::default::Default::default()),
}
}

Expand Down Expand Up @@ -3259,7 +3278,7 @@ pub mod builder {
Self {
client: client,
task: Err("task was not initialized".to_string()),
body: Ok(types::builder::WorkerAppendTask::default()),
body: Ok(::std::default::Default::default()),
}
}

Expand Down Expand Up @@ -3405,7 +3424,7 @@ pub mod builder {
Self {
client: client,
task: Err("task was not initialized".to_string()),
body: Ok(types::builder::WorkerCompleteTask::default()),
body: Ok(::std::default::Default::default()),
}
}

Expand Down Expand Up @@ -3479,7 +3498,7 @@ pub mod builder {
Self {
client: client,
task: Err("task was not initialized".to_string()),
body: Ok(types::builder::WorkerAddOutput::default()),
body: Ok(::std::default::Default::default()),
}
}

Expand Down
10 changes: 10 additions & 0 deletions progenitor-impl/tests/output/src/buildomat_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ impl<T: CliConfig> Cli<T> {

pub fn cli_task_submit() -> ::clap::Command {
::clap::Command::new("")
.arg(
::clap::Arg::new("default")
.long("default")
.value_parser(::clap::value_parser!(bool))
.required_unless_present("json-body"),
)
.arg(
::clap::Arg::new("name")
.long("name")
Expand Down Expand Up @@ -416,6 +422,10 @@ impl<T: CliConfig> Cli<T> {

pub async fn execute_task_submit(&self, matches: &::clap::ArgMatches) -> anyhow::Result<()> {
let mut request = self.client.task_submit();
if let Some(value) = matches.get_one::<bool>("default") {
request = request.body_map(|body| body.default(value.clone()))
}

if let Some(value) = matches.get_one::<::std::string::String>("name") {
request = request.body_map(|body| body.name(value.clone()))
}
Expand Down
5 changes: 5 additions & 0 deletions progenitor-impl/tests/output/src/buildomat_positional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,14 @@ pub mod types {
///{
/// "type": "object",
/// "required": [
/// "default",
/// "name",
/// "script"
/// ],
/// "properties": {
/// "default": {
/// "type": "boolean"
/// },
/// "name": {
/// "type": "string"
/// },
Expand All @@ -274,6 +278,7 @@ pub mod types {
/// </details>
#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)]
pub struct TaskSubmit {
pub default: bool,
pub name: ::std::string::String,
#[serde(default, skip_serializing_if = "::std::vec::Vec::is_empty")]
pub output_rules: ::std::vec::Vec<::std::string::String>,
Expand Down
2 changes: 1 addition & 1 deletion progenitor-impl/tests/output/src/cli_gen_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ pub mod builder {
Self {
client: client,
gateway: Err("gateway was not initialized".to_string()),
body: Ok(types::builder::UnoBody::default()),
body: Ok(::std::default::Default::default()),
}
}

Expand Down
2 changes: 1 addition & 1 deletion progenitor-impl/tests/output/src/cli_gen_builder_tagged.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ pub mod builder {
Self {
client: client,
gateway: Err("gateway was not initialized".to_string()),
body: Ok(types::builder::UnoBody::default()),
body: Ok(::std::default::Default::default()),
}
}

Expand Down
8 changes: 4 additions & 4 deletions progenitor-impl/tests/output/src/keeper_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1432,7 +1432,7 @@ pub mod builder {
Self {
client: client,
authorization: Err("authorization was not initialized".to_string()),
body: Ok(types::builder::EnrolBody::default()),
body: Ok(::std::default::Default::default()),
}
}

Expand Down Expand Up @@ -1623,7 +1623,7 @@ pub mod builder {
Self {
client: client,
authorization: Err("authorization was not initialized".to_string()),
body: Ok(types::builder::ReportFinishBody::default()),
body: Ok(::std::default::Default::default()),
}
}

Expand Down Expand Up @@ -1708,7 +1708,7 @@ pub mod builder {
Self {
client: client,
authorization: Err("authorization was not initialized".to_string()),
body: Ok(types::builder::ReportOutputBody::default()),
body: Ok(::std::default::Default::default()),
}
}

Expand Down Expand Up @@ -1793,7 +1793,7 @@ pub mod builder {
Self {
client: client,
authorization: Err("authorization was not initialized".to_string()),
body: Ok(types::builder::ReportStartBody::default()),
body: Ok(::std::default::Default::default()),
}
}

Expand Down
Loading

0 comments on commit cf8d20f

Please sign in to comment.