Skip to content

Commit

Permalink
Fix for sksamuel#456
Browse files Browse the repository at this point in the history
  • Loading branch information
mdii committed Dec 30, 2024
1 parent 1c698e5 commit aeba64c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package com.sksamuel.hoplite.decoder

import com.sksamuel.hoplite.*
import com.sksamuel.hoplite.fp.invalid
import com.sksamuel.hoplite.ConfigFailure
import com.sksamuel.hoplite.ConfigResult
import com.sksamuel.hoplite.DecoderContext
import com.sksamuel.hoplite.MapNode
import com.sksamuel.hoplite.Node
import com.sksamuel.hoplite.StringNode
import com.sksamuel.hoplite.fp.flatMap
import com.sksamuel.hoplite.fp.sequence
import kotlin.reflect.KType
Expand All @@ -33,7 +28,7 @@ class LinkedHashMapDecoder : NullHandlingDecoder<LinkedHashMap<*, *>> {
vdecoder: Decoder<V>,
context: DecoderContext): ConfigResult<LinkedHashMap<K, V>> {

return node.map.entries.map { (k, v) ->
return node.denormalize().map.entries.map { (k, v) ->
kdecoder.decode(StringNode(k, node.pos, node.path, emptyMap()), kType, context).flatMap { kk ->
vdecoder.decode(v, vType, context).map { vv ->
kk to vv
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.sksamuel.hoplite.yaml

import com.sksamuel.hoplite.ConfigLoaderBuilder
import com.sksamuel.hoplite.addResourceOrFileSource
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe

class DenormalizedLinkedHashMapKeysTest : FunSpec({
data class Foo(
val xVal: String = "x"
)

data class LinkedHashMapContainer(
val m: LinkedHashMap<String, Foo> = linkedMapOf()
)

test("should set denormalized map keys and decode a data class inside a linked has map map") {
val config = ConfigLoaderBuilder.default()
.addResourceOrFileSource("/test_data_class_in_map.yaml")
.build()
.loadConfigOrThrow<LinkedHashMapContainer>()

config shouldBe LinkedHashMapContainer(
m = linkedMapOf(
"DC1" to Foo("10"),
"DC2" to Foo("20"),
)
)
}
})

0 comments on commit aeba64c

Please sign in to comment.