use super::{
super::{data::*, store::*},
class::*,
property::*,
};
use {kutil::std::immutable::*, std::collections::*};
#[derive(Clone, Debug)]
pub struct Instance {
pub id: ID,
pub origin_template_id: Option<ID>,
pub metadata: Metadata,
pub class_ids: Vec<ID>,
pub properties: BTreeMap<ByteString, Property>,
}
impl Instance {
pub fn new_for(kind: EntityKind, directory: Directory, id: ByteString, origin_template_id: Option<ID>) -> Self {
Self::new_with(ID::new_for(kind, directory, id), origin_template_id)
}
pub fn new_with(id: ID, origin_template_id: Option<ID>) -> Self {
Self {
id,
origin_template_id,
metadata: Default::default(),
class_ids: Default::default(),
properties: Default::default(),
}
}
pub fn into_expression<'own, StoreT>(
self,
map: &mut BTreeMap<Expression, Expression>,
embedded: bool,
store: &'own StoreT,
) -> Result<(), StoreError>
where
StoreT: Store,
{
map.insert("kind".into(), self.id.kind.as_str().into());
map.insert("id".into(), self.id.to_string().into());
if let Some(origin_template_id) = &self.origin_template_id {
map.insert("origin-template-id".into(), origin_template_id.to_string().into());
}
map.insert("metadata".into(), metadata_into_expression(self.metadata));
classes_into_expression(store, map, embedded, self.class_ids)?;
properties_into_expression(store, map, "properties", embedded, self.properties)?;
Ok(())
}
}