Skip to content

Commit

Permalink
KTOR-5000 Fix merging nested configs (#3212)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsinukov committed Oct 19, 2022
1 parent 928d405 commit 3a10900
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
Expand Up @@ -30,15 +30,11 @@ internal class MergedApplicationConfig(
else -> second.propertyOrNull(path)
}

override fun config(path: String): ApplicationConfig = when {
firstKeys.contains(path) -> first.config(path)
else -> second.config(path)
}
override fun config(path: String): ApplicationConfig =
MergedApplicationConfig(first.config(path), second.config(path))

override fun configList(path: String): List<ApplicationConfig> = when {
firstKeys.contains(path) -> first.configList(path)
else -> second.configList(path)
}
override fun configList(path: String): List<ApplicationConfig> =
first.configList(path) + second.configList(path)

override fun keys(): Set<String> = firstKeys + second.keys()

Expand Down
Expand Up @@ -17,6 +17,39 @@ class MergedApplicationConfigTest {
assertEquals("value3", config.property("second").getString())
}

@Test
fun testConfig() {
val first = MapApplicationConfig("first.second.third1" to "value1", "first.second.third2" to "value2")
val second = MapApplicationConfig("first.second.third1" to "ignored", "first.second.third3" to "value3")
val config = MergedApplicationConfig(first, second)
val mergedNestedConfig = config.config("first.second")
assertEquals("value1", mergedNestedConfig.property("third1").getString())
assertEquals("value2", mergedNestedConfig.property("third2").getString())
assertEquals("value3", mergedNestedConfig.property("third3").getString())
assertEquals(setOf("third1", "third2", "third3"), mergedNestedConfig.keys())
}

@Test
fun testConfigList() {
val first = MapApplicationConfig(
"first.second.size" to "2",
"first.second.0.third" to "value1",
"first.second.1.third" to "value2"
)
val second = MapApplicationConfig(
"first.second.size" to "2",
"first.second.0.third" to "value3",
"first.second.1.third" to "value4"
)
val config = MergedApplicationConfig(first, second)
val configs = config.configList("first.second")
assertEquals(4, configs.size)
assertEquals("value1", configs[0].property("third").getString())
assertEquals("value2", configs[1].property("third").getString())
assertEquals("value3", configs[2].property("third").getString())
assertEquals("value4", configs[3].property("third").getString())
}

@Test
fun testPropertyOrNull() {
val first = MapApplicationConfig("first" to "value1")
Expand Down

0 comments on commit 3a10900

Please sign in to comment.