Skip to content

Commit cf8d20f

Browse files
authored
a body param named default generates non-compiling code (#1019)
1 parent bcf29e1 commit cf8d20f

15 files changed

+199
-140
lines changed

progenitor-impl/src/method.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,18 +1478,20 @@ impl Generator {
14781478
.map(|param| match &param.typ {
14791479
OperationParameterType::Type(type_id) => {
14801480
let ty = self.type_space.get_type(type_id)?;
1481-
let optional = param.kind.is_optional();
1482-
if optional {
1483-
Ok(quote! { Ok(None) })
1484-
} else if let (OperationParameterKind::Body(_), Some(builder_name)) =
1485-
(&param.kind, ty.builder())
1481+
1482+
// Fill in the appropriate initial value for the
1483+
// param_types generated above.
1484+
if let (OperationParameterKind::Body(_), Some(_)) = (&param.kind, ty.builder())
14861485
{
1487-
Ok(quote! { Ok(#builder_name :: default()) })
1488-
} else {
1486+
Ok(quote! { Ok(::std::default::Default::default()) })
1487+
} else if param.kind.is_required() {
14891488
let err_msg = format!("{} was not initialized", param.name);
14901489
Ok(quote! { Err(#err_msg.to_string()) })
1490+
} else {
1491+
Ok(quote! { Ok(None) })
14911492
}
14921493
}
1494+
14931495
OperationParameterType::RawBody => {
14941496
let err_msg = format!("{} was not initialized", param.name);
14951497
Ok(quote! { Err(#err_msg.to_string()) })

progenitor-impl/tests/output/src/buildomat_builder.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,14 @@ pub mod types {
283283
///{
284284
/// "type": "object",
285285
/// "required": [
286+
/// "default",
286287
/// "name",
287288
/// "script"
288289
/// ],
289290
/// "properties": {
291+
/// "default": {
292+
/// "type": "boolean"
293+
/// },
290294
/// "name": {
291295
/// "type": "string"
292296
/// },
@@ -307,6 +311,7 @@ pub mod types {
307311
:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug, schemars :: JsonSchema,
308312
)]
309313
pub struct TaskSubmit {
314+
pub default: bool,
310315
pub name: ::std::string::String,
311316
#[serde(default, skip_serializing_if = "::std::vec::Vec::is_empty")]
312317
pub output_rules: ::std::vec::Vec<::std::string::String>,
@@ -1317,6 +1322,7 @@ pub mod types {
13171322

13181323
#[derive(Clone, Debug)]
13191324
pub struct TaskSubmit {
1325+
default: ::std::result::Result<bool, ::std::string::String>,
13201326
name: ::std::result::Result<::std::string::String, ::std::string::String>,
13211327
output_rules: ::std::result::Result<
13221328
::std::vec::Vec<::std::string::String>,
@@ -1328,6 +1334,7 @@ pub mod types {
13281334
impl ::std::default::Default for TaskSubmit {
13291335
fn default() -> Self {
13301336
Self {
1337+
default: Err("no value supplied for default".to_string()),
13311338
name: Err("no value supplied for name".to_string()),
13321339
output_rules: Ok(Default::default()),
13331340
script: Err("no value supplied for script".to_string()),
@@ -1336,6 +1343,16 @@ pub mod types {
13361343
}
13371344

13381345
impl TaskSubmit {
1346+
pub fn default<T>(mut self, value: T) -> Self
1347+
where
1348+
T: ::std::convert::TryInto<bool>,
1349+
T::Error: ::std::fmt::Display,
1350+
{
1351+
self.default = value
1352+
.try_into()
1353+
.map_err(|e| format!("error converting supplied value for default: {}", e));
1354+
self
1355+
}
13391356
pub fn name<T>(mut self, value: T) -> Self
13401357
where
13411358
T: ::std::convert::TryInto<::std::string::String>,
@@ -1374,6 +1391,7 @@ pub mod types {
13741391
value: TaskSubmit,
13751392
) -> ::std::result::Result<Self, super::error::ConversionError> {
13761393
Ok(Self {
1394+
default: value.default?,
13771395
name: value.name?,
13781396
output_rules: value.output_rules?,
13791397
script: value.script?,
@@ -1384,6 +1402,7 @@ pub mod types {
13841402
impl ::std::convert::From<super::TaskSubmit> for TaskSubmit {
13851403
fn from(value: super::TaskSubmit) -> Self {
13861404
Self {
1405+
default: Ok(value.default),
13871406
name: Ok(value.name),
13881407
output_rules: Ok(value.output_rules),
13891408
script: Ok(value.script),
@@ -2786,7 +2805,7 @@ pub mod builder {
27862805
pub fn new(client: &'a super::Client) -> Self {
27872806
Self {
27882807
client: client,
2789-
body: Ok(types::builder::TaskSubmit::default()),
2808+
body: Ok(::std::default::Default::default()),
27902809
}
27912810
}
27922811

@@ -3049,7 +3068,7 @@ pub mod builder {
30493068
pub fn new(client: &'a super::Client) -> Self {
30503069
Self {
30513070
client: client,
3052-
body: Ok(types::builder::UserCreate::default()),
3071+
body: Ok(::std::default::Default::default()),
30533072
}
30543073
}
30553074

@@ -3199,7 +3218,7 @@ pub mod builder {
31993218
pub fn new(client: &'a super::Client) -> Self {
32003219
Self {
32013220
client: client,
3202-
body: Ok(types::builder::WorkerBootstrap::default()),
3221+
body: Ok(::std::default::Default::default()),
32033222
}
32043223
}
32053224

@@ -3299,7 +3318,7 @@ pub mod builder {
32993318
Self {
33003319
client: client,
33013320
task: Err("task was not initialized".to_string()),
3302-
body: Ok(types::builder::WorkerAppendTask::default()),
3321+
body: Ok(::std::default::Default::default()),
33033322
}
33043323
}
33053324

@@ -3445,7 +3464,7 @@ pub mod builder {
34453464
Self {
34463465
client: client,
34473466
task: Err("task was not initialized".to_string()),
3448-
body: Ok(types::builder::WorkerCompleteTask::default()),
3467+
body: Ok(::std::default::Default::default()),
34493468
}
34503469
}
34513470

@@ -3519,7 +3538,7 @@ pub mod builder {
35193538
Self {
35203539
client: client,
35213540
task: Err("task was not initialized".to_string()),
3522-
body: Ok(types::builder::WorkerAddOutput::default()),
3541+
body: Ok(::std::default::Default::default()),
35233542
}
35243543
}
35253544

progenitor-impl/tests/output/src/buildomat_builder_tagged.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,14 @@ pub mod types {
275275
///{
276276
/// "type": "object",
277277
/// "required": [
278+
/// "default",
278279
/// "name",
279280
/// "script"
280281
/// ],
281282
/// "properties": {
283+
/// "default": {
284+
/// "type": "boolean"
285+
/// },
282286
/// "name": {
283287
/// "type": "string"
284288
/// },
@@ -297,6 +301,7 @@ pub mod types {
297301
/// </details>
298302
#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)]
299303
pub struct TaskSubmit {
304+
pub default: bool,
300305
pub name: ::std::string::String,
301306
#[serde(default, skip_serializing_if = "::std::vec::Vec::is_empty")]
302307
pub output_rules: ::std::vec::Vec<::std::string::String>,
@@ -1277,6 +1282,7 @@ pub mod types {
12771282

12781283
#[derive(Clone, Debug)]
12791284
pub struct TaskSubmit {
1285+
default: ::std::result::Result<bool, ::std::string::String>,
12801286
name: ::std::result::Result<::std::string::String, ::std::string::String>,
12811287
output_rules: ::std::result::Result<
12821288
::std::vec::Vec<::std::string::String>,
@@ -1288,6 +1294,7 @@ pub mod types {
12881294
impl ::std::default::Default for TaskSubmit {
12891295
fn default() -> Self {
12901296
Self {
1297+
default: Err("no value supplied for default".to_string()),
12911298
name: Err("no value supplied for name".to_string()),
12921299
output_rules: Ok(Default::default()),
12931300
script: Err("no value supplied for script".to_string()),
@@ -1296,6 +1303,16 @@ pub mod types {
12961303
}
12971304

12981305
impl TaskSubmit {
1306+
pub fn default<T>(mut self, value: T) -> Self
1307+
where
1308+
T: ::std::convert::TryInto<bool>,
1309+
T::Error: ::std::fmt::Display,
1310+
{
1311+
self.default = value
1312+
.try_into()
1313+
.map_err(|e| format!("error converting supplied value for default: {}", e));
1314+
self
1315+
}
12991316
pub fn name<T>(mut self, value: T) -> Self
13001317
where
13011318
T: ::std::convert::TryInto<::std::string::String>,
@@ -1334,6 +1351,7 @@ pub mod types {
13341351
value: TaskSubmit,
13351352
) -> ::std::result::Result<Self, super::error::ConversionError> {
13361353
Ok(Self {
1354+
default: value.default?,
13371355
name: value.name?,
13381356
output_rules: value.output_rules?,
13391357
script: value.script?,
@@ -1344,6 +1362,7 @@ pub mod types {
13441362
impl ::std::convert::From<super::TaskSubmit> for TaskSubmit {
13451363
fn from(value: super::TaskSubmit) -> Self {
13461364
Self {
1365+
default: Ok(value.default),
13471366
name: Ok(value.name),
13481367
output_rules: Ok(value.output_rules),
13491368
script: Ok(value.script),
@@ -2746,7 +2765,7 @@ pub mod builder {
27462765
pub fn new(client: &'a super::Client) -> Self {
27472766
Self {
27482767
client: client,
2749-
body: Ok(types::builder::TaskSubmit::default()),
2768+
body: Ok(::std::default::Default::default()),
27502769
}
27512770
}
27522771

@@ -3009,7 +3028,7 @@ pub mod builder {
30093028
pub fn new(client: &'a super::Client) -> Self {
30103029
Self {
30113030
client: client,
3012-
body: Ok(types::builder::UserCreate::default()),
3031+
body: Ok(::std::default::Default::default()),
30133032
}
30143033
}
30153034

@@ -3159,7 +3178,7 @@ pub mod builder {
31593178
pub fn new(client: &'a super::Client) -> Self {
31603179
Self {
31613180
client: client,
3162-
body: Ok(types::builder::WorkerBootstrap::default()),
3181+
body: Ok(::std::default::Default::default()),
31633182
}
31643183
}
31653184

@@ -3259,7 +3278,7 @@ pub mod builder {
32593278
Self {
32603279
client: client,
32613280
task: Err("task was not initialized".to_string()),
3262-
body: Ok(types::builder::WorkerAppendTask::default()),
3281+
body: Ok(::std::default::Default::default()),
32633282
}
32643283
}
32653284

@@ -3405,7 +3424,7 @@ pub mod builder {
34053424
Self {
34063425
client: client,
34073426
task: Err("task was not initialized".to_string()),
3408-
body: Ok(types::builder::WorkerCompleteTask::default()),
3427+
body: Ok(::std::default::Default::default()),
34093428
}
34103429
}
34113430

@@ -3479,7 +3498,7 @@ pub mod builder {
34793498
Self {
34803499
client: client,
34813500
task: Err("task was not initialized".to_string()),
3482-
body: Ok(types::builder::WorkerAddOutput::default()),
3501+
body: Ok(::std::default::Default::default()),
34833502
}
34843503
}
34853504

progenitor-impl/tests/output/src/buildomat_cli.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ impl<T: CliConfig> Cli<T> {
5656

5757
pub fn cli_task_submit() -> ::clap::Command {
5858
::clap::Command::new("")
59+
.arg(
60+
::clap::Arg::new("default")
61+
.long("default")
62+
.value_parser(::clap::value_parser!(bool))
63+
.required_unless_present("json-body"),
64+
)
5965
.arg(
6066
::clap::Arg::new("name")
6167
.long("name")
@@ -416,6 +422,10 @@ impl<T: CliConfig> Cli<T> {
416422

417423
pub async fn execute_task_submit(&self, matches: &::clap::ArgMatches) -> anyhow::Result<()> {
418424
let mut request = self.client.task_submit();
425+
if let Some(value) = matches.get_one::<bool>("default") {
426+
request = request.body_map(|body| body.default(value.clone()))
427+
}
428+
419429
if let Some(value) = matches.get_one::<::std::string::String>("name") {
420430
request = request.body_map(|body| body.name(value.clone()))
421431
}

progenitor-impl/tests/output/src/buildomat_positional.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,14 @@ pub mod types {
252252
///{
253253
/// "type": "object",
254254
/// "required": [
255+
/// "default",
255256
/// "name",
256257
/// "script"
257258
/// ],
258259
/// "properties": {
260+
/// "default": {
261+
/// "type": "boolean"
262+
/// },
259263
/// "name": {
260264
/// "type": "string"
261265
/// },
@@ -274,6 +278,7 @@ pub mod types {
274278
/// </details>
275279
#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)]
276280
pub struct TaskSubmit {
281+
pub default: bool,
277282
pub name: ::std::string::String,
278283
#[serde(default, skip_serializing_if = "::std::vec::Vec::is_empty")]
279284
pub output_rules: ::std::vec::Vec<::std::string::String>,

progenitor-impl/tests/output/src/cli_gen_builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ pub mod builder {
242242
Self {
243243
client: client,
244244
gateway: Err("gateway was not initialized".to_string()),
245-
body: Ok(types::builder::UnoBody::default()),
245+
body: Ok(::std::default::Default::default()),
246246
}
247247
}
248248

progenitor-impl/tests/output/src/cli_gen_builder_tagged.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ pub mod builder {
240240
Self {
241241
client: client,
242242
gateway: Err("gateway was not initialized".to_string()),
243-
body: Ok(types::builder::UnoBody::default()),
243+
body: Ok(::std::default::Default::default()),
244244
}
245245
}
246246

progenitor-impl/tests/output/src/keeper_builder.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,7 +1432,7 @@ pub mod builder {
14321432
Self {
14331433
client: client,
14341434
authorization: Err("authorization was not initialized".to_string()),
1435-
body: Ok(types::builder::EnrolBody::default()),
1435+
body: Ok(::std::default::Default::default()),
14361436
}
14371437
}
14381438

@@ -1623,7 +1623,7 @@ pub mod builder {
16231623
Self {
16241624
client: client,
16251625
authorization: Err("authorization was not initialized".to_string()),
1626-
body: Ok(types::builder::ReportFinishBody::default()),
1626+
body: Ok(::std::default::Default::default()),
16271627
}
16281628
}
16291629

@@ -1708,7 +1708,7 @@ pub mod builder {
17081708
Self {
17091709
client: client,
17101710
authorization: Err("authorization was not initialized".to_string()),
1711-
body: Ok(types::builder::ReportOutputBody::default()),
1711+
body: Ok(::std::default::Default::default()),
17121712
}
17131713
}
17141714

@@ -1793,7 +1793,7 @@ pub mod builder {
17931793
Self {
17941794
client: client,
17951795
authorization: Err("authorization was not initialized".to_string()),
1796-
body: Ok(types::builder::ReportStartBody::default()),
1796+
body: Ok(::std::default::Default::default()),
17971797
}
17981798
}
17991799

0 commit comments

Comments
 (0)