use std::collections::HashMap;
use cheetah_string::CheetahString;
use serde::Deserialize;
use serde::Serialize;
use crate::protocol::static_topic::topic_queue_mapping_detail::TopicQueueMappingDetail;
use crate::protocol::DataVersion;
#[derive(Clone, Debug, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct TopicQueueMappingSerializeWrapper {
topic_queue_mapping_info_map: Option<HashMap<CheetahString, TopicQueueMappingDetail>>,
data_version: Option<DataVersion>,
}
impl TopicQueueMappingSerializeWrapper {
pub fn new(
topic_queue_mapping_info_map: Option<HashMap<CheetahString, TopicQueueMappingDetail>>,
data_version: Option<DataVersion>,
) -> Self {
Self {
topic_queue_mapping_info_map,
data_version,
}
}
}
impl TopicQueueMappingSerializeWrapper {
pub fn topic_queue_mapping_info_map(&self) -> Option<&HashMap<CheetahString, TopicQueueMappingDetail>> {
self.topic_queue_mapping_info_map.as_ref()
}
pub fn take_topic_queue_mapping_info_map(&mut self) -> Option<HashMap<CheetahString, TopicQueueMappingDetail>> {
self.topic_queue_mapping_info_map.take()
}
pub fn data_version(&self) -> Option<&DataVersion> {
match &self.data_version {
None => None,
Some(value) => Some(value),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn create_test_map() -> HashMap<CheetahString, TopicQueueMappingDetail> {
let mut map = HashMap::new();
let detail = TopicQueueMappingDetail::default();
map.insert(CheetahString::from("test_topic"), detail);
map
}
#[test]
fn test_take_topic_queue_mapping_info_map() {
let map = create_test_map();
let mut wrapper = TopicQueueMappingSerializeWrapper::new(Some(map), None);
assert!(wrapper.data_version().is_none());
let taken_map = wrapper.take_topic_queue_mapping_info_map();
assert!(taken_map.is_some());
assert_eq!(taken_map.unwrap().len(), 1);
assert!(wrapper.topic_queue_mapping_info_map().is_none());
}
#[test]
fn test_serialization_deserialization() {
let map = create_test_map();
let version = DataVersion::default();
let wrapper = TopicQueueMappingSerializeWrapper::new(Some(map), Some(version));
let json = serde_json::to_string(&wrapper).expect("Should serialize");
assert!(json.contains("topicQueueMappingInfoMap"));
assert!(json.contains("dataVersion"));
let deserialized: TopicQueueMappingSerializeWrapper = serde_json::from_str(&json).expect("Should deserialize");
assert_eq!(deserialized.topic_queue_mapping_info_map().unwrap().len(), 1);
}
}