Skip to content

Commit

Permalink
[ISSUE #1511]♻️Refactor NotifyMinBrokerIdChangeRequestHeader with der…
Browse files Browse the repository at this point in the history
…ive marco RequestHeaderCodec
  • Loading branch information
mxsm committed Jan 18, 2025
1 parent eb6d6b1 commit 465767f
Showing 1 changed file with 38 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,12 @@
* limitations under the License.
*/

use std::collections::HashMap;

use anyhow::Error;
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, Serialize, Deserialize, Default)]
#[derive(Debug, Clone, Serialize, Deserialize, Default, RequestHeaderCodec)]

Check warning on line 23 in rocketmq-remoting/src/protocol/header/namesrv/brokerid_change_request_header.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-remoting/src/protocol/header/namesrv/brokerid_change_request_header.rs#L23

Added line #L23 was not covered by tests
pub struct NotifyMinBrokerIdChangeRequestHeader {
#[serde(rename = "minBrokerId")]
pub min_broker_id: Option<u64>,
Expand All @@ -44,12 +39,6 @@ pub struct NotifyMinBrokerIdChangeRequestHeader {
}

impl NotifyMinBrokerIdChangeRequestHeader {
const BROKER_NAME: &'static str = "brokerName";
const HA_BROKER_ADDR: &'static str = "haBrokerAddr";
const MIN_BROKER_ADDR: &'static str = "minBrokerAddr";
const MIN_BROKER_ID: &'static str = "minBrokerId";
const OFFLINE_BROKER_ADDR: &'static str = "offlineBrokerAddr";

pub fn new(
min_broker_id: Option<u64>,
broker_name: Option<CheetahString>,
Expand All @@ -67,97 +56,45 @@ impl NotifyMinBrokerIdChangeRequestHeader {
}
}

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

type Target = Self;

fn from(map: &HashMap<CheetahString, CheetahString>) -> Result<Self::Target, Self::Error> {
Ok(NotifyMinBrokerIdChangeRequestHeader {
min_broker_id: map
.get(&CheetahString::from_static_str(
NotifyMinBrokerIdChangeRequestHeader::MIN_BROKER_ID,
))
.and_then(|s| s.parse::<u64>().ok()),
broker_name: map
.get(&CheetahString::from_static_str(
NotifyMinBrokerIdChangeRequestHeader::BROKER_NAME,
))
.cloned(),
min_broker_addr: map
.get(&CheetahString::from_static_str(
NotifyMinBrokerIdChangeRequestHeader::MIN_BROKER_ADDR,
))
.cloned(),
offline_broker_addr: map
.get(&CheetahString::from_static_str(
NotifyMinBrokerIdChangeRequestHeader::OFFLINE_BROKER_ADDR,
))
.cloned(),
ha_broker_addr: map
.get(&CheetahString::from_static_str(
NotifyMinBrokerIdChangeRequestHeader::HA_BROKER_ADDR,
))
.cloned(),
})
#[cfg(test)]
mod tests {
use cheetah_string::CheetahString;

use super::*;

#[test]
fn new_creates_instance_with_all_fields() {
let header = NotifyMinBrokerIdChangeRequestHeader::new(
Some(1),
Some(CheetahString::from("broker1")),
Some(CheetahString::from("addr1")),
Some(CheetahString::from("addr2")),
Some(CheetahString::from("addr3")),
);
assert_eq!(header.min_broker_id, Some(1));
assert_eq!(header.broker_name.as_deref(), Some("broker1"));
assert_eq!(header.min_broker_addr.as_deref(), Some("addr1"));
assert_eq!(header.offline_broker_addr.as_deref(), Some("addr2"));
assert_eq!(header.ha_broker_addr.as_deref(), Some("addr3"));
}
}

impl CommandCustomHeader for NotifyMinBrokerIdChangeRequestHeader {
fn check_fields(&self) -> anyhow::Result<(), Error> {
todo!()
#[test]
fn new_creates_instance_with_none_fields() {
let header = NotifyMinBrokerIdChangeRequestHeader::new(None, None, None, None, None);
assert_eq!(header.min_broker_id, None);
assert_eq!(header.broker_name, None);
assert_eq!(header.min_broker_addr, None);
assert_eq!(header.offline_broker_addr, None);
assert_eq!(header.ha_broker_addr, None);
}

fn to_map(&self) -> Option<HashMap<CheetahString, CheetahString>> {
let mut map = HashMap::<CheetahString, CheetahString>::new();
if let Some(min_broker_id) = self.min_broker_id {
map.insert(
CheetahString::from_static_str(NotifyMinBrokerIdChangeRequestHeader::MIN_BROKER_ID),
CheetahString::from_string(min_broker_id.to_string()),
);
}
if let Some(ref broker_name) = self.broker_name {
map.insert(
CheetahString::from_static_str(NotifyMinBrokerIdChangeRequestHeader::BROKER_NAME),
broker_name.clone(),
);
}
if let Some(ref min_broker_addr) = self.min_broker_addr {
map.insert(
CheetahString::from_static_str(
NotifyMinBrokerIdChangeRequestHeader::MIN_BROKER_ADDR,
),
min_broker_addr.clone(),
);
}

if let Some(ref ha_broker_addr) = self.ha_broker_addr {
map.insert(
CheetahString::from_static_str(
NotifyMinBrokerIdChangeRequestHeader::HA_BROKER_ADDR,
),
ha_broker_addr.clone(),
);
}

if let Some(ref offline_broker_addr) = self.offline_broker_addr {
map.insert(
CheetahString::from_static_str(
NotifyMinBrokerIdChangeRequestHeader::OFFLINE_BROKER_ADDR,
),
offline_broker_addr.clone(),
);
}

if let Some(ref ha_broker_addr) = self.ha_broker_addr {
map.insert(
CheetahString::from_static_str(
NotifyMinBrokerIdChangeRequestHeader::HA_BROKER_ADDR,
),
ha_broker_addr.clone(),
);
}

Some(map)
#[test]
fn default_creates_instance_with_none_fields() {
let header: NotifyMinBrokerIdChangeRequestHeader = Default::default();
assert_eq!(header.min_broker_id, None);
assert_eq!(header.broker_name, None);
assert_eq!(header.min_broker_addr, None);
assert_eq!(header.offline_broker_addr, None);
assert_eq!(header.ha_broker_addr, None);
}
}

0 comments on commit 465767f

Please sign in to comment.