feat: add unwrap utility function to entity
This commit is contained in:
13
01.workspace/heave/src/imp/bool_from_value.rs
Normal file
13
01.workspace/heave/src/imp/bool_from_value.rs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
use crate::*;
|
||||||
|
|
||||||
|
impl From<Value> for bool {
|
||||||
|
fn from(value: Value) -> bool {
|
||||||
|
match value {
|
||||||
|
Value::Bool(value) => value,
|
||||||
|
_ => panic!("Type mismatch"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// #[cfg(test)]
|
||||||
|
// mod unit_tests { use super::*; }
|
||||||
17
01.workspace/heave/src/imp/entity_from_value.rs
Normal file
17
01.workspace/heave/src/imp/entity_from_value.rs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
use crate::*;
|
||||||
|
|
||||||
|
impl From<Value> for Entity {
|
||||||
|
fn from(value: Value) -> Entity {
|
||||||
|
let id = match value {
|
||||||
|
Value::Text(value) => value,
|
||||||
|
_ => panic!("Type mismatch"),
|
||||||
|
};
|
||||||
|
Entity {
|
||||||
|
id,
|
||||||
|
..Entity::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// #[cfg(test)]
|
||||||
|
// mod unit_tests { use super::*; }
|
||||||
13
01.workspace/heave/src/imp/f64_from_value.rs
Normal file
13
01.workspace/heave/src/imp/f64_from_value.rs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
use crate::*;
|
||||||
|
|
||||||
|
impl From<Value> for f64 {
|
||||||
|
fn from(value: Value) -> f64 {
|
||||||
|
match value {
|
||||||
|
Value::Real(value) => value,
|
||||||
|
_ => panic!("Type mismatch"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// #[cfg(test)]
|
||||||
|
// mod unit_tests { use super::*; }
|
||||||
13
01.workspace/heave/src/imp/i64_from_value.rs
Normal file
13
01.workspace/heave/src/imp/i64_from_value.rs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
use crate::*;
|
||||||
|
|
||||||
|
impl From<Value> for i64 {
|
||||||
|
fn from(value: Value) -> i64 {
|
||||||
|
match value {
|
||||||
|
Value::SignedInt(value) => value,
|
||||||
|
_ => panic!("Type mismatch"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// #[cfg(test)]
|
||||||
|
// mod unit_tests { use super::*; }
|
||||||
@@ -1,7 +1,13 @@
|
|||||||
|
pub mod bool_from_value;
|
||||||
pub mod bool_to_value;
|
pub mod bool_to_value;
|
||||||
|
pub mod entity_from_value;
|
||||||
pub mod entity_to_value;
|
pub mod entity_to_value;
|
||||||
|
pub mod f64_from_value;
|
||||||
pub mod f64_to_value;
|
pub mod f64_to_value;
|
||||||
|
pub mod i64_from_value;
|
||||||
pub mod i64_to_value;
|
pub mod i64_to_value;
|
||||||
pub mod str_to_value;
|
pub mod str_to_value;
|
||||||
|
pub mod string_from_value;
|
||||||
pub mod string_to_value;
|
pub mod string_to_value;
|
||||||
|
pub mod u64_from_value;
|
||||||
pub mod u64_to_value;
|
pub mod u64_to_value;
|
||||||
|
|||||||
13
01.workspace/heave/src/imp/string_from_value.rs
Normal file
13
01.workspace/heave/src/imp/string_from_value.rs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
use crate::*;
|
||||||
|
|
||||||
|
impl From<Value> for String {
|
||||||
|
fn from(value: Value) -> String {
|
||||||
|
match value {
|
||||||
|
Value::Text(value) => value,
|
||||||
|
_ => panic!("Type mismatch"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// #[cfg(test)]
|
||||||
|
// mod unit_tests { use super::*; }
|
||||||
13
01.workspace/heave/src/imp/u64_from_value.rs
Normal file
13
01.workspace/heave/src/imp/u64_from_value.rs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
use crate::*;
|
||||||
|
|
||||||
|
impl From<Value> for u64 {
|
||||||
|
fn from(value: Value) -> u64 {
|
||||||
|
match value {
|
||||||
|
Value::UnsignedInt(value) => value,
|
||||||
|
_ => panic!("Type mismatch"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// #[cfg(test)]
|
||||||
|
// mod unit_tests { use super::*; }
|
||||||
@@ -42,4 +42,11 @@ impl O {
|
|||||||
Some(attribute) => Some(attribute.value),
|
Some(attribute) => Some(attribute.value),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pub fn unwrap<T>(&self, id: &str) -> T
|
||||||
|
where
|
||||||
|
T: From<Value>,
|
||||||
|
{
|
||||||
|
let value = self.value_of(id).unwrap();
|
||||||
|
T::from(value.clone())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,26 @@
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::*;
|
use crate::*;
|
||||||
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
struct Product {
|
||||||
|
pub name: String,
|
||||||
|
pub price: u64,
|
||||||
|
}
|
||||||
|
impl ToEAV for Product {
|
||||||
|
fn to_eav(self) -> Entity {
|
||||||
|
Entity::new("product")
|
||||||
|
.with_attribute("name", self.name)
|
||||||
|
.with_attribute("price", self.price)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl FromEAV for Product {
|
||||||
|
fn from_eav(entity: Entity) -> Product {
|
||||||
|
Product {
|
||||||
|
name: entity.unwrap("name"),
|
||||||
|
price: entity.unwrap("price"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn check_001() {
|
fn check_001() {
|
||||||
// Demonstrates the costruction of a new entity instance
|
// Demonstrates the costruction of a new entity instance
|
||||||
@@ -61,4 +81,39 @@ mod tests {
|
|||||||
Some(&Value::Text(category_id))
|
Some(&Value::Text(category_id))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
#[test]
|
||||||
|
fn check_005() {
|
||||||
|
let tag = Entity::new("tag").with_attribute("label", "new");
|
||||||
|
let tag_id = tag.id.clone();
|
||||||
|
let entity = Entity::new("product")
|
||||||
|
.with_attribute("name", "laptop")
|
||||||
|
.with_attribute("price", 200000u64)
|
||||||
|
.with_attribute("delta", -50i64)
|
||||||
|
.with_attribute("in_stock", true)
|
||||||
|
.with_attribute("discount", 5.2f64)
|
||||||
|
.with_attribute("tag", tag);
|
||||||
|
let name: String = entity.unwrap("name");
|
||||||
|
let price: u64 = entity.unwrap("price");
|
||||||
|
let delta: i64 = entity.unwrap("delta");
|
||||||
|
let in_stock: bool = entity.unwrap("in_stock");
|
||||||
|
let discount: f64 = entity.unwrap("discount");
|
||||||
|
let tag: Entity = entity.unwrap("tag");
|
||||||
|
assert_eq!(name, "laptop".to_string());
|
||||||
|
assert_eq!(price, 200000u64);
|
||||||
|
assert_eq!(delta, -50i64);
|
||||||
|
assert!(in_stock);
|
||||||
|
assert_eq!(discount, 5.2f64);
|
||||||
|
assert_eq!(tag.id, tag_id);
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn check_006() {
|
||||||
|
let product = Product {
|
||||||
|
name: "laptop".to_string(),
|
||||||
|
price: 200000u64,
|
||||||
|
};
|
||||||
|
let expected_product = product.clone();
|
||||||
|
let entity = product.to_eav();
|
||||||
|
let converted_product = Product::from_eav(entity);
|
||||||
|
assert_eq!(expected_product, converted_product);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user