-
Notifications
You must be signed in to change notification settings - Fork 73
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 #410.
- Loading branch information
1 parent
90662ef
commit fafb6a4
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
59 changes: 59 additions & 0 deletions
59
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,59 @@ | ||
package com.sksamuel.hoplite.json | ||
|
||
import com.sksamuel.hoplite.ConfigLoader | ||
import com.sksamuel.hoplite.sources.EnvironmentVariablesPropertySource | ||
import com.sksamuel.hoplite.transformer.PathNormalizer | ||
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() | ||
addNodeTransformer(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") | ||
} | ||
} | ||
}) |
125 changes: 125 additions & 0 deletions
125
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,125 @@ | ||
package com.sksamuel.hoplite.json | ||
|
||
import com.sksamuel.hoplite.ConfigLoader | ||
import com.sksamuel.hoplite.sources.EnvironmentVariablesPropertySource | ||
import com.sksamuel.hoplite.transformer.PathNormalizer | ||
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 { | ||
addNodeTransformer(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 { | ||
addNodeTransformer(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 { | ||
addNodeTransformer(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 { | ||
addNodeTransformer(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 { | ||
addNodeTransformer(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.transformer.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() | ||
.addNodeTransformer(PathNormalizer) | ||
.addIdiomaticEnvironmentSource() | ||
.build() | ||
.loadConfigOrThrow<Config>() shouldBe Config(Creds("a", "b"), "c") | ||
} | ||
} | ||
}) |
Oops, something went wrong.