Skip to content

Commit 912efc9

Browse files
apollo_config: add serialize_optional_comma_separated fn (#9646)
1 parent 5dd37f5 commit 912efc9

File tree

7 files changed

+32
-38
lines changed

7 files changed

+32
-38
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/apollo_config/src/converters.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,18 @@ where
273273
.collect()
274274
}
275275

276+
/// Serializes an optional list into a comma-separated string.
277+
/// Returns `None` if the input is `None`.
278+
pub fn serialize_optional_comma_separated<T>(list: &Option<Vec<T>>) -> Option<String>
279+
where
280+
T: ToString,
281+
{
282+
match list {
283+
None => None,
284+
Some(list) => Some(list.iter().map(|item| item.to_string()).collect::<Vec<_>>().join(",")),
285+
}
286+
}
287+
276288
/// Deserializes an optional comma-separated list of values implementing `FromStr` into
277289
/// `Option<Vec<T>>`. Returns `None` for empty or missing strings.
278290
pub fn deserialize_comma_separated_str<'de, D, T>(de: D) -> Result<Option<Vec<T>>, D::Error>

crates/apollo_consensus_orchestrator_config/src/config.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use apollo_config::converters::{
66
deserialize_comma_separated_str,
77
deserialize_milliseconds_to_duration,
88
deserialize_seconds_to_duration,
9+
serialize_optional_comma_separated,
910
};
1011
use apollo_config::dumping::{ser_optional_param, ser_param, SerializeConfig};
1112
use apollo_config::{ParamPath, ParamPrivacyInput, SerializedParam};
@@ -253,9 +254,7 @@ impl SerializeConfig for ContextConfig {
253254
ParamPrivacyInput::Public,
254255
));
255256
dump.extend(ser_optional_param(
256-
&self.validator_ids.as_ref().map(|accounts| {
257-
accounts.iter().map(|addr| addr.0.to_string()).collect::<Vec<_>>().join(",")
258-
}),
257+
&serialize_optional_comma_separated(&self.validator_ids),
259258
"".to_string(),
260259
"validator_ids",
261260
"Optional explicit set of validator IDs (comma separated).",

crates/apollo_deployments/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ apollo_http_server_config.workspace = true
1515
apollo_infra.workspace = true
1616
apollo_infra_utils.workspace = true
1717
apollo_monitoring_endpoint_config.workspace = true
18-
apollo_network.workspace = true
1918
apollo_node_config.workspace = true
2019
indexmap.workspace = true
2120
libp2p.workspace = true

crates/apollo_deployments/src/config_override.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use std::path::Path;
22

3+
use apollo_config::converters::serialize_optional_comma_separated;
34
use apollo_infra_utils::dumping::serialize_to_file;
45
#[cfg(test)]
56
use apollo_infra_utils::dumping::serialize_to_file_test;
6-
use apollo_network::serialize_multi_addrs;
77
use libp2p::Multiaddr;
88
use serde::{Serialize, Serializer};
99
use serde_json::to_value;
@@ -185,7 +185,10 @@ impl DeploymentConfigOverride {
185185
#[derive(Clone, Debug, Serialize, PartialEq)]
186186
pub struct PeerToPeerBootstrapConfig {
187187
// Bootstrap peer address.
188-
#[serde(rename = "bootstrap_peer_multiaddr", serialize_with = "serialize_multi_addrs_wrapper")]
188+
#[serde(
189+
rename = "bootstrap_peer_multiaddr",
190+
serialize_with = "serialize_optional_comma_separated_wrapper"
191+
)]
189192
bootstrap_peers_multiaddrs: Option<Vec<Multiaddr>>,
190193
#[serde(rename = "bootstrap_peer_multiaddr.#is_none")]
191194
bootstrap_peer_multiaddr_is_none: bool,
@@ -244,23 +247,19 @@ impl InstanceConfigOverride {
244247
}
245248
}
246249

247-
// Wrapper function for the custom `serialize_multi_addrs` function, to be
250+
// Wrapper function for the generic `serialize_optional_comma_separated` function, to be
248251
// compatible with serde's `serialize_with` attribute. It first applies the custom serialization
249252
// logic to convert the optional list into a `String`, and then serializes that string.
250-
fn serialize_multi_addrs_wrapper<S>(
251-
optional_multi_addrs: &Option<Vec<Multiaddr>>,
253+
fn serialize_optional_comma_separated_wrapper<S, T>(
254+
optional_list: &Option<Vec<T>>,
252255
serializer: S,
253256
) -> Result<S::Ok, S::Error>
254257
where
255258
S: Serializer,
259+
T: ToString,
256260
{
257-
match optional_multi_addrs {
261+
match serialize_optional_comma_separated(optional_list) {
258262
None => serializer.serialize_none(),
259-
Some(multi_addrs) => {
260-
// Call the implemented custom serialization function
261-
let s = serialize_multi_addrs(&Some(multi_addrs.clone()));
262-
// Serialize the returned String
263-
serializer.serialize_some(&s)
264-
}
263+
Some(s) => serializer.serialize_some(&s),
265264
}
266265
}

crates/apollo_gateway_config/src/config.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use std::collections::BTreeMap;
22

3-
use apollo_config::converters::deserialize_comma_separated_str;
3+
use apollo_config::converters::{
4+
deserialize_comma_separated_str,
5+
serialize_optional_comma_separated,
6+
};
47
use apollo_config::dumping::{
58
prepend_sub_config_name,
69
ser_optional_param,
@@ -47,9 +50,7 @@ impl SerializeConfig for GatewayConfig {
4750
));
4851
dump.extend(prepend_sub_config_name(self.chain_info.dump(), "chain_info"));
4952
dump.extend(ser_optional_param(
50-
&self.authorized_declarer_accounts.as_ref().map(|accounts| {
51-
accounts.iter().map(|addr| addr.0.to_string()).collect::<Vec<_>>().join(",")
52-
}),
53+
&serialize_optional_comma_separated(&self.authorized_declarer_accounts),
5354
"".to_string(),
5455
"authorized_declarer_accounts",
5556
"Authorized declarer accounts. If set, only these accounts can declare new contracts. \

crates/apollo_network/src/lib.rs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use apollo_config::converters::{
2525
deserialize_comma_separated_str,
2626
deserialize_optional_vec_u8,
2727
deserialize_seconds_to_duration,
28+
serialize_optional_comma_separated,
2829
serialize_optional_vec_u8,
2930
};
3031
use apollo_config::dumping::{
@@ -45,18 +46,6 @@ use validator::{Validate, ValidationError};
4546

4647
pub(crate) type Bytes = Vec<u8>;
4748

48-
// TODO(Tsabary): move to the config converter module.
49-
pub fn serialize_multi_addrs(multi_addrs: &Option<Vec<Multiaddr>>) -> String {
50-
match multi_addrs {
51-
None => "".to_owned(),
52-
Some(multi_addrs) => multi_addrs
53-
.iter()
54-
.map(|multiaddr| multiaddr.to_string())
55-
.collect::<Vec<String>>()
56-
.join(","),
57-
}
58-
}
59-
6049
// TODO(Shahak): add peer manager config to the network config
6150
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Validate)]
6251
pub struct NetworkConfig {
@@ -123,11 +112,7 @@ impl SerializeConfig for NetworkConfig {
123112
// TODO(Tsabary): this is not the proper way to dump a config. Needs fixing, and
124113
// specifically, need to move the condition to be part of the serialization fn.
125114
config.extend(ser_optional_param(
126-
&if self.bootstrap_peer_multiaddr.is_some(){
127-
Some(serialize_multi_addrs(&self.bootstrap_peer_multiaddr))
128-
} else {
129-
None
130-
},
115+
&serialize_optional_comma_separated(&self.bootstrap_peer_multiaddr),
131116
String::from(""),
132117
"bootstrap_peer_multiaddr",
133118
"The multiaddress of the peer node. It should include the peer's id. For more info: https://docs.libp2p.io/concepts/fundamentals/peers/",

0 commit comments

Comments
 (0)