Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KTOR-5000 Fix merging nested configs #3212

Merged
merged 1 commit into from Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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