Add support for all_of

This commit is contained in:
Pierre HUBERT 2023-02-02 13:39:31 +01:00
parent abd8ba90c4
commit 8f721bbac7

View File

@ -35,6 +35,39 @@ struct TreeNode {
r#type: NodeType, 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() { fn main() {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); 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 let schema_type = schema
.instance_type .instance_type
.as_ref() .as_ref()
@ -109,8 +159,8 @@ fn build_tree_schema(
let children = schema let children = schema
.object .object
.as_ref() .as_ref()
.unwrap() .map(|s| s.properties.clone())
.properties .unwrap_or_default()
.iter() .iter()
.map(|e| { .map(|e| {
let o = expect_schema_object(e.1); let o = expect_schema_object(e.1);