use std::collections::{BTreeMap, HashMap};
use compact_str::ToCompactString as _;
use mlt_core::{
MltResult, NestedKey, NestedKind, NestedValue, PropKind, PropValue, PropertyKey, TileFeatureBuilder,
TileLayerBuilder,
};
use serde_json::Value;
const MAX_DEPTH: usize = 8;
#[derive(Default)]
pub(super) struct Documents {
names: Vec<String>,
index: HashMap<String, usize>,
shapes: Vec<Shape>,
}
impl Documents {
pub(super) fn push(&mut self, name: &str, document: Option<Value>, docs: &mut Vec<(usize, Value)>) {
let Some(document) = document.filter(|d| !d.is_null()) else {
return;
};
let shape = Shape::of(&document);
let idx = if let Some(&idx) = self.index.get(name) {
let merged = std::mem::take(&mut self.shapes[idx]).merge(shape);
self.shapes[idx] = merged;
idx
} else {
let idx = self.names.len();
self.names.push(name.to_owned());
self.index.insert(name.to_owned(), idx);
self.shapes.push(shape);
idx
};
docs.push((idx, document));
}
pub(super) fn declare(self, builder: &mut TileLayerBuilder) -> MltResult<Vec<Option<Column>>> {
self.names
.into_iter()
.zip(self.shapes)
.map(|(name, shape)| {
Ok(match shape.kind(1) {
None => None,
Some(NestedKind::Leaf(kind)) => Some(Column::Scalar(builder.add_property(name, kind)?, kind)),
Some(kind) => Some(Column::Nested(builder.add_nested(name, kind.clone())?, kind)),
})
})
.collect()
}
}
pub(super) enum Column {
Scalar(PropertyKey, PropKind),
Nested(NestedKey, NestedKind),
}
impl Column {
pub(super) fn set(&self, feature: &mut TileFeatureBuilder<'_>, document: Value) -> MltResult<()> {
match self {
Self::Scalar(key, kind) => feature.property(*key, leaf(*kind, document))?,
Self::Nested(key, kind) => feature.nested(*key, value(document, kind))?,
};
Ok(())
}
}
#[derive(Debug, Clone, Default, PartialEq)]
enum Shape {
#[default]
Null,
Leaf(PropKind),
List(Box<Self>),
Map(BTreeMap<String, Self>),
}
impl Shape {
fn of(value: &Value) -> Self {
match value {
Value::Null => Self::Null,
Value::Bool(_) => Self::Leaf(PropKind::Bool),
Value::Number(n) if n.is_i64() => Self::Leaf(PropKind::I64),
Value::Number(_) => Self::Leaf(PropKind::F64),
Value::String(_) => Self::Leaf(PropKind::Str),
Value::Array(items) => Self::List(Box::new(items.iter().map(Self::of).fold(Self::Null, Self::merge))),
Value::Object(entries) => Self::Map(
entries
.iter()
.map(|(key, value)| (key.clone(), Self::of(value)))
.collect(),
),
}
}
fn merge(self, other: Self) -> Self {
match (self, other) {
(Self::Null, shape) | (shape, Self::Null) => shape,
(Self::Leaf(a), Self::Leaf(b)) => Self::Leaf(match (a, b) {
_ if a == b => a,
(PropKind::I64, PropKind::F64) | (PropKind::F64, PropKind::I64) => PropKind::F64,
_ => PropKind::Str,
}),
(Self::List(a), Self::List(b)) => Self::List(Box::new(a.merge(*b))),
(Self::Map(mut a), Self::Map(b)) => {
for (key, shape) in b {
let merged = a.remove(&key).unwrap_or_default().merge(shape);
a.insert(key, merged);
}
Self::Map(a)
}
_ => Self::Leaf(PropKind::Str),
}
}
fn kind(self, depth: usize) -> Option<NestedKind> {
match self {
Self::Null => None,
Self::Leaf(kind) => Some(NestedKind::Leaf(kind)),
_ if depth >= MAX_DEPTH => Some(NestedKind::Leaf(PropKind::Str)),
Self::List(element) => Some(NestedKind::list(
element.kind(depth + 1).unwrap_or(NestedKind::Leaf(PropKind::Str)),
)),
Self::Map(fields) => {
let fields: BTreeMap<_, _> = fields
.into_iter()
.filter_map(|(key, shape)| Some((key, shape.kind(depth + 1)?)))
.collect();
(!fields.is_empty()).then_some(NestedKind::Map(fields))
}
}
}
}
fn value(document: Value, kind: &NestedKind) -> NestedValue {
match (kind, document) {
(_, Value::Null) => kind.null_value(),
(NestedKind::Leaf(kind), document) => NestedValue::Leaf(leaf(*kind, document)),
(NestedKind::List(element), Value::Array(items)) => {
NestedValue::List(Some(items.into_iter().map(|item| value(item, element)).collect()))
}
(NestedKind::Map(fields), Value::Object(entries)) => NestedValue::Map(Some(
entries
.into_iter()
.filter(|(_, value)| !value.is_null())
.filter_map(|(key, entry)| {
let kind = fields.get(&key)?;
Some((key.into(), value(entry, kind)))
})
.collect(),
)),
(NestedKind::List(_) | NestedKind::Map(_), _) => kind.null_value(),
}
}
fn leaf(kind: PropKind, document: Value) -> PropValue {
match (kind, document) {
(PropKind::Bool, Value::Bool(b)) => PropValue::Bool(Some(b)),
(PropKind::I64, Value::Number(n)) => PropValue::I64(n.as_i64()),
(PropKind::F64, Value::Number(n)) => PropValue::F64(n.as_f64()),
(PropKind::Str, Value::String(s)) => PropValue::Str(Some(s.into())),
(PropKind::Str, document) => PropValue::Str(Some(document.to_compact_string())),
(kind, _) => PropValue::null(kind),
}
}