Skip to content

Commit e7f0c87

Browse files
committed
Add test
1 parent f8f5d7c commit e7f0c87

2 files changed

Lines changed: 47 additions & 14 deletions

File tree

rust/operator-binary/src/controller/dereference.rs

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,10 @@ pub async fn dereference(
118118
let mut catalogs = Vec::with_capacity(catalog_definitions.len());
119119
for catalog in &catalog_definitions {
120120
let catalog_ref = ObjectRef::from_obj(catalog);
121-
// We are using a match here, as we might support other ways of naming (e.g. custom) later
122-
let catalog_name = match catalog.spec.name {
123-
catalog::v1alpha1::TrinoCatalogNameSpec::Inferred {
124-
replace_hyphens_with_underscores,
125-
} => {
126-
let mut catalog_name = catalog.name().context(ObjectHasNoNameSnafu)?.to_string();
127-
if replace_hyphens_with_underscores {
128-
catalog_name = catalog_name.replace('-', "_");
129-
}
130-
TrinoCatalogName::from_str(&catalog_name)
131-
.with_context(|_| InvalidTrinoCatalogNameSnafu { catalog_name })?
132-
}
133-
};
121+
let catalog_name = determine_catalog_name(
122+
&catalog.spec.name,
123+
&catalog.name().context(ObjectHasNoNameSnafu)?,
124+
)?;
134125
let catalog_config = CatalogConfig::from_catalog(
135126
&catalog_name,
136127
catalog,
@@ -189,3 +180,46 @@ pub async fn dereference(
189180
resolved_client_protocol_config,
190181
})
191182
}
183+
184+
/// Determines the Trino catalog name based on user settings and the Kubernetes TrinoCatalog object
185+
/// name.
186+
fn determine_catalog_name(
187+
catalog_name_spec: &catalog::v1alpha1::TrinoCatalogNameSpec,
188+
catalog_object_name: &str,
189+
) -> Result<TrinoCatalogName> {
190+
// A `match` is used because we might support other ways of naming (e.g. custom) later.
191+
match catalog_name_spec {
192+
catalog::v1alpha1::TrinoCatalogNameSpec::Inferred {
193+
replace_hyphens_with_underscores,
194+
} => {
195+
let mut catalog_name = catalog_object_name.to_owned();
196+
if *replace_hyphens_with_underscores {
197+
catalog_name = catalog_name.replace('-', "_");
198+
}
199+
TrinoCatalogName::from_str(&catalog_name)
200+
.with_context(|_| InvalidTrinoCatalogNameSnafu { catalog_name })
201+
}
202+
}
203+
}
204+
205+
#[cfg(test)]
206+
mod tests {
207+
use super::*;
208+
209+
#[test]
210+
fn determine_catalog_name_replaces_hyphens() {
211+
let inferred = |replace_hyphens_with_underscores| {
212+
determine_catalog_name(
213+
&catalog::v1alpha1::TrinoCatalogNameSpec::Inferred {
214+
replace_hyphens_with_underscores,
215+
},
216+
"my-postgres",
217+
)
218+
.expect("catalog name should be valid")
219+
.to_string()
220+
};
221+
222+
assert_eq!(inferred(false), "my-postgres");
223+
assert_eq!(inferred(true), "my_postgres");
224+
}
225+
}

rust/operator-binary/src/crd/catalog/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ attributed_string_type! {
143143
// As of 2026-07 the longest suffix is for a volume name (63 characters limit) and is
144144
// "-sheets-credentials" (19 characters).
145145
(max_length = 40),
146-
is_rfc_1035_label_name,
147146
is_valid_label_value
148147
}
149148

0 commit comments

Comments
 (0)