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
93a0227
commit c0b8fed
Showing
11 changed files
with
332 additions
and
11 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
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
65 changes: 65 additions & 0 deletions
65
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,65 @@ | ||
package com.sksamuel.hoplite.json | ||
|
||
import com.sksamuel.hoplite.ConfigLoader | ||
import com.sksamuel.hoplite.ConfigLoaderBuilder | ||
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() | ||
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 { | ||
ConfigLoaderBuilder.empty() | ||
.addDefaultDecoders() | ||
.addDefaultResolvers() | ||
.addDefaultParamMappers() | ||
.addDefaultPropertySources() | ||
.addDefaultParsers() | ||
.withReport() | ||
.addPropertySource( | ||
EnvironmentVariablesPropertySource( | ||
useUnderscoresAsSeparator = true, | ||
useSingleUnderscoresAsSeparator = false, | ||
allowUppercaseNames = false, | ||
environmentVariableMap = { | ||
mapOf( | ||
"someCamelSetting" to "a", | ||
"somecamelsetting" to "b", | ||
"SOMECAMELSETTING" to "c", | ||
) | ||
} | ||
) | ||
) | ||
.build() | ||
.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") | ||
} | ||
} | ||
|
||
}) |
Oops, something went wrong.