Skip to content

Commit b7ea0c5

Browse files
committed
fix: improve map rendering
1 parent ad0c451 commit b7ea0c5

3 files changed

Lines changed: 28 additions & 42 deletions

File tree

rust/operator-binary/src/controller.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ mod build;
6262
mod dereference;
6363
mod validate;
6464

65+
use build::properties::render_java_properties;
6566
pub use validate::{TrinoRoleGroupConfig, ValidatedCluster};
6667

6768
use crate::{
@@ -554,14 +555,9 @@ fn build_rolegroup_catalog_config_map(
554555
catalogs
555556
.iter()
556557
.map(|catalog| {
557-
let catalog_props: BTreeMap<String, String> = catalog
558-
.properties
559-
.iter()
560-
.map(|(k, v)| (k.to_string(), v.to_string()))
561-
.collect();
562558
Ok((
563559
format!("{}.properties", catalog.name),
564-
build::properties::to_java_properties_string(&catalog_props)
560+
render_java_properties(catalog.properties.clone())
565561
.context(FailedToWriteJavaPropertiesSnafu)?,
566562
))
567563
})

rust/operator-binary/src/controller/build/config_map.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::{
1919
build::properties::{
2020
ConfigFileName, access_control_properties, config_properties,
2121
exchange_manager_properties, log_properties, logging::get_vector_toml, node_properties,
22-
security_properties, spooling_manager_properties, to_java_properties_string,
22+
render_java_properties, security_properties, spooling_manager_properties,
2323
},
2424
},
2525
crd::{TrinoRole, v1alpha1},
@@ -65,7 +65,6 @@ pub enum Error {
6565

6666
type Result<T, E = Error> = std::result::Result<T, E>;
6767

68-
#[allow(clippy::too_many_arguments)]
6968
pub fn build_rolegroup_config_map(
7069
cluster: &ValidatedCluster,
7170
role: &TrinoRole,
@@ -94,10 +93,9 @@ pub fn build_rolegroup_config_map(
9493
// Auth files (e.g. password-authenticator file contents) — inserted FIRST
9594
// to match the legacy precedence.
9695
for (file_name, props) in cluster.cluster_config.authentication.config_files(role) {
97-
let rendered =
98-
to_java_properties_string(&props).with_context(|_| WritePropertiesSnafu {
99-
file: file_name.clone(),
100-
})?;
96+
let rendered = render_java_properties(props).with_context(|_| WritePropertiesSnafu {
97+
file: file_name.clone(),
98+
})?;
10199
data.insert(file_name, rendered);
102100
}
103101

@@ -106,7 +104,7 @@ pub fn build_rolegroup_config_map(
106104
.context(BuildConfigPropertiesSnafu)?;
107105
data.insert(
108106
ConfigFileName::Config.to_string(),
109-
to_java_properties_string(&cfg).with_context(|_| WritePropertiesSnafu {
107+
render_java_properties(cfg).with_context(|_| WritePropertiesSnafu {
110108
file: ConfigFileName::Config.to_string(),
111109
})?,
112110
);
@@ -115,7 +113,7 @@ pub fn build_rolegroup_config_map(
115113
let node = node_properties::build(cluster, rg);
116114
data.insert(
117115
ConfigFileName::Node.to_string(),
118-
to_java_properties_string(&node).with_context(|_| WritePropertiesSnafu {
116+
render_java_properties(node).with_context(|_| WritePropertiesSnafu {
119117
file: ConfigFileName::Node.to_string(),
120118
})?,
121119
);
@@ -125,7 +123,7 @@ pub fn build_rolegroup_config_map(
125123
if !log.is_empty() {
126124
data.insert(
127125
ConfigFileName::Log.to_string(),
128-
to_java_properties_string(&log).with_context(|_| WritePropertiesSnafu {
126+
render_java_properties(log).with_context(|_| WritePropertiesSnafu {
129127
file: ConfigFileName::Log.to_string(),
130128
})?,
131129
);
@@ -135,7 +133,7 @@ pub fn build_rolegroup_config_map(
135133
let sec = security_properties::build(rg);
136134
data.insert(
137135
ConfigFileName::Security.to_string(),
138-
to_java_properties_string(&sec).with_context(|_| WritePropertiesSnafu {
136+
render_java_properties(sec).with_context(|_| WritePropertiesSnafu {
139137
file: ConfigFileName::Security.to_string(),
140138
})?,
141139
);
@@ -145,7 +143,7 @@ pub fn build_rolegroup_config_map(
145143
if !ac.is_empty() {
146144
data.insert(
147145
ConfigFileName::AccessControl.to_string(),
148-
to_java_properties_string(&ac).with_context(|_| WritePropertiesSnafu {
146+
render_java_properties(ac).with_context(|_| WritePropertiesSnafu {
149147
file: ConfigFileName::AccessControl.to_string(),
150148
})?,
151149
);
@@ -156,7 +154,7 @@ pub fn build_rolegroup_config_map(
156154
if !em.is_empty() {
157155
data.insert(
158156
ConfigFileName::ExchangeManager.to_string(),
159-
to_java_properties_string(&em).with_context(|_| WritePropertiesSnafu {
157+
render_java_properties(em).with_context(|_| WritePropertiesSnafu {
160158
file: ConfigFileName::ExchangeManager.to_string(),
161159
})?,
162160
);
@@ -167,7 +165,7 @@ pub fn build_rolegroup_config_map(
167165
if !sm.is_empty() {
168166
data.insert(
169167
ConfigFileName::SpoolingManager.to_string(),
170-
to_java_properties_string(&sm).with_context(|_| WritePropertiesSnafu {
168+
render_java_properties(sm).with_context(|_| WritePropertiesSnafu {
171169
file: ConfigFileName::SpoolingManager.to_string(),
172170
})?,
173171
);

rust/operator-binary/src/controller/build/properties/mod.rs

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use std::collections::BTreeMap;
99

1010
use stackable_operator::v2::{
11-
config_file_writer::{self, PropertiesWriterError},
11+
config_file_writer::{PropertiesWriterError, to_java_properties_string},
1212
config_overrides::KeyValueConfigOverrides,
1313
};
1414

@@ -40,6 +40,19 @@ pub enum ConfigFileName {
4040
SpoolingManager,
4141
}
4242

43+
/// Render a `key -> value` properties map to the Java `.properties` on-wire format.
44+
///
45+
/// The upstream [`to_java_properties_string`] consumes an iterator of
46+
/// `(&String, &Option<String>)`, so we lift the plain `String` values into
47+
/// `Some(..)` first.
48+
pub(crate) fn render_java_properties(
49+
props: BTreeMap<String, String>,
50+
) -> Result<String, PropertiesWriterError> {
51+
let props: BTreeMap<String, Option<String>> =
52+
props.into_iter().map(|(k, v)| (k, Some(v))).collect();
53+
to_java_properties_string(props.iter())
54+
}
55+
4356
/// Keep only the set (`Some`) entries of a `key -> optional value` map, as `(key, value)` pairs.
4457
fn defined_entries(
4558
entries: BTreeMap<String, Option<String>>,
@@ -57,27 +70,6 @@ fn resolved_overrides(
5770
defined_entries(overrides.overrides)
5871
}
5972

60-
/// Serialize `props` as a Java-properties string, sorted by key, via the shared
61-
/// [`config_file_writer`] — the same `product-config`-derived writer used by all
62-
/// other operators (and by this operator before the product-config refactoring;
63-
/// a hand-rolled escaper briefly lived here on this branch but accidentally
64-
/// diverged from it on non-ASCII and control characters, so it was dropped in
65-
/// favour of the shared implementation).
66-
///
67-
/// This adapter maps trino's `String`-valued property maps onto the writer's
68-
/// `Option<String>`-valued interface. Alternative considered: converting at
69-
/// each of the nine call sites — rejected as repetitive; the clone here is
70-
/// negligible for config-sized maps.
71-
pub fn to_java_properties_string(
72-
props: &BTreeMap<String, String>,
73-
) -> Result<String, PropertiesWriterError> {
74-
let props: BTreeMap<String, Option<String>> = props
75-
.iter()
76-
.map(|(k, v)| (k.clone(), Some(v.clone())))
77-
.collect();
78-
config_file_writer::to_java_properties_string(props.iter())
79-
}
80-
8173
#[cfg(test)]
8274
pub(crate) mod test_support {
8375
use stackable_operator::cli::OperatorEnvironmentOptions;
@@ -138,7 +130,7 @@ mod tests {
138130
.iter()
139131
.map(|(k, v)| ((*k).to_string(), (*v).to_string()))
140132
.collect();
141-
to_java_properties_string(&props).expect("rendering the test properties should succeed")
133+
render_java_properties(props).expect("rendering the test properties should succeed")
142134
}
143135

144136
/// The escape behaviours pinned by the kuttl smoke snapshot

0 commit comments

Comments
 (0)