Skip to content

Commit 76a4438

Browse files
committed
refactor: clean up config retrieval
1 parent 886a25f commit 76a4438

3 files changed

Lines changed: 8 additions & 12 deletions

File tree

src/main/kotlin/io/sdkman/broker/config/AppConfig.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,11 @@ class DefaultAppConfig : AppConfig {
4848

4949
// Persistence backend selector
5050
override val persistenceBackend: PersistenceBackend =
51-
config.getStringOrDefault("persistence.backend", PersistenceBackend.Mongo.configValue).let { raw ->
52-
PersistenceBackend.fromConfigValue(raw).getOrElse {
51+
config.getOptionString("persistence.backend")
52+
.flatMap { PersistenceBackend.fromConfigValue(it) }
53+
.getOrElse {
5354
throw IllegalArgumentException(
54-
"Invalid PERSISTENCE_BACKEND value '$raw'. " +
55-
"Supported values: ${PersistenceBackend.supportedValues.joinToString(", ")}."
55+
"PERSISTENCE_BACKEND value not found or incorrect. Supported values: mongo, postgres."
5656
)
5757
}
58-
}
5958
}
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.sdkman.broker.config
22

33
import arrow.core.Option
4-
import arrow.core.toOption
4+
import arrow.core.getOrNone
55

66
enum class PersistenceBackend(
77
val configValue: String
@@ -13,8 +13,6 @@ enum class PersistenceBackend(
1313
private val configValueToBackend: Map<String, PersistenceBackend> =
1414
entries.associateBy { it.configValue }
1515

16-
fun fromConfigValue(value: String): Option<PersistenceBackend> = configValueToBackend[value].toOption()
17-
18-
val supportedValues: List<String> = entries.map { it.configValue }
16+
fun fromConfigValue(value: String): Option<PersistenceBackend> = configValueToBackend.getOrNone(value)
1917
}
2018
}

src/test/kotlin/io/sdkman/broker/config/AppConfigSpec.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,7 @@ class AppConfigSpec :
126126

127127
// when/then: construction must blow up so the broker never serves traffic against an unsupported backend
128128
val error = shouldThrow<IllegalArgumentException> { DefaultAppConfig() }
129-
error.message shouldContain "cassandra"
130-
error.message shouldContain "mongo"
131-
error.message shouldContain "postgres"
129+
error.message shouldContain "PERSISTENCE_BACKEND value not found or incorrect."
130+
error.message shouldContain "Supported values: mongo, postgres."
132131
}
133132
})

0 commit comments

Comments
 (0)