Skip to content

Commit

Permalink
Cascader should carry through the source key from the override (#448)
Browse files Browse the repository at this point in the history
Co-authored-by: Oguzhan Soykan <oguzhansoykan@gmail.com>
  • Loading branch information
rocketraman and osoykan authored Sep 24, 2024
1 parent 40182da commit 265d516
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class Cascader(
val overrides = merges.values.toList().flatMap { it.overrides }
val elements = merges.mapValues { it.value.node }
CascadeResult(
MapNode(elements, a.pos, a.path, cascade(a.value, b.value).node),
MapNode(elements, a.pos, a.path, cascade(a.value, b.value).node, a.meta, a.sourceKey),
overrides
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package com.sksamuel.hoplite.yaml

import com.sksamuel.hoplite.ConfigLoaderBuilder
import com.sksamuel.hoplite.addCommandLineSource
import com.sksamuel.hoplite.addEnvironmentSource
import com.sksamuel.hoplite.addResourceOrFileSource
import io.kotest.core.spec.style.FunSpec
import io.kotest.extensions.system.withEnvironment
import io.kotest.matchers.shouldBe

class DenormalizedMapKeysTest : FunSpec({
Expand Down Expand Up @@ -49,4 +51,117 @@ class DenormalizedMapKeysTest : FunSpec({
)
)
}

test("should set denormalized map keys for CLI arguments, overriding a property source") {
val config = ConfigLoaderBuilder.default()
.addCommandLineSource(
arrayOf(
"--m.DC1.x-val=15",
"--m.DC2.x-val=25",
),
prefix = "--",
delimiter = "="
)
.addResourceOrFileSource("/test_data_class_in_map.yaml")
.build()
.loadConfigOrThrow<MapContainer>()

config shouldBe MapContainer(
m = mapOf(
"DC1" to Foo("15"),
"DC2" to Foo("25"),
)
)
}

test("should set denormalized map keys from environment variables") {
withEnvironment(
mapOf(
"m.DC1.x-val" to "15",
"m.DC2.x-val" to "25"
)
) {
val config = ConfigLoaderBuilder.default()
.addEnvironmentSource()
.build()
.loadConfigOrThrow<MapContainer>()

config shouldBe MapContainer(
m = mapOf(
"DC1" to Foo("15"),
"DC2" to Foo("25"),
)
)
}
}

test("should set denormalized map keys from environment variables, overriding a property source") {
withEnvironment(
mapOf(
"m.DC1.x-val" to "15",
"m.DC2.x-val" to "25"
)
) {
val config = ConfigLoaderBuilder.default()
.addEnvironmentSource()
.addResourceOrFileSource("/test_data_class_in_map.yaml")
.build()
.loadConfigOrThrow<MapContainer>()

config shouldBe MapContainer(
m = mapOf(
"DC1" to Foo("15"),
"DC2" to Foo("25"),
)
)
}
}

test("should set denormalized map keys from command line arguments, overriding environment variables and property sources") {
withEnvironment(
mapOf(
"m.DC1.x-val" to "15",
"m.DC2.x-val" to "25"
)
) {
val config = ConfigLoaderBuilder.default()
.addCommandLineSource(
arrayOf(
"--m.DC1.x-val=20",
"--m.DC2.x-val=30",
),
)
.addEnvironmentSource()
.addResourceOrFileSource("/test_data_class_in_map.yaml")
.build()
.loadConfigOrThrow<MapContainer>()

config shouldBe MapContainer(
m = mapOf(
"DC1" to Foo("20"),
"DC2" to Foo("30"),
)
)
}
}

test("should set denormalized map keys from command line arguments with the CLI case") {
val config = ConfigLoaderBuilder.default()
.addCommandLineSource(
arrayOf(
"--m.Dc1.x-val=20",
"--m.Dc2.x-val=30",
),
)
.addResourceOrFileSource("/test_data_class_in_map.yaml")
.build()
.loadConfigOrThrow<MapContainer>()

config shouldBe MapContainer(
m = mapOf(
"Dc1" to Foo("20"),
"Dc2" to Foo("30"),
)
)
}
})

0 comments on commit 265d516

Please sign in to comment.