use serde_json::{Map, Value};
use crate::schema_override;
pub(super) fn mirror_global_schema_into_subcharts(
schema: &mut Value,
subchart_prefixes: &[Vec<String>],
) {
let Some(root_global_schema) = schema.pointer("/properties/global").cloned() else {
return;
};
for prefix in subchart_prefixes {
let subchart_schema = schema_object_at_values_prefix(schema, prefix);
let subchart_global_schema = schema_property_mut(subchart_schema, "global");
let existing = std::mem::take(subchart_global_schema);
*subchart_global_schema =
schema_override::apply_schema_override(existing, root_global_schema.clone());
}
}
fn schema_object_at_values_prefix<'a>(schema: &'a mut Value, prefix: &[String]) -> &'a mut Value {
let mut current = schema;
for segment in prefix {
current = schema_property_mut(current, segment);
}
current
}
fn schema_property_mut<'a>(schema: &'a mut Value, property: &str) -> &'a mut Value {
let object = ensure_json_object(schema);
let properties = object
.entry("properties".to_string())
.or_insert_with(|| Value::Object(Map::new()));
let properties = ensure_json_object(properties);
properties
.entry(property.to_string())
.or_insert_with(|| Value::Object(Map::new()))
}
fn ensure_json_object(value: &mut Value) -> &mut Map<String, Value> {
if let Value::Object(object) = value {
object
} else {
*value = Value::Object(Map::new());
ensure_json_object(value)
}
}
#[cfg(test)]
#[path = "tests/global_mirror.rs"]
mod tests;