From 8f721bbac76c640e8b36bdd01212aa7157ebb444 Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Thu, 2 Feb 2023 13:39:31 +0100 Subject: [PATCH] Add support for all_of --- src/main.rs | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index c29e802..2b0a444 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,6 +35,39 @@ struct TreeNode { r#type: NodeType, } +impl TreeNode { + pub fn merge_with(&self, other: &Self) -> Self { + if !matches!(self.r#type, NodeType::String | NodeType::Object { .. }) { + panic!("Cannot merge!"); + } + + if !matches!(other.r#type, NodeType::String | NodeType::Object { .. }) { + panic!("Cannot merge other!"); + } + + let r#type = match (&self.r#type, &other.r#type) { + (NodeType::String, NodeType::String) => NodeType::String, + (NodeType::String, NodeType::Object { children }) + | (NodeType::Object { children }, NodeType::String) => NodeType::Object { + children: children.clone(), + }, + + (NodeType::Object { children: c1 }, NodeType::Object { children: c2 }) => { + let mut children = c1.clone(); + children.append(&mut c2.clone()); + NodeType::Object { children } + } + + (_, _) => unreachable!(), + }; + + TreeNode { + name: other.name.to_string(), + r#type, + } + } +} + fn main() { env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); @@ -96,6 +129,23 @@ fn build_tree_schema( ); } + if let Some(subschemas) = &schema.subschemas { + if let Some(all_of) = &subschemas.all_of { + assert!(!all_of.is_empty()); + let mut tree = + build_tree_schema(expect_schema_object(&all_of[0]), struct_name, components); + + for other in all_of.iter().skip(1) { + let other = build_tree_schema(expect_schema_object(other), struct_name, components); + tree = tree.merge_with(&other); + } + + return tree; + } else { + panic!("Unsupported case!"); + } + } + let schema_type = schema .instance_type .as_ref() @@ -109,8 +159,8 @@ fn build_tree_schema( let children = schema .object .as_ref() - .unwrap() - .properties + .map(|s| s.properties.clone()) + .unwrap_or_default() .iter() .map(|e| { let o = expect_schema_object(e.1);