Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: nested string enum support #111

Merged
merged 1 commit into from
Oct 11, 2024
Merged
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
3 changes: 1 addition & 2 deletions crates/cli/src/commands/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
5 changes: 1 addition & 4 deletions crates/cli/src/commands/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
1 change: 0 additions & 1 deletion crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ pub mod error;

#[derive(Parser)]
#[command(author, version, about)]

struct Opts {
#[clap(subcommand)]
command: Command,
Expand Down
5 changes: 1 addition & 4 deletions crates/schematools/src/codegen/jsonschema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
29 changes: 25 additions & 4 deletions crates/schematools/src/codegen/jsonschema/oneof/extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DiscriminatorValue> {
model.name.clone().map(|name| {
DiscriminatorValue::Model(Self {
name,
kind: model.type_.clone(),
})
})
}
}

pub struct Simple {
properties: Vec<SimpleProperty>,
}
Expand Down Expand Up @@ -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()),
}
Expand All @@ -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),
}
Expand Down Expand Up @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions crates/schematools/src/codegen/jsonschema/oneof/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ mod tests {
"_discriminator".to_string(),
json!({
"property": "type",
"value": {"model": "value1"},
"value": {"model":{"name": "value1","kind":"string"}},
"properties": 1
})
)]
Expand All @@ -317,7 +317,7 @@ mod tests {
"_discriminator".to_string(),
json!({
"property": "type",
"value": {"model":"value2"},
"value": {"model":{"name": "value2","kind":"string"}},
"properties": 1
})
)]
Expand Down
13 changes: 3 additions & 10 deletions crates/schematools/src/codegen/jsonschema/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
};
Expand Down
2 changes: 1 addition & 1 deletion crates/schematools/src/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self::Item> {
Expand Down
Loading