use std::collections::BTreeMap;
use dataferry_registry::RawConnection;
pub(crate) type NamespaceFn = fn(&RawConnection) -> Option<String>;
#[derive(Default)]
pub(crate) struct Namespaces {
by_type: BTreeMap<&'static str, NamespaceFn>,
}
impl Namespaces {
pub fn add(&mut self, type_name: &'static str, namespace: NamespaceFn) {
self.by_type.insert(type_name, namespace);
}
pub fn of(&self, connection: &RawConnection) -> Option<String> {
self.by_type
.get(connection.r#type.as_str())
.and_then(|f| f(connection))
}
}
#[cfg(feature = "postgres")]
pub(crate) fn postgres(conn: &RawConnection) -> Option<String> {
let options: dataferry_connector_postgres::PostgresOptions =
serde_json::from_value(serde_json::Value::Object(conn.fields.clone())).ok()?;
Some(options.schema)
}