@@ -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+ }
0 commit comments