forked from sksamuel/hoplite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support an idiomatic environment source
Resolves sksamuel#410.
- Loading branch information
1 parent
37d1662
commit c3fa46a
Showing
9 changed files
with
312 additions
and
6 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
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
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
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
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
60 changes: 60 additions & 0 deletions
60
hoplite-json/src/test/kotlin/com/sksamuel/hoplite/json/AmbiguousPropertyNameTest.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,60 @@ | ||
package com.sksamuel.hoplite.json | ||
|
||
import com.sksamuel.hoplite.ConfigLoader | ||
import com.sksamuel.hoplite.defaultParamMappers | ||
import com.sksamuel.hoplite.normalizer.PathNormalizer | ||
import com.sksamuel.hoplite.sources.EnvironmentVariablesPropertySource | ||
import io.kotest.core.spec.style.DescribeSpec | ||
import io.kotest.matchers.shouldBe | ||
|
||
class AmbiguousPropertyNameTest : DescribeSpec({ | ||
|
||
data class Ambiguous(val someCamelSetting: String, val somecamelsetting: String) | ||
|
||
describe("loading property differing in case from envs") { | ||
it("with path normalizer cannot disambiguate") { | ||
run { | ||
ConfigLoader { | ||
withReport() | ||
addNodeNormalizer(PathNormalizer) | ||
addPropertySource( | ||
EnvironmentVariablesPropertySource( | ||
useUnderscoresAsSeparator = true, | ||
useSingleUnderscoresAsSeparator = false, | ||
allowUppercaseNames = false, | ||
environmentVariableMap = { | ||
mapOf( | ||
"someCamelSetting" to "a", | ||
"somecamelsetting" to "b", | ||
"SOMECAMELSETTING" to "c", | ||
) | ||
} | ||
) | ||
) | ||
}.loadConfigOrThrow<Ambiguous>() | ||
} shouldBe Ambiguous("c", "c") | ||
} | ||
|
||
it("without path normalizer") { | ||
run { | ||
ConfigLoader { | ||
withReport() | ||
addPropertySource( | ||
EnvironmentVariablesPropertySource( | ||
useUnderscoresAsSeparator = true, | ||
useSingleUnderscoresAsSeparator = false, | ||
allowUppercaseNames = false, | ||
environmentVariableMap = { | ||
mapOf( | ||
"someCamelSetting" to "a", | ||
"somecamelsetting" to "b", | ||
"SOMECAMELSETTING" to "c", | ||
) | ||
} | ||
) | ||
) | ||
}.loadConfigOrThrow<Ambiguous>() | ||
} shouldBe Ambiguous("a", "b") | ||
} | ||
} | ||
}) |
126 changes: 126 additions & 0 deletions
126
hoplite-json/src/test/kotlin/com/sksamuel/hoplite/json/EnvPropertySourceNormalizationTest.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,126 @@ | ||
package com.sksamuel.hoplite.json | ||
|
||
import com.sksamuel.hoplite.ConfigLoader | ||
import com.sksamuel.hoplite.defaultParamMappers | ||
import com.sksamuel.hoplite.normalizer.PathNormalizer | ||
import com.sksamuel.hoplite.sources.EnvironmentVariablesPropertySource | ||
import io.kotest.core.spec.style.DescribeSpec | ||
import io.kotest.matchers.shouldBe | ||
|
||
class EnvPropertySourceNormalizationTest : DescribeSpec({ | ||
|
||
data class Creds(val username: String, val password: String) | ||
data class Config(val creds: Creds, val someCamelSetting: String) | ||
|
||
describe("loading from envs") { | ||
it("with path normalizer") { | ||
run { | ||
ConfigLoader { | ||
addNodeNormalizer(PathNormalizer) | ||
addPropertySource( | ||
EnvironmentVariablesPropertySource( | ||
useUnderscoresAsSeparator = true, | ||
useSingleUnderscoresAsSeparator = false, | ||
allowUppercaseNames = false, | ||
environmentVariableMap = { | ||
mapOf( | ||
"CREDS__USERNAME" to "a", | ||
"CREDS__PASSWORD" to "c", | ||
"SOMECAMELSETTING" to "c" | ||
) | ||
} | ||
) | ||
) | ||
}.loadConfigOrThrow<Config>() | ||
} shouldBe Config(Creds("a", "c"), "c") | ||
} | ||
|
||
it("with path normalizer and kebab case underscore separator") { | ||
run { | ||
ConfigLoader { | ||
addNodeNormalizer(PathNormalizer) | ||
addPropertySource( | ||
EnvironmentVariablesPropertySource( | ||
useUnderscoresAsSeparator = true, | ||
useSingleUnderscoresAsSeparator = false, | ||
allowUppercaseNames = false, | ||
environmentVariableMap = { | ||
mapOf( | ||
"CREDS__USERNAME" to "a", | ||
"CREDS__PASSWORD" to "c", | ||
"SOME_CAMEL_SETTING" to "c" | ||
) | ||
} | ||
) | ||
) | ||
}.loadConfigOrThrow<Config>() | ||
} shouldBe Config(Creds("a", "c"), "c") | ||
} | ||
|
||
it("with path normalizer and underscore separator") { | ||
run { | ||
ConfigLoader { | ||
addNodeNormalizer(PathNormalizer) | ||
addPropertySource( | ||
EnvironmentVariablesPropertySource( | ||
useUnderscoresAsSeparator = false, | ||
useSingleUnderscoresAsSeparator = true, | ||
allowUppercaseNames = true, | ||
environmentVariableMap = { | ||
mapOf( | ||
"CREDS_USERNAME" to "a", | ||
"CREDS_PASSWORD" to "b", | ||
"SOMECAMELSETTING" to "c" | ||
) | ||
} | ||
) | ||
) | ||
}.loadConfigOrThrow<Config>() | ||
} shouldBe Config(Creds("a", "b"), "c") | ||
} | ||
|
||
it("with path normalizer and lowercase names") { | ||
run { | ||
ConfigLoader { | ||
addNodeNormalizer(PathNormalizer) | ||
addPropertySource(EnvironmentVariablesPropertySource( | ||
useUnderscoresAsSeparator = false, | ||
useSingleUnderscoresAsSeparator = true, | ||
allowUppercaseNames = false, | ||
environmentVariableMap = { | ||
mapOf( | ||
"creds_username" to "a", | ||
"creds_password" to "d", | ||
"somecamelsetting" to "e" | ||
) | ||
} | ||
)) | ||
}.loadConfigOrThrow<Config>() | ||
} shouldBe Config(Creds("a", "d"), "e") | ||
} | ||
|
||
it("with path normalizer and prefix") { | ||
run { | ||
ConfigLoader { | ||
addNodeNormalizer(PathNormalizer) | ||
addPropertySource( | ||
EnvironmentVariablesPropertySource( | ||
useUnderscoresAsSeparator = false, | ||
useSingleUnderscoresAsSeparator = true, | ||
allowUppercaseNames = false, | ||
environmentVariableMap = { | ||
mapOf( | ||
"WIBBLE_CREDS_USERNAME" to "a", | ||
"WIBBLE_CREDS_PASSWORD" to "c", | ||
"WIBBLE_SOMECAMELSETTING" to "c" | ||
) | ||
}, | ||
prefix = "WIBBLE_" | ||
) | ||
) | ||
}.loadConfigOrThrow<Config>() | ||
} shouldBe Config(Creds("a", "c"), "c") | ||
} | ||
} | ||
|
||
}) |
35 changes: 35 additions & 0 deletions
35
...test/kotlin/com/sksamuel/hoplite/json/EnvPropertySourceSingleUnderscoreAsSeparatorTest.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,35 @@ | ||
package com.sksamuel.hoplite.json | ||
|
||
import com.sksamuel.hoplite.ConfigLoader | ||
import com.sksamuel.hoplite.addIdiomaticEnvironmentSource | ||
import com.sksamuel.hoplite.normalizer.PathNormalizer | ||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.extensions.system.withEnvironment | ||
import io.kotest.matchers.shouldBe | ||
|
||
class EnvPropertySourceSingleUnderscoreAsSeparatorTest : FunSpec({ | ||
|
||
data class Creds(val username: String, val password: String) | ||
data class Config(val creds: Creds, val someCamelSetting: String) | ||
|
||
test("loading from envs") { | ||
withEnvironment(mapOf("creds_username" to "a", "creds_password" to "b", "someCamelSetting" to "c")) { | ||
ConfigLoader | ||
.builder() | ||
.addIdiomaticEnvironmentSource() | ||
.build() | ||
.loadConfigOrThrow<Config>() shouldBe Config(Creds("a", "b"), "c") | ||
} | ||
} | ||
|
||
test("loading from envs with a path normalizer") { | ||
withEnvironment(mapOf("CREDS_USERNAME" to "a", "CREDS_PASSWORD" to "b", "SOMECAMELSETTING" to "c")) { | ||
ConfigLoader | ||
.builder() | ||
.addNodeNormalizer(PathNormalizer) | ||
.addIdiomaticEnvironmentSource() | ||
.build() | ||
.loadConfigOrThrow<Config>() shouldBe Config(Creds("a", "b"), "c") | ||
} | ||
} | ||
}) |
Oops, something went wrong.