feat: add utility functions with_id and with_class for entity construction
This commit is contained in:
@@ -12,9 +12,17 @@ impl O {
|
|||||||
Self {
|
Self {
|
||||||
id: short_uuid::short!().to_string(),
|
id: short_uuid::short!().to_string(),
|
||||||
class: String::from(class),
|
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 {
|
pub fn with_attribute(mut self, id: &str, value: impl ToValue) -> Self {
|
||||||
let attribute = Attribute::new(id, value);
|
let attribute = Attribute::new(id, value);
|
||||||
self.attributes.insert(attribute.id.clone(), attribute);
|
self.attributes.insert(attribute.id.clone(), attribute);
|
||||||
|
|||||||
@@ -3,12 +3,15 @@ mod tests {
|
|||||||
use crate::*;
|
use crate::*;
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
struct Product {
|
struct Product {
|
||||||
|
pub id: String,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub price: u64,
|
pub price: u64,
|
||||||
}
|
}
|
||||||
impl ToEAV for Product {
|
impl ToEAV for Product {
|
||||||
fn to_eav(self) -> Entity {
|
fn to_eav(self) -> Entity {
|
||||||
Entity::new("product")
|
Entity::default()
|
||||||
|
.with_class("product")
|
||||||
|
.with_id(&self.id)
|
||||||
.with_attribute("name", self.name)
|
.with_attribute("name", self.name)
|
||||||
.with_attribute("price", self.price)
|
.with_attribute("price", self.price)
|
||||||
}
|
}
|
||||||
@@ -16,6 +19,7 @@ mod tests {
|
|||||||
impl FromEAV for Product {
|
impl FromEAV for Product {
|
||||||
fn from_eav(entity: Entity) -> Product {
|
fn from_eav(entity: Entity) -> Product {
|
||||||
Product {
|
Product {
|
||||||
|
id: entity.id.clone(),
|
||||||
name: entity.unwrap("name"),
|
name: entity.unwrap("name"),
|
||||||
price: entity.unwrap("price"),
|
price: entity.unwrap("price"),
|
||||||
}
|
}
|
||||||
@@ -108,6 +112,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn check_006() {
|
fn check_006() {
|
||||||
let product = Product {
|
let product = Product {
|
||||||
|
id: "abcdef".to_string(),
|
||||||
name: "laptop".to_string(),
|
name: "laptop".to_string(),
|
||||||
price: 200000u64,
|
price: 200000u64,
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user