@@ -15,7 +15,7 @@ public class YamlConfigLoader : ConfigLoader {
1515 /* *
1616 * Tries loading an application configuration from the specified [path].
1717 *
18- * @return configuration or null if the path is not found or configuration format is not supported.
18+ * @return configuration or null if the path is not found or a configuration format is not supported.
1919 */
2020 override fun load (path : String? ): ApplicationConfig ? {
2121 return YamlConfig (path)?.apply { checkEnvironmentVariables() }
@@ -24,14 +24,14 @@ public class YamlConfigLoader : ConfigLoader {
2424
2525/* *
2626 * Loads a configuration from the YAML file, if found.
27- * On JVM, loads a configuration from application resources, if exist ; otherwise, reads a configuration from a file.
27+ * On JVM, loads a configuration from application resources, if exists ; otherwise, reads a configuration from a file.
2828 * On Native, always reads a configuration from a file.
2929 */
3030public expect fun YamlConfig (path : String? ): YamlConfig ?
3131
3232/* *
3333 * Implements [ApplicationConfig] by loading a configuration from a YAML file.
34- * Values can reference to environment variables with `$ENV_VAR` syntax.
34+ * Values can reference to environment variables with `$ENV_VAR` or `"$ENV_VAR:default_value"` syntax.
3535 */
3636public class YamlConfig (private val yaml : YamlMap ) : ApplicationConfig {
3737
@@ -125,9 +125,18 @@ public class YamlConfig(private val yaml: YamlMap) : ApplicationConfig {
125125private fun resolveValue (value : String ): String {
126126 val isEnvVariable = value.startsWith(" \$ " )
127127 if (! isEnvVariable) return value
128- val key = value.drop(1 )
128+ val keyWithDefault = value.drop(1 )
129+ val separatorIndex = keyWithDefault.indexOf(' :' )
130+ val (key, default) = if (separatorIndex == - 1 ) {
131+ keyWithDefault to null
132+ } else {
133+ keyWithDefault.substring(0 , separatorIndex) to keyWithDefault.substring(separatorIndex + 1 )
134+ }
129135 return getEnvironmentValue(key)
130- ? : throw ApplicationConfigurationException (" Environment variable \" $key \" not found" )
136+ ? : default
137+ ? : throw ApplicationConfigurationException (
138+ " Environment variable \" $key \" not found and no default value is present"
139+ )
131140}
132141
133142internal expect fun getEnvironmentValue (key : String ): String?
0 commit comments