Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
max-dfinity committed Sep 27, 2024
1 parent f15e5cc commit e309ad7
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 20 deletions.
12 changes: 7 additions & 5 deletions rs/protobuf/def/registry/node/v1/node.proto
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ message IPv4InterfaceConfig {
enum NodeType {
NODE_TYPE_UNSPECIFIED = 0;
// type0
NODE_TYPE_Type0 = 1;
NODE_TYPE_TYPE0 = 1;
// type1
NODE_TYPE_Type1 = 2;
NODE_TYPE_TYPE1 = 2;
// type2
NODE_TYPE_Type2 = 3;
NODE_TYPE_TYPE2 = 3;
// type3
NODE_TYPE_Type3 = 4;
NODE_TYPE_TYPE3 = 4;
// type3.1
NODE_TYPE_Type3dot1 = 5;
NODE_TYPE_TYPE3DOT1 = 5;
// type1.1
NODE_TYPE_TYPE1DOT1 = 6;
}

// A node: one machine running a replica instance.
Expand Down
24 changes: 14 additions & 10 deletions rs/protobuf/src/gen/registry/registry.node.v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ pub enum NodeType {
Type3 = 4,
/// type3.1
Type3dot1 = 5,
/// type1.1
Type1dot1 = 6,
}
impl NodeType {
/// String value of the enum field names used in the ProtoBuf definition.
Expand All @@ -92,22 +94,24 @@ impl NodeType {
pub fn as_str_name(&self) -> &'static str {
match self {
NodeType::Unspecified => "NODE_TYPE_UNSPECIFIED",
NodeType::Type0 => "NODE_TYPE_Type0",
NodeType::Type1 => "NODE_TYPE_Type1",
NodeType::Type2 => "NODE_TYPE_Type2",
NodeType::Type3 => "NODE_TYPE_Type3",
NodeType::Type3dot1 => "NODE_TYPE_Type3dot1",
NodeType::Type0 => "NODE_TYPE_TYPE0",
NodeType::Type1 => "NODE_TYPE_TYPE1",
NodeType::Type2 => "NODE_TYPE_TYPE2",
NodeType::Type3 => "NODE_TYPE_TYPE3",
NodeType::Type3dot1 => "NODE_TYPE_TYPE3DOT1",
NodeType::Type1dot1 => "NODE_TYPE_TYPE1DOT1",
}
}
/// Creates an enum from field names used in the ProtoBuf definition.
pub fn from_str_name(value: &str) -> ::core::option::Option<Self> {
match value {
"NODE_TYPE_UNSPECIFIED" => Some(Self::Unspecified),
"NODE_TYPE_Type0" => Some(Self::Type0),
"NODE_TYPE_Type1" => Some(Self::Type1),
"NODE_TYPE_Type2" => Some(Self::Type2),
"NODE_TYPE_Type3" => Some(Self::Type3),
"NODE_TYPE_Type3dot1" => Some(Self::Type3dot1),
"NODE_TYPE_TYPE0" => Some(Self::Type0),
"NODE_TYPE_TYPE1" => Some(Self::Type1),
"NODE_TYPE_TYPE2" => Some(Self::Type2),
"NODE_TYPE_TYPE3" => Some(Self::Type3),
"NODE_TYPE_TYPE3DOT1" => Some(Self::Type3dot1),
"NODE_TYPE_TYPE1DOT1" => Some(Self::Type1dot1),
_ => None,
}
}
Expand Down
19 changes: 19 additions & 0 deletions rs/protobuf/src/registry/node.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
use crate::registry::node::v1::NodeType;
use std::fmt::Display;

#[allow(clippy::all)]
#[path = "../gen/registry/registry.node.v1.rs"]
pub mod v1;

impl From<NodeType> for String {
fn from(value: NodeType) -> Self {
match value {
NodeType::Unspecified => {
panic!("Cannot create a node type string from unspecified")
}
NodeType::Type0 => "type0".to_string(),
NodeType::Type1 => "type1".to_string(),
NodeType::Type2 => "type2".to_string(),
NodeType::Type3 => "type3".to_string(),
NodeType::Type3dot1 => "type3.1".to_string(),
NodeType::Type1dot1 => "type1.1".to_string(),
}
}
}
3 changes: 2 additions & 1 deletion rs/registry/canister/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ pub struct AddNodePayload {
pub prometheus_metrics_endpoint: String,

// String representation of the node type. Must be a valid type.
// Currently valid types are type0, type1, type2, type3, and type3.1.
// See registry/canister/src/mutations/do_add_node.rs, fn `try_str_to_node_type` for currently
// accepted types.
pub node_type: Option<String>,
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{common::LOG_PREFIX, registry::Registry};
use std::fmt::Display;

use std::net::SocketAddr;

Expand Down Expand Up @@ -80,7 +81,7 @@ impl Registry {
.node_type
.as_ref()
.map(|t| {
try_str_to_node_type(t).map_err(|e| {
validate_str_as_node_type(t).map_err(|e| {
format!(
"{}do_add_node: Error parsing node type from payload: {}",
LOG_PREFIX, e
Expand Down Expand Up @@ -160,14 +161,16 @@ impl Registry {
}
}

// try to convert input string inot NodeType enum
fn try_str_to_node_type(type_string: &str) -> Result<NodeType, String> {
Ok(match type_string {
// try to convert input string into NodeType enum
// If a type is no longer supported for newly registered nodes, it should be removed from this function
fn validate_str_as_node_type<T: AsRef<str> + Display>(type_string: T) -> Result<NodeType, String> {
Ok(match type_string.as_ref() {
"type0" => NodeType::Type0,
"type1" => NodeType::Type1,
"type2" => NodeType::Type2,
"type3" => NodeType::Type3,
"type3.1" => NodeType::Type3dot1,
"type1.1" => NodeType::Type1dot1,
_ => return Err(format!("Invalid node type: {}", type_string)),
})
}
Expand Down

0 comments on commit e309ad7

Please sign in to comment.