Skip to content

Commit

Permalink
Added extra test for cross file substitions
Browse files Browse the repository at this point in the history
  • Loading branch information
sksamuel committed Jan 30, 2021
1 parent 5663ff7 commit 119ab1f
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,13 @@ class ConfigLoader constructor(
}
}

/**
* Loads all property sources and combines them into a single node.
*/
private fun loadNode(configs: List<ConfigSource>): ConfigResult<Node> {
val srcs = propertySources + configs.map { ConfigFilePropertySource(it, parserRegistry) }
return srcs.map { it.node() }.sequence()
.map { it.reduce { acc, b -> acc.fallback(b) } }
.map { it.reduce { acc, b -> acc.merge(b) } }
.mapInvalid {
val multipleFailures = ConfigFailure.MultipleFailures(it)
multipleFailures
Expand Down
16 changes: 8 additions & 8 deletions hoplite-core/src/main/kotlin/com/sksamuel/hoplite/fallback.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ package com.sksamuel.hoplite

/**
* Returns a new [Node] which is the result of merging this node, with keys from the given node,
* such as keys in this node take precedence.
* with keys in this node taking precedence.
*/
fun Node.fallback(other: Node): Node {
return when (val self = this) {
internal fun Node.merge(other: Node): Node {
return when (this) {
is Undefined -> other
is MapNode -> when (other) {
is MapNode -> {
val keys = self.map.keys + other.map.keys
val map = keys.associateWith { self.atKey(it).fallback(other.atKey(it)) }
MapNode(map, self.pos, self.value.fallback(other.value))
val keys = this.map.keys + other.map.keys
val map = keys.associateWith { this.atKey(it).merge(other.atKey(it)) }
MapNode(map, this.pos, this.value.merge(other.value))
}
else -> self
else -> this
}
else -> self
else -> this
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ class FallbackTest : FunSpec({
Pos.NoPos
)

val f = node1.fallback(node2)
val f = node1.merge(node2)
f["a"] shouldBe StringNode("foo", Pos.NoPos)
f["b"] shouldBe StringNode("bar", Pos.NoPos)
f["c"] shouldBe StringNode("baz", Pos.NoPos)

val g = node2.fallback(node1)
val g = node2.merge(node1)
g["a"] shouldBe StringNode("faz", Pos.NoPos)
g["b"] shouldBe StringNode("bar", Pos.NoPos)
g["c"] shouldBe StringNode("baz", Pos.NoPos)
Expand Down Expand Up @@ -63,13 +63,13 @@ class FallbackTest : FunSpec({
Pos.NoPos
)

val f = node1.fallback(node2)
val f = node1.merge(node2)
f["a"] shouldBe StringNode("foo", Pos.NoPos)
f["b"]["j"] shouldBe StringNode("jen", Pos.NoPos)
f["b"]["k"] shouldBe StringNode("ken", Pos.NoPos)
f["c"] shouldBe StringNode("baz", Pos.NoPos)

val g = node2.fallback(node1)
val g = node2.merge(node1)
g["a"] shouldBe StringNode("foo", Pos.NoPos)
g["b"]["j"] shouldBe StringNode("jen", Pos.NoPos)
g["b"]["k"] shouldBe StringNode("kez", Pos.NoPos)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ import io.kotest.matchers.shouldBe

class LookupPreprocessorTest : FunSpec() {

data class Test(val a: String,
val b: String,
val c: String,
val d: String,
val e: String,
val f: String
data class Test(
val a: String,
val b: String,
val c: String,
val d: String,
val e: String,
val f: String
)

data class Test2(
val env: String,
val hostname: String
)

init {
Expand Down Expand Up @@ -39,5 +45,13 @@ class LookupPreprocessorTest : FunSpec() {
f = "f{{unknown}}"
)
}

test("lookup preprocessor across multiple files") {
ConfigLoader().loadConfigOrThrow<Test2>("/test_lookup1.yml", "/test_lookup2.yml") shouldBe
Test2(
env = "PROD",
hostname = "wibble.PROD"
)
}
}
}
1 change: 1 addition & 0 deletions hoplite-yaml/src/test/resources/test_lookup1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
env: PROD
1 change: 1 addition & 0 deletions hoplite-yaml/src/test/resources/test_lookup2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hostname: wibble.{{env}}

0 comments on commit 119ab1f

Please sign in to comment.