Skip to content

Commit

Permalink
feature: support flatten env source
Browse files Browse the repository at this point in the history
  • Loading branch information
uchuhimo committed Dec 22, 2019
1 parent dcee99b commit cc4b493
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,11 @@ class DefaultLoaders(
/**
* Returns a child config containing values from system environment.
*
* @param nested whether to treat "AA_BB_CC" as nested format "AA.BB.CC" or not. True by default.
* @return a child config containing values from system environment
*/
fun env(): Config = config.withSource(EnvProvider.env().orMapped())
@JvmOverloads
fun env(nested: Boolean = true): Config = config.withSource(EnvProvider.env(nested).orMapped())

/**
* Returns a child config containing values from system properties.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ object DefaultProviders {
/**
* Returns a source from system environment.
*
* @param nested whether to treat "AA_BB_CC" as nested format "AA.BB.CC" or not. True by default.
* @return a source from system environment
*/
fun env(): Source = EnvProvider.env()
@JvmOverloads
fun env(nested: Boolean = true): Source = EnvProvider.env(nested)

/**
* Returns a source from system properties.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ object EnvProvider {
/**
* Returns a new source from system environment.
*
* @param nested whether to treat "AA_BB_CC" as nested format "AA.BB.CC" or not. True by default.
* @return a new source from system environment
*/
@JvmOverloads
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import java.util.concurrent.TimeUnit
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.given
import org.jetbrains.spek.api.dsl.it
import org.jetbrains.spek.api.dsl.on
Expand Down Expand Up @@ -271,6 +272,19 @@ object DefaultLoadersSpec : SubjectSpek<DefaultLoaders>({
}
})

object DefaultLoadersWithFlattenEnvSpec : Spek({
given("a loader") {
on("load as flatten format from system environment") {
val config = Config {
addSpec(FlattenDefaultLoadersConfig)
}.from.env(nested = false)
it("should return a config which contains value from system environment") {
assertThat(config[FlattenDefaultLoadersConfig.SOURCE_TEST_TYPE], equalTo("env"))
}
}
}
})

object MappedDefaultLoadersSpec : SubjectSpek<DefaultLoaders>({
subject {
Config {
Expand Down Expand Up @@ -305,4 +319,8 @@ object DefaultLoadersConfig : ConfigSpec("source.test") {
val type by required<String>()
}

object FlattenDefaultLoadersConfig : ConfigSpec("") {
val SOURCE_TEST_TYPE by required<String>()
}

const val propertiesContent = "source.test.type = properties"
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ object DefaultProvidersSpec : SubjectSpek<DefaultProviders>({
assertThat(config[item], equalTo("env"))
}
}
on("provide flatten source from system environment") {
val config = subject.env(nested = false).toFlattenConfig()
it("should return a source which contains value from system environment") {
assertThat(config[FlattenDefaultLoadersConfig.SOURCE_TEST_TYPE], equalTo("env"))
}
}
on("provide source from system properties") {
System.setProperty(DefaultLoadersConfig.qualify(DefaultLoadersConfig.type), "system")
val config = subject.systemProperties().toConfig()
Expand Down Expand Up @@ -102,3 +108,7 @@ object DefaultProvidersSpec : SubjectSpek<DefaultProviders>({
fun Source.toConfig(): Config = Config {
addSpec(DefaultLoadersConfig)
}.withSource(this)

fun Source.toFlattenConfig(): Config = Config {
addSpec(FlattenDefaultLoadersConfig)
}.withSource(this)

0 comments on commit cc4b493

Please sign in to comment.