Skip to content

Commit

Permalink
[ISSUE #1512]♻️Refactor PutKVConfigRequestHeader with derive marco Re…
Browse files Browse the repository at this point in the history
…questHeaderCodec (#2133)
  • Loading branch information
mxsm authored Jan 7, 2025
1 parent 794da8e commit 2834153
Showing 1 changed file with 7 additions and 59 deletions.
66 changes: 7 additions & 59 deletions rocketmq-remoting/src/protocol/header/namesrv/kv_config_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,26 @@
use std::collections::HashMap;

use cheetah_string::CheetahString;
use rocketmq_macros::RequestHeaderCodec;
use serde::Deserialize;
use serde::Serialize;

use crate::protocol::command_custom_header::CommandCustomHeader;
use crate::protocol::command_custom_header::FromMap;

#[derive(Debug, Clone, Deserialize, Serialize, Default)]
#[derive(Debug, Clone, Deserialize, Serialize, Default, RequestHeaderCodec)]
pub struct PutKVConfigRequestHeader {
#[required]
pub namespace: CheetahString,

#[required]
pub key: CheetahString,

#[required]
pub value: CheetahString,
}

impl PutKVConfigRequestHeader {
const KEY: &'static str = "key";
const NAMESPACE: &'static str = "namespace";
const VALUE: &'static str = "value";

/// Creates a new instance of `PutKVConfigRequestHeader`.
///
/// # Arguments
Expand All @@ -56,60 +58,6 @@ impl PutKVConfigRequestHeader {
}
}

impl CommandCustomHeader for PutKVConfigRequestHeader {
fn to_map(&self) -> Option<HashMap<CheetahString, CheetahString>> {
Some(HashMap::from([
(
CheetahString::from_static_str(PutKVConfigRequestHeader::NAMESPACE),
self.namespace.clone(),
),
(
CheetahString::from_static_str(PutKVConfigRequestHeader::KEY),
self.key.clone(),
),
(
CheetahString::from_static_str(PutKVConfigRequestHeader::VALUE),
self.value.clone(),
),
]))
}
}

impl FromMap for PutKVConfigRequestHeader {
type Error = crate::remoting_error::RemotingError;

type Target = PutKVConfigRequestHeader;

fn from(map: &HashMap<CheetahString, CheetahString>) -> Result<Self::Target, Self::Error> {
Ok(PutKVConfigRequestHeader {
namespace: map
.get(&CheetahString::from_static_str(
PutKVConfigRequestHeader::NAMESPACE,
))
.cloned()
.ok_or(Self::Error::RemotingCommandError(
"Miss namespace field".to_string(),
))?,
key: map
.get(&CheetahString::from_static_str(
PutKVConfigRequestHeader::KEY,
))
.cloned()
.ok_or(Self::Error::RemotingCommandError(
"Miss key field".to_string(),
))?,
value: map
.get(&CheetahString::from_static_str(
PutKVConfigRequestHeader::VALUE,
))
.cloned()
.ok_or(Self::Error::RemotingCommandError(
"Miss value field".to_string(),
))?,
})
}
}

#[derive(Debug, Clone, Deserialize, Serialize, Default)]
pub struct GetKVConfigRequestHeader {
pub namespace: CheetahString,
Expand Down

0 comments on commit 2834153

Please sign in to comment.