added support for root schema compiled properties for the mixer

This commit is contained in:
2026-03-20 18:04:49 -04:00
parent 9038607729
commit 9255439d53
4 changed files with 126 additions and 40 deletions

View File

@ -23,6 +23,7 @@ use relation::Relation;
use schema::Schema;
use serde_json::Value;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use r#type::Type;
pub struct Database {
@ -310,12 +311,84 @@ impl Database {
}
fn compile_schemas(&mut self) {
// Pass 3: compile_internals across pure structure
let schema_ids: Vec<String> = self.schemas.keys().cloned().collect();
let mut compiled_names_map: HashMap<String, Vec<String>> = HashMap::new();
let mut compiled_props_map: HashMap<String, std::collections::BTreeMap<String, Arc<Schema>>> =
HashMap::new();
for id in &schema_ids {
if let Some(schema) = self.schemas.get(id) {
let mut visited = HashSet::new();
let merged = self.merged_properties(schema, &mut visited);
let mut names: Vec<String> = merged.keys().cloned().collect();
if !names.is_empty() {
names.sort();
compiled_names_map.insert(id.clone(), names);
compiled_props_map.insert(id.clone(), merged);
}
}
}
for id in schema_ids {
if let Some(schema) = self.schemas.get_mut(&id) {
if let Some(names) = compiled_names_map.remove(&id) {
schema.obj.compiled_property_names = Some(names);
}
if let Some(props) = compiled_props_map.remove(&id) {
schema.obj.compiled_properties = Some(props);
}
schema.compile_internals();
}
}
}
pub fn merged_properties(
&self,
schema: &Schema,
visited: &mut HashSet<String>,
) -> std::collections::BTreeMap<String, Arc<Schema>> {
if let Some(props) = &schema.obj.compiled_properties {
return props.clone();
}
let mut props = std::collections::BTreeMap::new();
if let Some(id) = &schema.obj.id {
if !visited.insert(id.clone()) {
return props;
}
}
if let Some(ref_id) = &schema.obj.r#ref {
if let Some(parent_schema) = self.schemas.get(ref_id) {
props.extend(self.merged_properties(parent_schema, visited));
}
}
if let Some(all_of) = &schema.obj.all_of {
for ao in all_of {
props.extend(self.merged_properties(ao, visited));
}
}
if let Some(then_schema) = &schema.obj.then_ {
props.extend(self.merged_properties(then_schema, visited));
}
if let Some(else_schema) = &schema.obj.else_ {
props.extend(self.merged_properties(else_schema, visited));
}
if let Some(local_props) = &schema.obj.properties {
for (k, v) in local_props {
props.insert(k.clone(), v.clone());
}
}
if let Some(id) = &schema.obj.id {
visited.remove(id);
}
props
}
}