feat: add utility functions with_id and with_class for entity construction

This commit is contained in:
2025-09-29 11:28:22 +02:00
parent 342c971ce3
commit 3a8c000665
2 changed files with 15 additions and 2 deletions

View File

@@ -12,9 +12,17 @@ impl O {
Self {
id: short_uuid::short!().to_string(),
class: String::from(class),
attributes: std::collections::HashMap::new(),
..Entity::default()
}
}
pub fn with_id(mut self, id: &str) -> Self {
self.id = id.to_string();
self
}
pub fn with_class(mut self, class: &str) -> Self {
self.class = class.to_string();
self
}
pub fn with_attribute(mut self, id: &str, value: impl ToValue) -> Self {
let attribute = Attribute::new(id, value);
self.attributes.insert(attribute.id.clone(), attribute);

View File

@@ -3,12 +3,15 @@ mod tests {
use crate::*;
#[derive(Debug, Clone, PartialEq)]
struct Product {
pub id: String,
pub name: String,
pub price: u64,
}
impl ToEAV for Product {
fn to_eav(self) -> Entity {
Entity::new("product")
Entity::default()
.with_class("product")
.with_id(&self.id)
.with_attribute("name", self.name)
.with_attribute("price", self.price)
}
@@ -16,6 +19,7 @@ mod tests {
impl FromEAV for Product {
fn from_eav(entity: Entity) -> Product {
Product {
id: entity.id.clone(),
name: entity.unwrap("name"),
price: entity.unwrap("price"),
}
@@ -108,6 +112,7 @@ mod tests {
#[test]
fn check_006() {
let product = Product {
id: "abcdef".to_string(),
name: "laptop".to_string(),
price: 200000u64,
};