Не подтверждена Коммит 07210204 создал по автору Mariia Skripchenko's avatar Mariia Skripchenko Зафиксировано автором GitHub
Просмотр файлов

KTOR-5208 Add ApplicationConfig merge with reversed priority (#3258)

KTOR-5208 Add ApplicationConfig merge with reversed priority
владелец 4e98d3ba
......@@ -314,6 +314,8 @@ protected final class io/ktor/server/config/MapApplicationConfig$MapApplicationC
public final class io/ktor/server/config/MergedApplicationConfigKt {
public static final fun merge (Ljava/util/List;)Lio/ktor/server/config/ApplicationConfig;
public static final fun mergeWith (Lio/ktor/server/config/ApplicationConfig;Lio/ktor/server/config/ApplicationConfig;)Lio/ktor/server/config/ApplicationConfig;
public static final fun withFallback (Lio/ktor/server/config/ApplicationConfig;Lio/ktor/server/config/ApplicationConfig;)Lio/ktor/server/config/ApplicationConfig;
}
public final class io/ktor/server/http/HttpDateJvmKt {
......
......@@ -8,9 +8,30 @@ package io.ktor.server.config
* Merge configuration combining all their keys.
* If key is not found in one of the configs, search will continue in the next config in the list.
*/
@Deprecated("Use mergeWith/withFallback instead.")
public fun List<ApplicationConfig>.merge(): ApplicationConfig {
require(isNotEmpty()) { "List of configs can not be empty" }
return foldRight(last()) { config, acc -> MergedApplicationConfig(config, acc) }
return foldRight(last()) { config, acc -> config.withFallback(acc) }
}
/**
* Merge configuration combining all their keys.
* If the key exists in this and [other] config, the value from the [other] config will be used.
*
* @see [withFallback]
*/
public fun ApplicationConfig.mergeWith(other: ApplicationConfig): ApplicationConfig {
return MergedApplicationConfig(other, this)
}
/**
* Merge configuration combining all their keys.
* If the key exists in this and [other] config, the value from this config will be used.
*
* @see [mergeWith]
*/
public fun ApplicationConfig.withFallback(other: ApplicationConfig): ApplicationConfig {
return MergedApplicationConfig(this, other)
}
internal class MergedApplicationConfig(
......
......@@ -98,4 +98,44 @@ class MergedApplicationConfigTest {
val config = MergedApplicationConfig(first, second)
assertEquals(mapOf("first" to "value1", "second" to "value3"), config.toMap())
}
@Test
fun testMergeWith() {
val first = MapApplicationConfig(
"value1" to "1",
"value2" to "2",
"value3" to "3",
"value4" to "4"
)
val second = MapApplicationConfig(
"value1" to "2",
"value2" to "2",
"value3" to "1",
)
val mergedConfig = first.mergeWith(second)
assertEquals("2", mergedConfig.property("value1").getString())
assertEquals("2", mergedConfig.property("value2").getString())
assertEquals("1", mergedConfig.property("value3").getString())
assertEquals("4", mergedConfig.property("value4").getString())
}
@Test
fun testMergeWithFallback() {
val first = MapApplicationConfig(
"value1" to "1",
"value2" to "2",
"value3" to "3"
)
val second = MapApplicationConfig(
"value1" to "2",
"value2" to "2",
"value3" to "1",
"value4" to "4"
)
val mergedConfig = first.withFallback(second)
assertEquals("1", mergedConfig.property("value1").getString())
assertEquals("2", mergedConfig.property("value2").getString())
assertEquals("3", mergedConfig.property("value3").getString())
assertEquals("4", mergedConfig.property("value4").getString())
}
}
......@@ -101,7 +101,7 @@ internal fun buildApplicationConfig(args: Array<String>): ApplicationConfig {
val environmentConfig = getConfigFromEnvironment()
val fileConfig = ConfigLoader.load(configPath)
return listOf(commandLineConfig, environmentConfig, fileConfig).merge()
return fileConfig.mergeWith(environmentConfig).mergeWith(commandLineConfig)
}
internal expect fun ApplicationEngineEnvironmentBuilder.configureSSLConnectors(
......
Поддерживает Markdown
0% или .
You are about to add 0 people to the discussion. Proceed with caution.
Сначала завершите редактирование этого сообщения!
Пожалуйста, зарегистрируйтесь или чтобы прокомментировать