Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Check params for Config/Naming #240

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/api/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ pub enum Error {
#[error("get result failed: {0}")]
ErrResult(String),

#[error("param:{0} with error_message:{1}")]
InvalidParam(String, String),

#[error("request_id:{0:?} ret_code:{1} error_code:{2} message:{3:?}")]
ErrResponse(Option<String>, i32, i32, Option<String>),

Expand Down
1 change: 1 addition & 0 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub(crate) mod cache;
pub(crate) mod executor;
pub(crate) mod remote;
pub(crate) mod util;
39 changes: 39 additions & 0 deletions src/common/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use crate::api::error::Error;
use crate::api::error::Result;

/// Checks param_val not blank
pub(crate) fn check_not_blank<'a>(param_val: &'a str, param_name: &'a str) -> Result<&'a str> {
if param_val.trim().is_empty() {
Err(Error::InvalidParam(
param_name.into(),
"param must not blank!".into(),
))
} else {
Ok(param_val)
}
}

#[cfg(test)]
mod tests {
use crate::common::util::check_not_blank;

#[test]
fn test_check_not_blank() {
let data_id = "data_id";
let group = "group";
let namespace = "namespace";

assert_eq!(data_id, check_not_blank(data_id, "data_id").unwrap());
assert_eq!(group, check_not_blank(group, "group").unwrap());
assert_eq!(namespace, check_not_blank(namespace, "namespace").unwrap());
}

#[test]
fn test_check_not_blank_fail() {
let data_id = "";
assert!(check_not_blank(data_id, "data_id").is_err());

let data_id = " ";
assert!(check_not_blank(data_id, "data_id").is_err());
}
}
22 changes: 22 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ impl ConfigService for NacosConfigService {
data_id: String,
group: String,
) -> crate::api::error::Result<crate::api::config::ConfigResponse> {
crate::common::util::check_not_blank(&data_id, "data_id")?;
crate::common::util::check_not_blank(&group, "group")?;
self.client_worker.get_config(data_id, group).await
}

Expand All @@ -69,6 +71,9 @@ impl ConfigService for NacosConfigService {
content: String,
content_type: Option<String>,
) -> crate::api::error::Result<bool> {
crate::common::util::check_not_blank(&data_id, "data_id")?;
crate::common::util::check_not_blank(&group, "group")?;
crate::common::util::check_not_blank(&content, "content")?;
self.client_worker
.publish_config(data_id, group, content, content_type)
.await
Expand All @@ -83,6 +88,10 @@ impl ConfigService for NacosConfigService {
content_type: Option<String>,
cas_md5: String,
) -> crate::api::error::Result<bool> {
crate::common::util::check_not_blank(&data_id, "data_id")?;
crate::common::util::check_not_blank(&group, "group")?;
crate::common::util::check_not_blank(&content, "content")?;
crate::common::util::check_not_blank(&cas_md5, "cas_md5")?;
self.client_worker
.publish_config_cas(data_id, group, content, content_type, cas_md5)
.await
Expand All @@ -97,6 +106,10 @@ impl ConfigService for NacosConfigService {
content_type: Option<String>,
beta_ips: String,
) -> crate::api::error::Result<bool> {
crate::common::util::check_not_blank(&data_id, "data_id")?;
crate::common::util::check_not_blank(&group, "group")?;
crate::common::util::check_not_blank(&content, "content")?;
crate::common::util::check_not_blank(&beta_ips, "beta_ips")?;
self.client_worker
.publish_config_beta(data_id, group, content, content_type, beta_ips)
.await
Expand All @@ -112,6 +125,9 @@ impl ConfigService for NacosConfigService {
cas_md5: Option<String>,
params: std::collections::HashMap<String, String>,
) -> crate::api::error::Result<bool> {
crate::common::util::check_not_blank(&data_id, "data_id")?;
crate::common::util::check_not_blank(&group, "group")?;
crate::common::util::check_not_blank(&content, "content")?;
self.client_worker
.publish_config_param(data_id, group, content, content_type, cas_md5, params)
.await
Expand All @@ -123,6 +139,8 @@ impl ConfigService for NacosConfigService {
data_id: String,
group: String,
) -> crate::api::error::Result<bool> {
crate::common::util::check_not_blank(&data_id, "data_id")?;
crate::common::util::check_not_blank(&group, "group")?;
self.client_worker.remove_config(data_id, group).await
}

Expand All @@ -133,6 +151,8 @@ impl ConfigService for NacosConfigService {
group: String,
listener: std::sync::Arc<dyn crate::api::config::ConfigChangeListener>,
) -> crate::api::error::Result<()> {
crate::common::util::check_not_blank(&data_id, "data_id")?;
crate::common::util::check_not_blank(&group, "group")?;
self.client_worker
.add_listener(data_id, group, listener)
.await;
Expand All @@ -146,6 +166,8 @@ impl ConfigService for NacosConfigService {
group: String,
listener: std::sync::Arc<dyn crate::api::config::ConfigChangeListener>,
) -> crate::api::error::Result<()> {
crate::common::util::check_not_blank(&data_id, "data_id")?;
crate::common::util::check_not_blank(&group, "group")?;
self.client_worker
.remove_listener(data_id, group, listener)
.await;
Expand Down
8 changes: 8 additions & 0 deletions src/naming/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,7 @@ impl NamingService for NacosNamingService {
group_name: Option<String>,
service_instance: ServiceInstance,
) -> Result<()> {
crate::common::util::check_not_blank(&service_name, "service_name")?;
if service_instance.ephemeral {
self.register_ephemeral_instance_async(service_name, group_name, service_instance)
.await
Expand All @@ -668,6 +669,7 @@ impl NamingService for NacosNamingService {
group_name: Option<String>,
service_instance: ServiceInstance,
) -> Result<()> {
crate::common::util::check_not_blank(&service_name, "service_name")?;
if service_instance.ephemeral {
self.deregister_ephemeral_instance_async(service_name, group_name, service_instance)
.await
Expand All @@ -684,6 +686,7 @@ impl NamingService for NacosNamingService {
group_name: Option<String>,
service_instances: Vec<ServiceInstance>,
) -> Result<()> {
crate::common::util::check_not_blank(&service_name, "service_name")?;
self.batch_register_instance_async(service_name, group_name, service_instances)
.await
}
Expand All @@ -696,6 +699,7 @@ impl NamingService for NacosNamingService {
clusters: Vec<String>,
subscribe: bool,
) -> Result<Vec<ServiceInstance>> {
crate::common::util::check_not_blank(&service_name, "service_name")?;
self.get_all_instances_async(service_name, group_name, clusters, subscribe)
.await
}
Expand All @@ -709,6 +713,7 @@ impl NamingService for NacosNamingService {
subscribe: bool,
healthy: bool,
) -> Result<Vec<ServiceInstance>> {
crate::common::util::check_not_blank(&service_name, "service_name")?;
self.select_instances_async(service_name, group_name, clusters, subscribe, healthy)
.await
}
Expand All @@ -721,6 +726,7 @@ impl NamingService for NacosNamingService {
clusters: Vec<String>,
subscribe: bool,
) -> Result<ServiceInstance> {
crate::common::util::check_not_blank(&service_name, "service_name")?;
self.select_one_healthy_instance_async(service_name, group_name, clusters, subscribe)
.await
}
Expand All @@ -744,6 +750,7 @@ impl NamingService for NacosNamingService {
clusters: Vec<String>,
event_listener: Arc<dyn NamingEventListener>,
) -> Result<()> {
crate::common::util::check_not_blank(&service_name, "service_name")?;
let _ = self
.subscribe_async(service_name, group_name, clusters, Some(event_listener))
.await;
Expand All @@ -758,6 +765,7 @@ impl NamingService for NacosNamingService {
clusters: Vec<String>,
event_listener: Arc<dyn NamingEventListener>,
) -> Result<()> {
crate::common::util::check_not_blank(&service_name, "service_name")?;
self.unsubscribe_async(service_name, group_name, clusters, Some(event_listener))
.await
}
Expand Down
Loading