-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds to define if defaults are applied to config loader. (#223)
* Adds command line property source. * Adds map property source and tests for map and command line. * Adds to define if defaults are applied to config loader. Co-authored-by: Frederic Kneier <[email protected]>
- Loading branch information
1 parent
4837f59
commit cad34b4
Showing
4 changed files
with
208 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
hoplite-core/src/main/kotlin/com/sksamuel/hoplite/ConfigLoaderBuilderExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.sksamuel.hoplite | ||
|
||
import java.io.File | ||
import java.io.InputStream | ||
import java.nio.file.Path | ||
|
||
/** | ||
* Returns a [PropertySource] that will read the specified resource from the classpath. | ||
* | ||
* @param optional if true then the resource can not exist and the config loader will ignore this source | ||
*/ | ||
fun ConfigLoader.Builder.addResourceSource(resource: String, optional: Boolean = false) = addSource( | ||
ConfigFilePropertySource(ConfigSource.ClasspathSource(resource), optional = optional) | ||
) | ||
|
||
/** | ||
* Returns a [PropertySource] that will read the specified file from the filesystem. | ||
* | ||
* @param optional if true then the resource can not exist and the config loader will ignore this source | ||
*/ | ||
fun ConfigLoader.Builder.addFileSource(file: File, optional: Boolean = false) = addSource( | ||
ConfigFilePropertySource(ConfigSource.FileSource(file), optional = optional) | ||
) | ||
|
||
/** | ||
* Returns a [PropertySource] that will read the specified resource from the classpath. | ||
* | ||
* @param optional if true then the resource can not exist and the config loader will ignore this source | ||
*/ | ||
fun ConfigLoader.Builder.addPathSource(path: Path, optional: Boolean = false) = addSource( | ||
ConfigFilePropertySource(ConfigSource.PathSource(path), optional = optional) | ||
) | ||
|
||
/** | ||
* Returns a [PropertySource] that will read the specified input stream. | ||
* | ||
* @param input the input stream to read from | ||
* @param ext the file extension of the input format | ||
*/ | ||
fun ConfigLoader.Builder.addStreamSource(input: InputStream, ext: String) = addSource( | ||
InputStreamPropertySource(input, ext) | ||
) | ||
|
||
/** | ||
* Returns a [PropertySource] that read the specified map values. | ||
* | ||
* @param map map | ||
*/ | ||
fun ConfigLoader.Builder.addMapSource(map: Map<String, Any>) = addSource( | ||
MapPropertySource(map) | ||
) | ||
|
||
/** | ||
* Returns a [PropertySource] that will read the specified command line arguments. | ||
* | ||
* @param arguments command line arguments as passed to main method | ||
* @param prefix argument prefix | ||
* @param delimiter key value delimiter | ||
*/ | ||
fun ConfigLoader.Builder.addCommandLineSource( | ||
arguments: Array<String>, | ||
prefix: String = "--", | ||
delimiter: String = "=", | ||
) = addSource( | ||
CommandLinePropertySource(arguments, prefix, delimiter) | ||
) | ||
|
||
/** | ||
* Returns a [PropertySource] that will read the environment settings. | ||
* | ||
* @param arguments command line arguments as passed to main method | ||
* @param prefix argument prefix | ||
* @param delimiter key value delimiter | ||
*/ | ||
fun ConfigLoader.Builder.addEnvironmentSource( | ||
useUnderscoresAsSeparator: Boolean = true, | ||
allowUppercaseNames: Boolean = true, | ||
) = addSource( | ||
EnvironmentVariablesPropertySource(useUnderscoresAsSeparator, allowUppercaseNames) | ||
) | ||
|
||
/** | ||
* Returns a [PropertySource] that will read from the specified string. | ||
* | ||
* @param str the string to read from | ||
* @param ext the file extension of the input format | ||
*/ | ||
fun ConfigLoader.Builder.string( | ||
str: String, ext: String | ||
) = addStreamSource(str.byteInputStream(), ext) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
hoplite-core/src/test/kotlin/com/sksamuel/hoplite/WithoutDefaultsRegistryTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.sksamuel.hoplite | ||
|
||
import com.sksamuel.hoplite.fp.Validated | ||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.matchers.shouldBe | ||
import io.kotest.matchers.shouldNotBe | ||
import io.kotest.matchers.types.instanceOf | ||
|
||
class WithoutDefaultsRegistryTest : FunSpec() { | ||
init { | ||
test("default registry throws no error") { | ||
val loader = ConfigLoader { | ||
addMapSource(mapOf("custom_value" to "\${PATH}")) | ||
} | ||
val e = loader.loadConfig<Config>() | ||
e as Validated.Valid<Config> | ||
e.value.customValue shouldNotBe "\${path}" | ||
} | ||
|
||
test("empty sources registry throws error") { | ||
val loader = ConfigLoader { | ||
withDefaultSources(false) | ||
addMapSource(mapOf("custom_value" to "\${PATH}")) | ||
} | ||
val e = loader.loadConfig<Config>() | ||
e as Validated.Invalid<ConfigFailure> | ||
e.error shouldBe instanceOf(ConfigFailure.DataClassFieldErrors::class) | ||
} | ||
|
||
test("empty param mappers registry throws error") { | ||
val loader = ConfigLoader { | ||
withDefaultParamMappers(false) | ||
addMapSource(mapOf("custom_value" to "\${PATH}")) | ||
} | ||
|
||
val e = loader.loadConfig<Config>() | ||
e as Validated.Invalid<ConfigFailure> | ||
e.error shouldBe instanceOf(ConfigFailure.DataClassFieldErrors::class) | ||
} | ||
|
||
test("empty preprocessors registry throws error") { | ||
val loader = ConfigLoader { | ||
withDefaultPreprocessors(false) | ||
addMapSource(mapOf("custom_value" to "\${PATH}")) | ||
} | ||
val e = loader.loadConfig<Config>() | ||
e as Validated.Valid<Config> | ||
e.value.customValue shouldBe "\${PATH}" | ||
} | ||
|
||
} | ||
|
||
data class Config(val PATH: String, val customValue: String) | ||
} |