Skip to content

Commit

Permalink
fix: 修复部分场景下openapi naming model中的使用unwrap解析空参数发生panic的问题 #151
Browse files Browse the repository at this point in the history
  • Loading branch information
heqingpan committed Oct 21, 2024
1 parent f1802b8 commit ee94b25
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/openapi/naming/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,19 @@ impl InstanceWebParams {
}

pub(crate) fn convert_to_instance(self) -> Result<Instance, String> {
let ip = if let Some(ip) = self.ip {
Arc::new(ip)
} else {
return Err("the instance ip is None".to_string());
};
let port = if let Some(port) = self.port {
port
} else {
return Err("the instance port is None".to_string());
};
let mut instance = Instance {
ip: Arc::new(self.ip.unwrap()),
port: self.port.unwrap(),
ip,
port,
weight: self.weight.unwrap_or(1f32),
enabled: get_bool_from_string(&self.enabled, true),
healthy: true,
Expand All @@ -64,7 +74,7 @@ impl InstanceWebParams {
..Default::default()
};

let grouped_name = self.service_name.unwrap();
let grouped_name = self.service_name.unwrap_or_default();
if let Some((group_name, service_name)) =
NamingUtils::split_group_and_serivce_name(&grouped_name)
{
Expand Down Expand Up @@ -108,7 +118,7 @@ impl InstanceWebQueryListParams {
pub(crate) fn to_clusters_key(&self) -> Result<(ServiceKey, String), String> {
let mut service_name = "".to_owned();
let mut group_name = "".to_owned();
let grouped_name = self.service_name.as_ref().unwrap().to_owned();
let grouped_name = self.service_name.clone().unwrap_or_default();
if let Some((_group_name, _service_name)) =
NamingUtils::split_group_and_serivce_name(&grouped_name)
{
Expand Down Expand Up @@ -303,8 +313,8 @@ pub struct BeatInfo {
impl BeatInfo {
pub fn convert_to_instance(self) -> Instance {
let mut instance = Instance {
ip: Arc::new(self.ip.unwrap()),
port: self.port.unwrap(),
ip: Arc::new(self.ip.unwrap_or("unknown".to_string())),
port: self.port.unwrap_or(1),
cluster_name: NamingUtils::default_cluster(
self.cluster.as_ref().unwrap_or(&"".to_owned()).to_owned(),
),
Expand Down

0 comments on commit ee94b25

Please sign in to comment.