use std::collections::BTreeMap;
use fv_value::{json_to_value, Value};
use serde_json::Value as J;
use crate::labels::LabelBinding;
pub type DatasetResolver<'a> = dyn Fn(&str) -> Option<(String, String)> + 'a;
#[derive(Debug, Clone, PartialEq)]
pub struct Binding {
pub object_type: String,
pub source_type: String,
pub table: Option<String>,
pub catalog: Option<String>,
pub incremental_strategy: String,
pub source_updated_at_column: Option<String>,
pub correlation_token_column: Option<String>,
pub label_columns: BTreeMap<String, String>,
pub default_label: BTreeMap<String, Value>,
pub on_missing_label: String,
pub name: String,
pub primary: bool,
pub properties: Vec<String>,
}
impl Binding {
pub fn checkpoint_key(&self) -> String {
if self.name == "default" {
self.object_type.clone()
} else {
format!("{}:{}", self.object_type, self.name)
}
}
pub fn label_binding(&self) -> LabelBinding {
LabelBinding {
label_columns: self.label_columns.clone(),
default_label: self.default_label.clone(),
on_missing_label: self.on_missing_label.clone(),
properties: self.properties.clone(),
correlation_token_column: self.correlation_token_column.clone(),
source_updated_at_column: self.source_updated_at_column.clone(),
primary: self.primary,
}
}
}
fn str_map(j: Option<&J>) -> BTreeMap<String, String> {
j.and_then(J::as_object)
.map(|o| {
o.iter()
.filter_map(|(k, v)| v.as_str().map(|s| (k.clone(), s.to_string())))
.collect()
})
.unwrap_or_default()
}
fn str_vec(j: Option<&J>) -> Vec<String> {
j.and_then(J::as_array)
.map(|a| a.iter().filter_map(|v| v.as_str().map(String::from)).collect())
.unwrap_or_default()
}
pub fn binding_from_definition(
object_type: &str,
ds: &J,
primary_default: bool,
resolve: Option<&DatasetResolver>,
) -> Binding {
let mut table = ds.get("table").and_then(J::as_str).map(String::from);
let mut catalog = ds.get("catalog").and_then(J::as_str).map(String::from);
if table.is_none() {
if let (Some(dataset_ref), Some(r)) = (ds.get("dataset").and_then(J::as_str), resolve) {
if let Some((connector, resolved_table)) = r(dataset_ref) {
table = Some(resolved_table);
catalog = Some(connector);
}
}
}
Binding {
object_type: object_type.to_string(),
source_type: ds.get("type").and_then(J::as_str).unwrap_or("ICEBERG").to_string(),
table,
catalog,
incremental_strategy: ds
.get("incrementalStrategy")
.and_then(J::as_str)
.unwrap_or("FULL_SNAPSHOT")
.to_string(),
source_updated_at_column: ds.get("sourceUpdatedAtColumn").and_then(J::as_str).map(String::from),
correlation_token_column: ds.get("correlationTokenColumn").and_then(J::as_str).map(String::from),
label_columns: str_map(ds.get("labelColumns")),
default_label: ds
.get("defaultLabel")
.and_then(J::as_object)
.map(|o| o.iter().map(|(k, v)| (k.clone(), json_to_value(v))).collect())
.unwrap_or_default(),
on_missing_label: ds
.get("onMissingLabel")
.and_then(J::as_str)
.unwrap_or("DENY")
.to_string(),
name: ds.get("name").and_then(J::as_str).unwrap_or("default").to_string(),
primary: ds.get("primary").and_then(J::as_bool).unwrap_or(primary_default),
properties: str_vec(ds.get("properties")),
}
}
pub fn bindings_from_definitions(defs: &J, resolve: Option<&DatasetResolver>) -> BTreeMap<String, Vec<Binding>> {
let mut out = BTreeMap::new();
for ot in defs.get("objectTypes").and_then(J::as_array).into_iter().flatten() {
let api = match ot.get("apiName").and_then(J::as_str) {
Some(a) => a,
None => continue,
};
match ot.get("datasources").and_then(J::as_array) {
Some(plural) if !plural.is_empty() => {
out.insert(
api.to_string(),
plural
.iter()
.map(|ds| binding_from_definition(api, ds, false, resolve))
.collect(),
);
}
_ => {
if let Some(single) = ot.get("datasource").filter(|d| d.is_object()) {
out.insert(
api.to_string(),
vec![binding_from_definition(api, single, true, resolve)],
);
}
}
}
}
out
}
pub fn primary_binding(bindings: &[Binding]) -> &Binding {
bindings.iter().find(|b| b.primary).unwrap_or(&bindings[0])
}