use std::collections::HashMap;
use super::DirGraph;
use crate::datatypes::values::Value;
use crate::graph::schema::SchemaDefinition;
impl DirGraph {
pub fn set_schema(&mut self, schema: SchemaDefinition) {
self.schema_definition = Some(schema);
}
pub fn get_schema(&self) -> Option<&SchemaDefinition> {
self.schema_definition.as_ref()
}
pub fn clear_schema(&mut self) {
self.schema_definition = None;
}
pub fn primary_key_for(&self, node_type: &str) -> Option<&str> {
self.schema_definition
.as_ref()?
.node_schemas
.get(node_type)?
.primary_key
.as_deref()
}
pub fn set_instructions(&mut self, text: &str, channel: Option<&str>) {
let key = channel.unwrap_or("").to_string();
if text.is_empty() {
self.graph_instructions.remove(&key);
} else {
self.graph_instructions.insert(key, text.to_string());
}
}
pub fn layer_for(&self, node_type: &str) -> Option<&str> {
self.schema_definition
.as_ref()?
.node_schemas
.get(node_type)?
.layer
.as_deref()
}
pub fn auto_timestamp_for(&self, node_type: &str) -> bool {
self.schema_definition
.as_ref()
.and_then(|s| s.node_schemas.get(node_type))
.and_then(|n| n.auto_timestamp)
.unwrap_or(false)
}
pub fn auto_timestamp_for_connection(&self, conn_type: &str) -> bool {
self.schema_definition
.as_ref()
.and_then(|s| s.connection_schemas.get(conn_type))
.and_then(|c| c.auto_timestamp)
.unwrap_or(false)
}
pub(crate) fn provenance_props(&self) -> Vec<(&'static str, Value)> {
let mut v = Vec::with_capacity(3);
v.push((
"updated_at",
Value::Timestamp(chrono::Local::now().naive_local()),
));
if let Some(sha) = &self.active_git_sha {
v.push(("git_sha", Value::String(sha.clone())));
}
if let Some(mb) = &self.active_modified_by {
v.push(("modified_by", Value::String(mb.clone())));
}
v
}
pub(crate) fn inject_provenance(&self, node_type: &str, props: &mut HashMap<String, Value>) {
if !self.auto_timestamp_for(node_type) {
return;
}
for (k, v) in self.provenance_props() {
props.insert(k.to_string(), v);
}
}
pub(crate) fn inject_edge_provenance(
&self,
conn_type: &str,
props: &mut HashMap<String, Value>,
) {
if !self.auto_timestamp_for_connection(conn_type) {
return;
}
for (k, v) in self.provenance_props() {
props.insert(k.to_string(), v);
}
}
pub fn get_instructions(&self, channel: Option<&str>) -> Option<&str> {
self.graph_instructions
.get(channel.unwrap_or(""))
.or_else(|| self.graph_instructions.get(""))
.map(String::as_str)
}
}