diff --git a/telers/Cargo.toml b/telers/Cargo.toml index a46dd45..dfcc216 100644 --- a/telers/Cargo.toml +++ b/telers/Cargo.toml @@ -17,8 +17,8 @@ full = ["storages"] storages = ["redis-storage", "memory-storage"] # For possible use redis FSM storage redis-storage = ["redis", "deadpool-redis"] -# For possible use memory FSM storage -memory-storage = ["bincode"] +# For possible use memory FSM storage. +memory-storage = [] [dependencies] telers-macros = { path = "../telers-macros", version = "1.0.0-alpha.2", features = ["default"] } @@ -46,7 +46,6 @@ uuid = { version = "1.7", features = ["v4"] } redis = { version = "0.25", features = ["tokio-comp"], optional = true } deadpool-redis = {version = "0.15", optional = true } -bincode = { version = "1.3", optional = true } [dev-dependencies] tokio-test = "0.4" diff --git a/telers/src/fsm/storage/memory.rs b/telers/src/fsm/storage/memory.rs index f5698a4..23c461a 100644 --- a/telers/src/fsm/storage/memory.rs +++ b/telers/src/fsm/storage/memory.rs @@ -13,7 +13,7 @@ use tracing::{event, instrument, Level, Span}; #[derive(Debug, Default, Clone, PartialEq, Eq)] struct Record { states: Vec>, - data: HashMap, Box<[u8]>>, + data: HashMap, Box>, } /// This is a simple thread-safe in-memory storage implementation used for testing purposes usually @@ -167,7 +167,7 @@ impl Storage for Memory { for (value_key, value) in data { new_data.insert( value_key.into(), - bincode::serialize(&value) + serde_json::to_string(&value) .map_err(|err| { event!(Level::ERROR, "Failed to serialize value"); @@ -196,7 +196,7 @@ impl Storage for Memory { for (value_key, value) in data { new_data.insert( value_key.into(), - bincode::serialize(&value) + serde_json::to_string(&value) .map_err(|err| { event!(Level::ERROR, "Failed to serialize value"); @@ -242,7 +242,7 @@ impl Storage for Memory { Entry::Occupied(mut entry) => { entry.get_mut().data.insert( value_key, - bincode::serialize(&value) + serde_json::to_string(&value) .map_err(|err| { event!(Level::ERROR, "Failed to serialize value"); @@ -261,7 +261,7 @@ impl Storage for Memory { let mut new_data = HashMap::with_capacity(1); new_data.insert( value_key, - bincode::serialize(&value) + serde_json::to_string(&value) .map_err(|err| { event!(Level::ERROR, "Failed to serialize value"); @@ -303,7 +303,7 @@ impl Storage for Memory { for (value_key, value) in entry_data { data.insert( value_key.as_ref().into(), - bincode::deserialize(value).map_err(|err| { + serde_json::from_str(value).map_err(|err| { event!(Level::ERROR, "Failed to deserialize value"); Error::new( @@ -342,7 +342,7 @@ impl Storage for Memory { match self.storage.lock().await.entry(key.clone()) { Entry::Occupied(entry) => entry.get().data.get(&value_key).map_or(Ok(None), |value| { - Ok(Some(bincode::deserialize(value).map_err(|err| { + Ok(Some(serde_json::from_str(value).map_err(|err| { event!(Level::ERROR, "Failed to deserialize value"); Error::new( @@ -373,6 +373,8 @@ impl Storage for Memory { #[cfg(test)] mod tests { use super::*; + use serde::{Deserialize, Serialize}; + use serde_with::skip_serializing_none; #[tokio::test] async fn test_state() { @@ -542,4 +544,82 @@ mod tests { Some("value1") ); } + + /// Test for issue #27 + /// https://github.com/Desiders/telers/issues/27 + #[tokio::test] + async fn test_data_issue_27() { + #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] + struct AOption { + a: Option, + } + + #[skip_serializing_none] + #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] + struct AOptionSkip { + a: Option, + } + + let storage = Memory::default(); + + let key1 = StorageKey::new(0, 1, 2, None, None); + let key2 = StorageKey::new(2, 1, 0, None, None); + let key3 = StorageKey::new(2, 2, 1, None, None); + let key4 = StorageKey::new(1, 0, 0, None, None); + + let value1 = AOption { + a: Some("a".to_owned()), + }; + let value2 = AOption { a: None }; + let value3 = AOptionSkip { + a: Some("a".to_owned()), + }; + let value4 = AOptionSkip { a: None }; + + storage + .set_value(&key1, "key1", value1.clone()) + .await + .unwrap(); + storage + .set_value(&key2, "key2", value2.clone()) + .await + .unwrap(); + storage + .set_value(&key3, "key3", value3.clone()) + .await + .unwrap(); + storage + .set_value(&key4, "key4", value4.clone()) + .await + .unwrap(); + + assert_eq!( + storage + .get_value::<_, AOption>(&key1, "key1") + .await + .unwrap(), + Some(value1) + ); + assert_eq!( + storage + .get_value::<_, AOption>(&key2, "key2") + .await + .unwrap(), + Some(value2) + ); + assert_eq!( + storage + .get_value::<_, AOptionSkip>(&key3, "key3") + .await + .unwrap(), + Some(value3) + ); + assert_eq!( + storage + .get_value::<_, AOptionSkip>(&key4, "key4") + .await + .unwrap(), + Some(value4) + ); + } }