sea_core/primitives/
resource_instance.rs1use crate::ConceptId;
2use serde::{Deserialize, Serialize};
3use serde_json::Value;
4use std::collections::HashMap;
5use uuid::Uuid;
6
7const DEFAULT_NAMESPACE: &str = "default";
8
9#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
31pub struct ResourceInstance {
32 id: ConceptId,
33 resource_id: ConceptId,
34 entity_id: ConceptId,
35 namespace: String,
36 attributes: HashMap<String, Value>,
37}
38
39impl ResourceInstance {
40 pub fn new(resource_id: ConceptId, entity_id: ConceptId) -> Self {
42 Self::new_with_namespace(resource_id, entity_id, DEFAULT_NAMESPACE)
43 }
44
45 pub fn new_with_namespace(
46 resource_id: ConceptId,
47 entity_id: ConceptId,
48 namespace: impl Into<String>,
49 ) -> Self {
50 let namespace = namespace.into();
51 let id = ConceptId::from_uuid(Uuid::new_v4());
53
54 Self {
55 id,
56 resource_id,
57 entity_id,
58 namespace,
59 attributes: HashMap::new(),
60 }
61 }
62
63 pub fn id(&self) -> &ConceptId {
64 &self.id
65 }
66 pub fn resource_id(&self) -> &ConceptId {
67 &self.resource_id
68 }
69 pub fn entity_id(&self) -> &ConceptId {
70 &self.entity_id
71 }
72 pub fn namespace(&self) -> &str {
73 &self.namespace
74 }
75
76 pub fn set_attribute(&mut self, key: impl Into<String>, value: Value) {
77 self.attributes.insert(key.into(), value);
78 }
79
80 pub fn get_attribute(&self, key: &str) -> Option<&Value> {
81 self.attributes.get(key)
82 }
83
84 pub fn attributes(&self) -> &HashMap<String, Value> {
86 &self.attributes
87 }
88}