From 2417885158f7bc7463ddb4566914c88cdb9d7cc5 Mon Sep 17 00:00:00 2001 From: Kacper Stasik Date: Fri, 11 Oct 2024 17:15:20 +0200 Subject: [PATCH] feat: nested string enum support --- crates/cli/src/commands/chain.rs | 3 +- crates/cli/src/commands/validate.rs | 5 +--- crates/cli/src/main.rs | 1 - .../schematools/src/codegen/jsonschema/mod.rs | 5 +--- .../src/codegen/jsonschema/oneof/extractor.rs | 29 ++++++++++++++++--- .../src/codegen/jsonschema/oneof/mod.rs | 4 +-- .../src/codegen/jsonschema/properties.rs | 13 ++------- crates/schematools/src/tools.rs | 2 +- 8 files changed, 34 insertions(+), 28 deletions(-) diff --git a/crates/cli/src/commands/chain.rs b/crates/cli/src/commands/chain.rs index dec5520..fad3512 100644 --- a/crates/cli/src/commands/chain.rs +++ b/crates/cli/src/commands/chain.rs @@ -156,11 +156,10 @@ pub fn execute(opts: Opts, client: &Client) -> Result<(), Error> { match cmd { #[cfg(feature = "codegen")] ChainCommandOption::Codegen(c) => c.run(current, &discovery, &storage), - ChainCommandOption::Process(c) => c.run(current, &storage).map(|result| { + ChainCommandOption::Process(c) => c.run(current, &storage).inspect(|_| { storage .schemas .insert(current.get_url().clone(), current.clone()); - result }), ChainCommandOption::Validate(v) => v.run(current), ChainCommandOption::Output(o) => { diff --git a/crates/cli/src/commands/validate.rs b/crates/cli/src/commands/validate.rs index bcb2559..f6e40f8 100644 --- a/crates/cli/src/commands/validate.rs +++ b/crates/cli/src/commands/validate.rs @@ -84,10 +84,7 @@ impl Opts { validate::validate_jsonschema(schema).map_err(Error::Schematools) } } - .map(|r| { - log::info!("\x1b[0;32mSuccessful validation!\x1b[0m"); - r - }) + .inspect(|_| log::info!("\x1b[0;32mSuccessful validation!\x1b[0m")) .or_else(|e| { log::error!("\x1b[1;31mValidation failed: \x1b[0m {}", e); diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index a0c1ef8..7dcee05 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -6,7 +6,6 @@ pub mod error; #[derive(Parser)] #[command(author, version, about)] - struct Opts { #[clap(subcommand)] command: Command, diff --git a/crates/schematools/src/codegen/jsonschema/mod.rs b/crates/schematools/src/codegen/jsonschema/mod.rs index 22af07c..5efcaa1 100644 --- a/crates/schematools/src/codegen/jsonschema/mod.rs +++ b/crates/schematools/src/codegen/jsonschema/mod.rs @@ -233,10 +233,7 @@ pub fn extract_type( match node { Value::Object(schema) => { - title::extract_title(schema, scope, options).map(|s| { - scope.entity(&s); - s - })?; + title::extract_title(schema, scope, options).inspect(|s| scope.entity(s))?; log::trace!("{}", scope); diff --git a/crates/schematools/src/codegen/jsonschema/oneof/extractor.rs b/crates/schematools/src/codegen/jsonschema/oneof/extractor.rs index ff2c2ba..df7b20b 100644 --- a/crates/schematools/src/codegen/jsonschema/oneof/extractor.rs +++ b/crates/schematools/src/codegen/jsonschema/oneof/extractor.rs @@ -50,10 +50,28 @@ pub struct DiscriminatorMeta { #[derive(Serialize)] #[serde(rename_all = "camelCase")] pub enum DiscriminatorValue { - Model(String), + Model(DiscriminatorValueModel), Simple(FlatModel), } +#[derive(Serialize)] +#[serde(rename_all = "camelCase")] +pub struct DiscriminatorValueModel { + name: String, + kind: String, +} + +impl DiscriminatorValueModel { + pub fn flat(model: &FlatModel) -> Option { + model.name.clone().map(|name| { + DiscriminatorValue::Model(Self { + name, + kind: model.type_.clone(), + }) + }) + } +} + pub struct Simple { properties: Vec, } @@ -136,7 +154,7 @@ impl Simple { value: f .model .as_ref() - .and_then(|m| m.name.clone().map(DiscriminatorValue::Model)) + .and_then(|e| DiscriminatorValueModel::flat(e)) .unwrap_or(DiscriminatorValue::Simple(f.clone())), properties: Some(object.properties.len()), } @@ -155,7 +173,7 @@ impl Simple { value: f .model .as_ref() - .and_then(|m| m.name.clone().map(DiscriminatorValue::Model)) + .and_then(|e| DiscriminatorValueModel::flat(e)) .unwrap_or(DiscriminatorValue::Simple(f.clone())), properties: Some(object.properties.len() - 1), } @@ -247,7 +265,10 @@ impl Extractor for Discriminator { DISCRIMINATOR_META.to_owned(), serde_json::to_value(DiscriminatorMeta { property: self.property.clone(), - value: DiscriminatorValue::Model(value), + value: DiscriminatorValue::Model(DiscriminatorValueModel { + name: value, + kind: "string".to_string(), + }), properties, }) .unwrap(), diff --git a/crates/schematools/src/codegen/jsonschema/oneof/mod.rs b/crates/schematools/src/codegen/jsonschema/oneof/mod.rs index 4101ea8..5bffbd8 100644 --- a/crates/schematools/src/codegen/jsonschema/oneof/mod.rs +++ b/crates/schematools/src/codegen/jsonschema/oneof/mod.rs @@ -291,7 +291,7 @@ mod tests { "_discriminator".to_string(), json!({ "property": "type", - "value": {"model": "value1"}, + "value": {"model":{"name": "value1","kind":"string"}}, "properties": 1 }) )] @@ -317,7 +317,7 @@ mod tests { "_discriminator".to_string(), json!({ "property": "type", - "value": {"model":"value2"}, + "value": {"model":{"name": "value2","kind":"string"}}, "properties": 1 }) )] diff --git a/crates/schematools/src/codegen/jsonschema/properties.rs b/crates/schematools/src/codegen/jsonschema/properties.rs index b1ce28c..b6aa91a 100644 --- a/crates/schematools/src/codegen/jsonschema/properties.rs +++ b/crates/schematools/src/codegen/jsonschema/properties.rs @@ -30,10 +30,7 @@ pub fn from_object_with_properties( let mut model = super::extract_type(property, container, scope, resolver, options) .and_then(|s| s.flatten(container, scope)) - .map_err(|e| { - scope.pop(); - e - })?; + .inspect_err(|_| scope.pop())?; model.name = Some(name.clone()); model.attributes.required = required.contains(name); @@ -42,12 +39,8 @@ pub fn from_object_with_properties( && !model.attributes.required && options.optional_and_nullable_as_models { - convert_to_nullable_optional_wrapper(model, container, scope).map_err( - |e| { - scope.pop(); - e - }, - )? + convert_to_nullable_optional_wrapper(model, container, scope) + .inspect_err(|_| scope.pop())? } else { model }; diff --git a/crates/schematools/src/tools.rs b/crates/schematools/src/tools.rs index 103f4da..f1daab0 100644 --- a/crates/schematools/src/tools.rs +++ b/crates/schematools/src/tools.rs @@ -156,7 +156,7 @@ impl<'a> ArgumentsExtractor<'a> { } } -impl<'a> Iterator for ArgumentsExtractor<'a> { +impl Iterator for ArgumentsExtractor<'_> { type Item = String; fn next(&mut self) -> Option {