all jspg tests now passing

This commit is contained in:
2026-03-04 01:02:32 -05:00
parent e7f20e2cb6
commit 566b599512
32 changed files with 531 additions and 1068 deletions

View File

@ -9,13 +9,15 @@ use crate::database::r#enum::Enum;
use crate::database::punc::Punc;
use crate::database::schema::Schema;
use crate::database::r#type::Type;
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
pub struct Database {
pub enums: HashMap<String, Enum>,
pub types: HashMap<String, Type>,
pub puncs: HashMap<String, Punc>,
pub schemas: HashMap<String, Schema>,
pub descendants: HashMap<String, Vec<String>>,
pub depths: HashMap<String, usize>,
}
impl Database {
@ -25,6 +27,8 @@ impl Database {
types: HashMap::new(),
puncs: HashMap::new(),
schemas: HashMap::new(),
descendants: HashMap::new(),
depths: HashMap::new(),
};
if let Some(arr) = val.get("enums").and_then(|v| v.as_array()) {
@ -69,43 +73,12 @@ impl Database {
db
}
/// Organizes the graph of the database, compiling regex, format functions, and pointing schema references.
/// Organizes the graph of the database, compiling regex, format functions, and caching relationships.
fn compile(&mut self) -> Result<(), String> {
self.collect_schemas();
// 1. Build a structural descendant graph for $family macro expansion
let mut direct_refs: std::collections::HashMap<String, Vec<String>> =
std::collections::HashMap::new();
for (id, schema) in &self.schemas {
if let Some(ref_str) = &schema.obj.ref_string {
direct_refs
.entry(ref_str.clone())
.or_default()
.push(id.clone());
}
}
let schema_ids: Vec<String> = self.schemas.keys().cloned().collect();
// 2. Expand $family macros into oneOf blocks
for id in &schema_ids {
if let Some(schema) = self.schemas.get_mut(id) {
schema.map_children(|mut child| {
Self::expand_family(&mut child, &direct_refs);
});
Self::expand_family(schema, &direct_refs);
}
}
let schemas_snap = self.schemas.clone();
// 3. Compile internals and link memory pointers
for id in schema_ids {
if let Some(schema) = self.schemas.get_mut(&id) {
schema.compile_internals();
schema.link_refs(&schemas_snap);
}
}
self.collect_depths();
self.collect_descendants();
self.compile_schemas();
Ok(())
}
@ -113,38 +86,18 @@ impl Database {
fn collect_schemas(&mut self) {
let mut to_insert = Vec::new();
// Pass A: Entities - Compute Variations from hierarchies
// `hierarchy` is an array of ancestors. E.g. `person` -> `['entity', 'user', 'person']`.
// We map this backward so that `user`'s allowed variations = `['user', 'person']`.
let mut variations_by_entity = std::collections::HashMap::new();
// Pass 1: Extract all Schemas structurally off top level definitions into the master registry.
for type_def in self.types.values() {
for ancestor in &type_def.hierarchy {
variations_by_entity
.entry(ancestor.clone())
.or_insert_with(std::collections::HashSet::new)
.insert(type_def.name.clone());
}
}
// Now stamp all exported entity schemas with their precise physical variations
for (_, type_def) in &self.types {
let allowed_strings = variations_by_entity
.get(&type_def.name)
.cloned()
.unwrap_or_default();
for mut schema in type_def.schemas.clone() {
schema.stamp_variations(Some(allowed_strings.clone()));
schema.harvest(&mut to_insert);
}
}
// Pass B: APIs and Enums (No initial variations stamped)
for (_, punc_def) in &self.puncs {
for punc_def in self.puncs.values() {
for mut schema in punc_def.schemas.clone() {
schema.harvest(&mut to_insert);
}
}
for (_, enum_def) in &self.enums {
for enum_def in self.enums.values() {
for mut schema in enum_def.schemas.clone() {
schema.harvest(&mut to_insert);
}
@ -155,55 +108,80 @@ impl Database {
}
}
fn expand_family(
schema: &mut crate::database::schema::Schema,
direct_refs: &std::collections::HashMap<String, Vec<String>>,
) {
if let Some(family_target) = &schema.obj.family {
let mut descendants = std::collections::HashSet::new();
Self::collect_descendants(family_target, direct_refs, &mut descendants);
fn collect_depths(&mut self) {
let mut depths: HashMap<String, usize> = HashMap::new();
let schema_ids: Vec<String> = self.schemas.keys().cloned().collect();
// the "$family" macro is logically replaced by an anyOf of its descendants + itself
let mut derived_any_of = Vec::new();
for id in schema_ids {
let mut current_id = id.clone();
let mut depth = 0;
let mut visited = HashSet::new();
// Include the target base itself if valid (which it always is structurally)
let mut base_ref = crate::database::schema::SchemaObject::default();
base_ref.ref_string = Some(family_target.clone());
derived_any_of.push(std::sync::Arc::new(crate::database::schema::Schema {
obj: base_ref,
always_fail: false,
}));
// Sort descendants for determinism during testing
let mut desc_vec: Vec<String> = descendants.into_iter().collect();
desc_vec.sort();
for child_id in desc_vec {
let mut child_ref = crate::database::schema::SchemaObject::default();
child_ref.ref_string = Some(child_id);
derived_any_of.push(std::sync::Arc::new(crate::database::schema::Schema {
obj: child_ref,
always_fail: false,
}));
while let Some(schema) = self.schemas.get(&current_id) {
if !visited.insert(current_id.clone()) {
break; // Cycle detected
}
if let Some(ref_str) = &schema.obj.ref_string {
current_id = ref_str.clone();
depth += 1;
} else {
break;
}
}
schema.obj.any_of = Some(derived_any_of);
// Remove family so it doesn't cause conflicts or fail the simple validation
schema.obj.family = None;
depths.insert(id, depth);
}
self.depths = depths;
}
fn collect_descendants(
fn collect_descendants(&mut self) {
let mut direct_refs: HashMap<String, Vec<String>> = HashMap::new();
for (id, schema) in &self.schemas {
if let Some(ref_str) = &schema.obj.ref_string {
direct_refs
.entry(ref_str.clone())
.or_default()
.push(id.clone());
}
}
// Cache generic descendants for $family runtime lookups
let mut descendants = HashMap::new();
for (id, schema) in &self.schemas {
if let Some(family_target) = &schema.obj.family {
let mut desc_set = HashSet::new();
Self::collect_descendants_recursively(family_target, &direct_refs, &mut desc_set);
let mut desc_vec: Vec<String> = desc_set.into_iter().collect();
desc_vec.sort();
// By placing all descendants directly onto the ID mapped location of the Family declaration,
// we can lookup descendants natively in ValidationContext without AST replacement overrides.
descendants.insert(id.clone(), desc_vec);
}
}
self.descendants = descendants;
}
fn collect_descendants_recursively(
target: &str,
direct_refs: &std::collections::HashMap<String, Vec<String>>,
descendants: &mut std::collections::HashSet<String>,
direct_refs: &HashMap<String, Vec<String>>,
descendants: &mut HashSet<String>,
) {
if let Some(children) = direct_refs.get(target) {
for child in children {
if descendants.insert(child.clone()) {
Self::collect_descendants(child, direct_refs, descendants);
Self::collect_descendants_recursively(child, direct_refs, descendants);
}
}
}
}
fn compile_schemas(&mut self) {
// Pass 3: compile_internals across pure structure
let schema_ids: Vec<String> = self.schemas.keys().cloned().collect();
for id in schema_ids {
if let Some(schema) = self.schemas.get_mut(&id) {
schema.compile_internals();
}
}
}
}