Skip to content

Commit

Permalink
Added order to defaults (Sources, Preprocessors and ParamMappers) (#229)
Browse files Browse the repository at this point in the history
* Added order to defaults (Sources, Preprocessors and ParamMappers)

* Revert "Added order to defaults (Sources, Preprocessors and ParamMappers)"

This reverts commit 4964b19

* Added order to defaults by, when manually added, adding them where specified
  • Loading branch information
David Gomes authored Oct 31, 2021
1 parent 4e9f584 commit 4839b61
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,23 @@ class ConfigLoader constructor(
private var defaultPreprocessors = true
private var defaultParamMappers = true

/**
* Adds before the specified ones
*/
fun withDefaultSources(defaultSources: Boolean): Builder = apply {
this.defaultSources = defaultSources
}

/**
* Adds before the specified ones
*/
fun withDefaultPreprocessors(defaultPreprocessors: Boolean): Builder = apply {
this.defaultPreprocessors = defaultPreprocessors
}

/**
* Adds before the specified ones
*/
fun withDefaultParamMappers(defaultParamMappers: Boolean): Builder = apply {
this.defaultParamMappers = defaultParamMappers
}
Expand Down Expand Up @@ -167,10 +176,22 @@ class ConfigLoader constructor(

fun addSources(sources: Iterable<PropertySource>) = addPropertySources(sources)

fun addDefaultSources(): Builder {
withDefaultSources(false)

return addPropertySources(defaultPropertySources())
}

fun addPropertySources(propertySources: Iterable<PropertySource>): Builder = apply {
this.propertySourceStaging.addAll(propertySources)
}

fun addDefaultPropertySources(): Builder {
withDefaultSources(false)

return addPropertySources(defaultPropertySources())
}

fun addPreprocessor(preprocessor: Preprocessor): Builder = apply {
this.preprocessorStaging.add(preprocessor)
}
Expand All @@ -179,6 +200,12 @@ class ConfigLoader constructor(
this.preprocessorStaging.addAll(preprocessors)
}

fun addDefaultPreprocessors(): Builder {
withDefaultPreprocessors(false)

return addPreprocessors(defaultPreprocessors())
}

fun addParameterMapper(paramMapper: ParameterMapper): Builder = apply {
this.paramMapperStaging.add(paramMapper)
}
Expand All @@ -187,6 +214,12 @@ class ConfigLoader constructor(
this.paramMapperStaging.addAll(paramMappers)
}

fun addDefaultParameterMappers(): Builder {
withDefaultParamMappers(false)

return addParameterMappers(defaultParamMappers())
}

/**
* Registers a callback that will be invoked with any exception generated when
* the [loadConfigOrThrow] operation is used. The callback will be invoked immediately
Expand Down Expand Up @@ -374,7 +407,7 @@ class ConfigLoader constructor(
private fun loadNode(configs: List<ConfigSource>): ConfigResult<Node> {
val srcs = propertySources + configs.map { ConfigFilePropertySource(it) }
return srcs.map { it.node(PropertySourceContext(parserRegistry)) }.sequence()
.map { it.takeUnless { it.isEmpty() }?.reduce { acc, b -> acc.merge(b) } ?: NullNode(Pos.NoPos)}
.map { it.takeUnless { it.isEmpty() }?.reduce { acc, b -> acc.merge(b) } ?: NullNode(Pos.NoPos) }
.mapInvalid {
val multipleFailures = ConfigFailure.MultipleFailures(it)
multipleFailures
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.sksamuel.hoplite

import io.kotest.core.spec.style.FunSpec
import io.kotest.extensions.system.withEnvironment
import io.kotest.matchers.shouldBe

class PropertySourceTest : FunSpec() {
Expand Down Expand Up @@ -76,5 +77,49 @@ class PropertySourceTest : FunSpec() {
config shouldBe TestConfig("A value", 42, listOf("Value1", "Value2"))
}

test("reads from added source before defaults") {
data class TestConfig(val a: String, val b: Int, val other: List<String>)

withEnvironment(mapOf("b" to "91", "other" to "Random13")) {

val arguments = arrayOf(
"--a=A value",
"--b=42",
"some other value",
"--other=Value1",
"--other=Value2",
)

val config = ConfigLoader.Builder()
.addPropertySource(PropertySource.commandLine(arguments))
.addDefaultPropertySources()
.build()
.loadConfigOrThrow<TestConfig>()

config shouldBe TestConfig("A value", 42, listOf("Value1", "Value2"))
}
}

test("reads from default source before specified") {
data class TestConfig(val a: String, val b: Int, val other: List<String>)

withEnvironment(mapOf("b" to "91", "other" to "Random13")) {
val arguments = arrayOf(
"--a=A value",
"--b=42",
"some other value",
"--other=Value1",
"--other=Value2",
)

val config = ConfigLoader.Builder()
.addDefaultPropertySources()
.addPropertySource(PropertySource.commandLine(arguments))
.build()
.loadConfigOrThrow<TestConfig>()

config shouldBe TestConfig("A value", 91, listOf("Random13"))
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.sksamuel.hoplite.preprocessor

import com.sksamuel.hoplite.ConfigLoader
import io.kotest.core.spec.style.StringSpec
import io.kotest.extensions.system.withEnvironment
import io.kotest.matchers.shouldBe

class PropsPreprocessorTest : StringSpec() {
Expand All @@ -12,11 +13,46 @@ class PropsPreprocessorTest : StringSpec() {

val preprocessor = PropsPreprocessor("/sample.properties")

val config = ConfigLoader()
.withPreprocessor(preprocessor)
val config = ConfigLoader.Builder()
.addPreprocessor(preprocessor)
.build()
.loadConfigOrThrow<Config>("/processme.props")

config shouldBe Config(a = "I'm on branch master", b = "this replacement doesn't exist \${foo}")
}

"should replace props from file, by defaults before specified" {
data class Config(val a: String, val b: String)

withEnvironment(mapOf("git.branch" to "main")) {

val preprocessor = PropsPreprocessor("/sample.properties")

val config = ConfigLoader.Builder()
.addDefaultPreprocessors()
.addPreprocessor(preprocessor)
.build()
.loadConfigOrThrow<Config>("/processme.props")

config shouldBe Config(a = "I'm on branch main", b = "this replacement doesn't exist \${foo}")
}
}

"should replace props from file, by specified before defaults" {
data class Config(val a: String, val b: String)

withEnvironment(mapOf("git.branch" to "main", "foo" to "nevermind, it does!")) {
val preprocessor = PropsPreprocessor("/sample.properties")

val config = ConfigLoader.Builder()
.addPreprocessor(preprocessor)
.addDefaultPreprocessors()
.build()
.loadConfigOrThrow<Config>("/processme.props")

config shouldBe Config(a = "I'm on branch master",
b = "this replacement doesn't exist nevermind, it does!")
}
}
}
}

0 comments on commit 4839b61

Please sign in to comment.