Skip to content

Commit

Permalink
Subset configuration can be specified by prefix and/or suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
npryce committed May 2, 2018
1 parent e631f01 commit a60582c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 14 deletions.
25 changes: 17 additions & 8 deletions src/main/kotlin/com/natpryce/konfig/konfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -293,18 +293,27 @@ fun search(first: Configuration, vararg rest: Configuration) = rest.fold(first,
* delegated to [configuration] as a look up for "db.password".
*/
class Subset(
namePrefix: String,
private val configuration: Configuration
private val configuration: Configuration,
namePrefix: String? = null,
nameSuffix: String? = null
) : Configuration {
private val prefix = namePrefix + "."
// For backward compatibility with previous versions
constructor(namePrefix: String, configuration: Configuration):
this(configuration, namePrefix = namePrefix)

override fun <T> getOrNull(key: Key<T>) = configuration.getOrNull(prefixed(key))
private val prefix = namePrefix?.let { "$it." } ?: ""
private val suffix = nameSuffix?.let { ".$it" } ?: ""

override fun contains(key: Key<*>) = configuration.contains(prefixed(key))
override fun <T> getOrNull(key: Key<T>) = configuration.getOrNull(full(key))

override fun searchPath(key: Key<*>) = configuration.searchPath(prefixed(key))
override fun contains(key: Key<*>) = configuration.contains(full(key))

override fun list() = configuration.list().map { it.first to it.second.filterKeys { k -> k.startsWith(prefix) } }
override fun searchPath(key: Key<*>) = configuration.searchPath(full(key))

private fun <T> prefixed(key: Key<T>) = key.copy(name = prefix + key.name)
override fun list() =
configuration.list().map { it.first to it.second.filterKeys { k ->
(prefix.isEmpty() || k.startsWith(prefix)) && (suffix.isEmpty() || k.endsWith(suffix)) }
}

private fun <T> full(key: Key<T>) = key.copy(name = prefix + key.name + suffix)
}
49 changes: 43 additions & 6 deletions src/test/kotlin/com/natpryce/konfig/konfig_tests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -251,27 +251,57 @@ class ConfigSubset {
"a.two" to "a2",
"b.one" to "b1",
"b.two" to "b2",
"b.three" to "b3")
"b.three" to "b3",
"a.x.b" to "axb")

val subsetA = Subset("a", fullSet)
val subsetB = Subset("b", fullSet)

val key1 = Key("one", stringType)
val key2 = Key("two", stringType)
val key3 = Key("three", stringType)

val keyA = Key("a", stringType)
val keyB = Key("b", stringType)

val keyX = Key("x", stringType)


@Test
fun subset_properties() {
fun prefixed_properties() {
val subsetA = Subset(namePrefix = "a", configuration = fullSet)
val subsetB = Subset(namePrefix = "b", configuration = fullSet)

assertThat(subsetA[key1], equalTo("a1"))
assertThat(subsetA[key2], equalTo("a2"))

assertThat(subsetB[key1], equalTo("b1"))
assertThat(subsetB[key2], equalTo("b2"))
}

@Test
fun suffixed_properties() {
val subset1 = Subset(nameSuffix = "one", configuration = fullSet)
val subset2 = Subset(nameSuffix = "two", configuration = fullSet)

assertThat(subset1[keyA], equalTo("a1"))
assertThat(subset2[keyA], equalTo("a2"))

assertThat(subset1[keyB], equalTo("b1"))
assertThat(subset2[keyB], equalTo("b2"))
}

@Test
fun suffixed_and_prefixed_properties() {
val subset1 = Subset(namePrefix = "a", nameSuffix = "b", configuration = fullSet)

assertThat(subset1[keyX], equalTo("axb"))
}


@Test
fun contains() {
val subsetA = Subset("a", fullSet)
val subsetB = Subset("b", fullSet)

assertTrue(fullSet.contains(Key("a.one", stringType)))
assertTrue(fullSet.contains(Key("b.one", stringType)))

Expand All @@ -283,8 +313,15 @@ class ConfigSubset {
}

@Test
fun lists_only_those_variables_that_start_with_the_prefix() {
assertThat(subsetA.list(), equalTo(listOf(fullSet.location to mapOf("a.one" to "a1", "a.two" to "a2"))))
fun lists_only_properties_in_the_subset() {
assertThat("prefixed", Subset(fullSet, namePrefix = "a").list(), equalTo(listOf(
fullSet.location to mapOf("a.one" to "a1", "a.two" to "a2", "a.x.b" to "axb"))))

assertThat("suffixed", Subset(fullSet, nameSuffix= "two").list(), equalTo(listOf(
fullSet.location to mapOf("a.two" to "a2", "b.two" to "b2"))))

assertThat("pre/suff-ixed", Subset(fullSet, namePrefix = "a", nameSuffix= "b").list(), equalTo(listOf(
fullSet.location to mapOf("a.x.b" to "axb"))))
}
}

Expand Down

0 comments on commit a60582c

Please sign in to comment.