From af73b90ec293ec367e44fe8a76aac10d0ac80421 Mon Sep 17 00:00:00 2001 From: David Bernard Date: Sun, 14 Jan 2024 13:31:06 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20use=20the=20`context.type`?= =?UTF-8?q?=20(instead=20of=20`$id`)=20to=20define=20the=20module=20name?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit resulting in a more aligned file naming with spec (schema and example) --- cdevents-sdk/src/generated/mod.rs | 150 +++++++++--------- ...un_finished.rs => pipelinerun_finished.rs} | 0 ...ne_run_queued.rs => pipelinerun_queued.rs} | 0 ..._run_started.rs => pipelinerun_started.rs} | 0 ...sk_run_finished.rs => taskrun_finished.rs} | 0 ...task_run_started.rs => taskrun_started.rs} | 0 ...un_finished.rs => testcaserun_finished.rs} | 0 ...se_run_queued.rs => testcaserun_queued.rs} | 0 ..._run_started.rs => testcaserun_started.rs} | 0 ...t_published.rs => testoutput_published.rs} | 0 ...e_finished.rs => testsuiterun_finished.rs} | 0 ...e_run_queued.rs => testsuiterun_queued.rs} | 0 ...run_started.rs => testsuiterun_started.rs} | 0 generator/src/main.rs | 39 +++-- 14 files changed, 97 insertions(+), 92 deletions(-) rename cdevents-sdk/src/generated/{pipeline_run_finished.rs => pipelinerun_finished.rs} (100%) rename cdevents-sdk/src/generated/{pipeline_run_queued.rs => pipelinerun_queued.rs} (100%) rename cdevents-sdk/src/generated/{pipeline_run_started.rs => pipelinerun_started.rs} (100%) rename cdevents-sdk/src/generated/{task_run_finished.rs => taskrun_finished.rs} (100%) rename cdevents-sdk/src/generated/{task_run_started.rs => taskrun_started.rs} (100%) rename cdevents-sdk/src/generated/{test_case_run_finished.rs => testcaserun_finished.rs} (100%) rename cdevents-sdk/src/generated/{test_case_run_queued.rs => testcaserun_queued.rs} (100%) rename cdevents-sdk/src/generated/{test_case_run_started.rs => testcaserun_started.rs} (100%) rename cdevents-sdk/src/generated/{test_output_published.rs => testoutput_published.rs} (100%) rename cdevents-sdk/src/generated/{test_suite_finished.rs => testsuiterun_finished.rs} (100%) rename cdevents-sdk/src/generated/{test_suite_run_queued.rs => testsuiterun_queued.rs} (100%) rename cdevents-sdk/src/generated/{test_suite_run_started.rs => testsuiterun_started.rs} (100%) diff --git a/cdevents-sdk/src/generated/mod.rs b/cdevents-sdk/src/generated/mod.rs index 50bd19c..879534f 100644 --- a/cdevents-sdk/src/generated/mod.rs +++ b/cdevents-sdk/src/generated/mod.rs @@ -21,9 +21,9 @@ pub mod environment_modified; pub mod incident_detected; pub mod incident_reported; pub mod incident_resolved; -pub mod pipeline_run_finished; -pub mod pipeline_run_queued; -pub mod pipeline_run_started; +pub mod pipelinerun_finished; +pub mod pipelinerun_queued; +pub mod pipelinerun_started; pub mod repository_created; pub mod repository_deleted; pub mod repository_modified; @@ -32,17 +32,17 @@ pub mod service_published; pub mod service_removed; pub mod service_rolledback; pub mod service_upgraded; -pub mod task_run_finished; -pub mod task_run_started; -pub mod test_case_run_finished; -pub mod test_case_run_queued; -pub mod test_case_run_started; -pub mod test_output_published; -pub mod test_suite_finished; -pub mod test_suite_run_queued; -pub mod test_suite_run_started; +pub mod taskrun_finished; +pub mod taskrun_started; +pub mod testcaserun_finished; +pub mod testcaserun_queued; +pub mod testcaserun_started; +pub mod testoutput_published; +pub mod testsuiterun_finished; +pub mod testsuiterun_queued; +pub mod testsuiterun_started; -use serde::{Serialize, Deserialize}; +use serde::{Deserialize, Serialize}; pub const ARTIFACT_PACKAGED: &str = "dev.cdevents.artifact.packaged.0.1.1"; pub const ARTIFACT_PUBLISHED: &str = "dev.cdevents.artifact.published.0.1.1"; @@ -63,9 +63,9 @@ pub const ENVIRONMENT_MODIFIED: &str = "dev.cdevents.environment.modified.0.1.1" pub const INCIDENT_DETECTED: &str = "dev.cdevents.incident.detected.0.1.0"; pub const INCIDENT_REPORTED: &str = "dev.cdevents.incident.reported.0.1.0"; pub const INCIDENT_RESOLVED: &str = "dev.cdevents.incident.resolved.0.1.0"; -pub const PIPELINE_RUN_FINISHED: &str = "dev.cdevents.pipelinerun.finished.0.1.1"; -pub const PIPELINE_RUN_QUEUED: &str = "dev.cdevents.pipelinerun.queued.0.1.1"; -pub const PIPELINE_RUN_STARTED: &str = "dev.cdevents.pipelinerun.started.0.1.1"; +pub const PIPELINERUN_FINISHED: &str = "dev.cdevents.pipelinerun.finished.0.1.1"; +pub const PIPELINERUN_QUEUED: &str = "dev.cdevents.pipelinerun.queued.0.1.1"; +pub const PIPELINERUN_STARTED: &str = "dev.cdevents.pipelinerun.started.0.1.1"; pub const REPOSITORY_CREATED: &str = "dev.cdevents.repository.created.0.1.1"; pub const REPOSITORY_DELETED: &str = "dev.cdevents.repository.deleted.0.1.1"; pub const REPOSITORY_MODIFIED: &str = "dev.cdevents.repository.modified.0.1.1"; @@ -74,17 +74,17 @@ pub const SERVICE_PUBLISHED: &str = "dev.cdevents.service.published.0.1.1"; pub const SERVICE_REMOVED: &str = "dev.cdevents.service.removed.0.1.1"; pub const SERVICE_ROLLEDBACK: &str = "dev.cdevents.service.rolledback.0.1.1"; pub const SERVICE_UPGRADED: &str = "dev.cdevents.service.upgraded.0.1.1"; -pub const TASK_RUN_FINISHED: &str = "dev.cdevents.taskrun.finished.0.1.1"; -pub const TASK_RUN_STARTED: &str = "dev.cdevents.taskrun.started.0.1.1"; -pub const TEST_CASE_RUN_FINISHED: &str = "dev.cdevents.testcaserun.finished.0.1.0"; -pub const TEST_CASE_RUN_QUEUED: &str = "dev.cdevents.testcaserun.queued.0.1.0"; -pub const TEST_CASE_RUN_STARTED: &str = "dev.cdevents.testcaserun.started.0.1.0"; -pub const TEST_OUTPUT_PUBLISHED: &str = "dev.cdevents.testoutput.published.0.1.0"; -pub const TEST_SUITE_FINISHED: &str = "dev.cdevents.testsuiterun.finished.0.1.0"; -pub const TEST_SUITE_RUN_QUEUED: &str = "dev.cdevents.testsuiterun.queued.0.1.0"; -pub const TEST_SUITE_RUN_STARTED: &str = "dev.cdevents.testsuiterun.started.0.1.0"; +pub const TASKRUN_FINISHED: &str = "dev.cdevents.taskrun.finished.0.1.1"; +pub const TASKRUN_STARTED: &str = "dev.cdevents.taskrun.started.0.1.1"; +pub const TESTCASERUN_FINISHED: &str = "dev.cdevents.testcaserun.finished.0.1.0"; +pub const TESTCASERUN_QUEUED: &str = "dev.cdevents.testcaserun.queued.0.1.0"; +pub const TESTCASERUN_STARTED: &str = "dev.cdevents.testcaserun.started.0.1.0"; +pub const TESTOUTPUT_PUBLISHED: &str = "dev.cdevents.testoutput.published.0.1.0"; +pub const TESTSUITERUN_FINISHED: &str = "dev.cdevents.testsuiterun.finished.0.1.0"; +pub const TESTSUITERUN_QUEUED: &str = "dev.cdevents.testsuiterun.queued.0.1.0"; +pub const TESTSUITERUN_STARTED: &str = "dev.cdevents.testsuiterun.started.0.1.0"; -#[derive(Debug,Clone,Serialize,Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] #[serde(untagged)] // TODO how to use content of context.type as discriminator ? pub enum Content { ArtifactPackaged(artifact_packaged::Content), @@ -106,9 +106,9 @@ pub enum Content { IncidentDetected(incident_detected::Content), IncidentReported(incident_reported::Content), IncidentResolved(incident_resolved::Content), - PipelineRunFinished(pipeline_run_finished::Content), - PipelineRunQueued(pipeline_run_queued::Content), - PipelineRunStarted(pipeline_run_started::Content), + PipelinerunFinished(pipelinerun_finished::Content), + PipelinerunQueued(pipelinerun_queued::Content), + PipelinerunStarted(pipelinerun_started::Content), RepositoryCreated(repository_created::Content), RepositoryDeleted(repository_deleted::Content), RepositoryModified(repository_modified::Content), @@ -117,19 +117,19 @@ pub enum Content { ServiceRemoved(service_removed::Content), ServiceRolledback(service_rolledback::Content), ServiceUpgraded(service_upgraded::Content), - TaskRunFinished(task_run_finished::Content), - TaskRunStarted(task_run_started::Content), - TestCaseRunFinished(test_case_run_finished::Content), - TestCaseRunQueued(test_case_run_queued::Content), - TestCaseRunStarted(test_case_run_started::Content), - TestOutputPublished(test_output_published::Content), - TestSuiteFinished(test_suite_finished::Content), - TestSuiteRunQueued(test_suite_run_queued::Content), - TestSuiteRunStarted(test_suite_run_started::Content), + TaskrunFinished(taskrun_finished::Content), + TaskrunStarted(taskrun_started::Content), + TestcaserunFinished(testcaserun_finished::Content), + TestcaserunQueued(testcaserun_queued::Content), + TestcaserunStarted(testcaserun_started::Content), + TestoutputPublished(testoutput_published::Content), + TestsuiterunFinished(testsuiterun_finished::Content), + TestsuiterunQueued(testsuiterun_queued::Content), + TestsuiterunStarted(testsuiterun_started::Content), } impl Content { - pub fn from_json(ty: &str, json: serde_json::Value) -> Result{ + pub fn from_json(ty: &str, json: serde_json::Value) -> Result { match ty { ARTIFACT_PACKAGED => { let variant: artifact_packaged::Content = serde_json::from_value(json)?; @@ -207,17 +207,17 @@ impl Content { let variant: incident_resolved::Content = serde_json::from_value(json)?; Ok(Self::IncidentResolved(variant)) }, - PIPELINE_RUN_FINISHED => { - let variant: pipeline_run_finished::Content = serde_json::from_value(json)?; - Ok(Self::PipelineRunFinished(variant)) + PIPELINERUN_FINISHED => { + let variant: pipelinerun_finished::Content = serde_json::from_value(json)?; + Ok(Self::PipelinerunFinished(variant)) }, - PIPELINE_RUN_QUEUED => { - let variant: pipeline_run_queued::Content = serde_json::from_value(json)?; - Ok(Self::PipelineRunQueued(variant)) + PIPELINERUN_QUEUED => { + let variant: pipelinerun_queued::Content = serde_json::from_value(json)?; + Ok(Self::PipelinerunQueued(variant)) }, - PIPELINE_RUN_STARTED => { - let variant: pipeline_run_started::Content = serde_json::from_value(json)?; - Ok(Self::PipelineRunStarted(variant)) + PIPELINERUN_STARTED => { + let variant: pipelinerun_started::Content = serde_json::from_value(json)?; + Ok(Self::PipelinerunStarted(variant)) }, REPOSITORY_CREATED => { let variant: repository_created::Content = serde_json::from_value(json)?; @@ -251,41 +251,41 @@ impl Content { let variant: service_upgraded::Content = serde_json::from_value(json)?; Ok(Self::ServiceUpgraded(variant)) }, - TASK_RUN_FINISHED => { - let variant: task_run_finished::Content = serde_json::from_value(json)?; - Ok(Self::TaskRunFinished(variant)) + TASKRUN_FINISHED => { + let variant: taskrun_finished::Content = serde_json::from_value(json)?; + Ok(Self::TaskrunFinished(variant)) }, - TASK_RUN_STARTED => { - let variant: task_run_started::Content = serde_json::from_value(json)?; - Ok(Self::TaskRunStarted(variant)) + TASKRUN_STARTED => { + let variant: taskrun_started::Content = serde_json::from_value(json)?; + Ok(Self::TaskrunStarted(variant)) }, - TEST_CASE_RUN_FINISHED => { - let variant: test_case_run_finished::Content = serde_json::from_value(json)?; - Ok(Self::TestCaseRunFinished(variant)) + TESTCASERUN_FINISHED => { + let variant: testcaserun_finished::Content = serde_json::from_value(json)?; + Ok(Self::TestcaserunFinished(variant)) }, - TEST_CASE_RUN_QUEUED => { - let variant: test_case_run_queued::Content = serde_json::from_value(json)?; - Ok(Self::TestCaseRunQueued(variant)) + TESTCASERUN_QUEUED => { + let variant: testcaserun_queued::Content = serde_json::from_value(json)?; + Ok(Self::TestcaserunQueued(variant)) }, - TEST_CASE_RUN_STARTED => { - let variant: test_case_run_started::Content = serde_json::from_value(json)?; - Ok(Self::TestCaseRunStarted(variant)) + TESTCASERUN_STARTED => { + let variant: testcaserun_started::Content = serde_json::from_value(json)?; + Ok(Self::TestcaserunStarted(variant)) }, - TEST_OUTPUT_PUBLISHED => { - let variant: test_output_published::Content = serde_json::from_value(json)?; - Ok(Self::TestOutputPublished(variant)) + TESTOUTPUT_PUBLISHED => { + let variant: testoutput_published::Content = serde_json::from_value(json)?; + Ok(Self::TestoutputPublished(variant)) }, - TEST_SUITE_FINISHED => { - let variant: test_suite_finished::Content = serde_json::from_value(json)?; - Ok(Self::TestSuiteFinished(variant)) + TESTSUITERUN_FINISHED => { + let variant: testsuiterun_finished::Content = serde_json::from_value(json)?; + Ok(Self::TestsuiterunFinished(variant)) }, - TEST_SUITE_RUN_QUEUED => { - let variant: test_suite_run_queued::Content = serde_json::from_value(json)?; - Ok(Self::TestSuiteRunQueued(variant)) + TESTSUITERUN_QUEUED => { + let variant: testsuiterun_queued::Content = serde_json::from_value(json)?; + Ok(Self::TestsuiterunQueued(variant)) }, - TEST_SUITE_RUN_STARTED => { - let variant: test_suite_run_started::Content = serde_json::from_value(json)?; - Ok(Self::TestSuiteRunStarted(variant)) + TESTSUITERUN_STARTED => { + let variant: testsuiterun_started::Content = serde_json::from_value(json)?; + Ok(Self::TestsuiterunStarted(variant)) }, variant => Err(serde_json::Error::custom(format_args!( "unknown variant `{}`, expected 'dev.cdevents.{{subject}}.{{predicate}}.{{version}}'", diff --git a/cdevents-sdk/src/generated/pipeline_run_finished.rs b/cdevents-sdk/src/generated/pipelinerun_finished.rs similarity index 100% rename from cdevents-sdk/src/generated/pipeline_run_finished.rs rename to cdevents-sdk/src/generated/pipelinerun_finished.rs diff --git a/cdevents-sdk/src/generated/pipeline_run_queued.rs b/cdevents-sdk/src/generated/pipelinerun_queued.rs similarity index 100% rename from cdevents-sdk/src/generated/pipeline_run_queued.rs rename to cdevents-sdk/src/generated/pipelinerun_queued.rs diff --git a/cdevents-sdk/src/generated/pipeline_run_started.rs b/cdevents-sdk/src/generated/pipelinerun_started.rs similarity index 100% rename from cdevents-sdk/src/generated/pipeline_run_started.rs rename to cdevents-sdk/src/generated/pipelinerun_started.rs diff --git a/cdevents-sdk/src/generated/task_run_finished.rs b/cdevents-sdk/src/generated/taskrun_finished.rs similarity index 100% rename from cdevents-sdk/src/generated/task_run_finished.rs rename to cdevents-sdk/src/generated/taskrun_finished.rs diff --git a/cdevents-sdk/src/generated/task_run_started.rs b/cdevents-sdk/src/generated/taskrun_started.rs similarity index 100% rename from cdevents-sdk/src/generated/task_run_started.rs rename to cdevents-sdk/src/generated/taskrun_started.rs diff --git a/cdevents-sdk/src/generated/test_case_run_finished.rs b/cdevents-sdk/src/generated/testcaserun_finished.rs similarity index 100% rename from cdevents-sdk/src/generated/test_case_run_finished.rs rename to cdevents-sdk/src/generated/testcaserun_finished.rs diff --git a/cdevents-sdk/src/generated/test_case_run_queued.rs b/cdevents-sdk/src/generated/testcaserun_queued.rs similarity index 100% rename from cdevents-sdk/src/generated/test_case_run_queued.rs rename to cdevents-sdk/src/generated/testcaserun_queued.rs diff --git a/cdevents-sdk/src/generated/test_case_run_started.rs b/cdevents-sdk/src/generated/testcaserun_started.rs similarity index 100% rename from cdevents-sdk/src/generated/test_case_run_started.rs rename to cdevents-sdk/src/generated/testcaserun_started.rs diff --git a/cdevents-sdk/src/generated/test_output_published.rs b/cdevents-sdk/src/generated/testoutput_published.rs similarity index 100% rename from cdevents-sdk/src/generated/test_output_published.rs rename to cdevents-sdk/src/generated/testoutput_published.rs diff --git a/cdevents-sdk/src/generated/test_suite_finished.rs b/cdevents-sdk/src/generated/testsuiterun_finished.rs similarity index 100% rename from cdevents-sdk/src/generated/test_suite_finished.rs rename to cdevents-sdk/src/generated/testsuiterun_finished.rs diff --git a/cdevents-sdk/src/generated/test_suite_run_queued.rs b/cdevents-sdk/src/generated/testsuiterun_queued.rs similarity index 100% rename from cdevents-sdk/src/generated/test_suite_run_queued.rs rename to cdevents-sdk/src/generated/testsuiterun_queued.rs diff --git a/cdevents-sdk/src/generated/test_suite_run_started.rs b/cdevents-sdk/src/generated/testsuiterun_started.rs similarity index 100% rename from cdevents-sdk/src/generated/test_suite_run_started.rs rename to cdevents-sdk/src/generated/testsuiterun_started.rs diff --git a/generator/src/main.rs b/generator/src/main.rs index 885fd34..793af06 100644 --- a/generator/src/main.rs +++ b/generator/src/main.rs @@ -1,4 +1,4 @@ -use anyhow::{anyhow, Context, Result}; +use anyhow::{Context, Result}; use clap::Parser; use cruet::{to_class_case, Inflector}; use handlebars::{DirectorySourceOptions, Handlebars}; @@ -46,11 +46,7 @@ fn main() -> Result<()> { if let Some(extension) = path.extension() { if extension == "json" { let json: Value = serde_json::from_str(&std::fs::read_to_string(&path)?)?; - let context_type = json["properties"]["context"]["properties"]["type"]["default"] - .as_str() - .unwrap_or_default() - .to_string(); - let (rust_module, code) = generate_variant(&hbs, json) + let (rust_module, context_type, code) = generate_variant(&hbs, json) .with_context(|| format!("failed to generate variant on {:?}", &path))?; let file = settings .dest @@ -73,21 +69,30 @@ fn main() -> Result<()> { Ok(()) } -fn generate_variant(hbs: &Handlebars, jsonschema: Value) -> Result<(String, String)> { - let id = jsonschema["$id"] +fn generate_variant(hbs: &Handlebars, jsonschema: Value) -> Result<(String, String, String)> { + // let id = jsonschema["$id"] + // .as_str() + // .ok_or(anyhow!("$id not found or not a string")) + // .and_then(|s| url::Url::parse(s).with_context(|| format!("failed to parse: {}", s)))?; + // let module_name = id + // .path_segments() + // .and_then(|v| v.last()) + // .map(cruet::to_snake_case) + // .ok_or(anyhow!("no path in $id"))? + // .replace("_event", ""); + + // extract module's name from `context.type` (and not from `$id`) + let context_type = jsonschema["properties"]["context"]["properties"]["type"]["default"] .as_str() - .ok_or(anyhow!("$id not found or not a string")) - .and_then(|s| url::Url::parse(s).with_context(|| format!("failed to parse: {}", s)))?; - let module_name = id - .path_segments() - .and_then(|v| v.last()) - .map(cruet::to_snake_case) - .ok_or(anyhow!("no path in $id"))? - .replace("_event", ""); + .unwrap_or_default() + .to_string(); + + let fragments = context_type.split('.').collect::>(); + let module_name = format!("{}_{}", fragments[2], fragments[3]).to_snake_case(); let data = build_data_for_variants(jsonschema); let code = hbs.render("variant", &data)?; - Ok((module_name.to_string(), code)) + Ok((module_name.to_string(), context_type, code)) } fn generate_module(hbs: &Handlebars, variants: &[VariantInfo]) -> Result<(String, String)> {