Skip to content

Commit 69386b8

Browse files
Support decoding map with array (#353)
Co-authored-by: White Rabbit <[email protected]>
1 parent 8e9947d commit 69386b8

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/decoders/TomlMapDecoder.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ public class TomlMapDecoder private constructor(
9696
// stubs are internal technical nodes that are not needed in this scenario
9797
skipStubs()
9898
return when (val processedNode = rootNode.children[decodingElementIndex]) {
99+
is TomlKeyValueArray -> if (index % 2 == 0) {
100+
processedNode.key.toString() as T
101+
} else {
102+
decodeTomlKeyValueArray(processedNode, deserializer)
103+
}
99104
is TomlKeyValue -> if (index % 2 == 0) {
100105
processedNode.key.toString() as T
101106
} else {
@@ -148,6 +153,11 @@ public class TomlMapDecoder private constructor(
148153
TomlMainDecoder.decode(deserializer, rootNode, config)
149154
}
150155

156+
private fun <T> decodeTomlKeyValueArray(
157+
processedNode: TomlKeyValueArray,
158+
deserializer: DeserializationStrategy<T>
159+
): T = TomlArrayDecoder.decode(deserializer, processedNode, config)
160+
151161
private fun <T> decodeTomlTable(
152162
processedNode: TomlTable,
153163
deserializer: DeserializationStrategy<T>

ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/decoders/structures/MapDecoderTest.kt

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.akuleshov7.ktoml.decoders.structures
22

33
import com.akuleshov7.ktoml.Toml
4+
import com.akuleshov7.ktoml.TomlInputConfig
45
import com.akuleshov7.ktoml.exceptions.IllegalTypeException
56
import com.akuleshov7.ktoml.exceptions.MissingRequiredPropertyException
67
import com.akuleshov7.ktoml.exceptions.TomlDecodingException
8+
import com.akuleshov7.ktoml.parsers.TomlParser
79
import kotlinx.serialization.SerialName
810
import kotlinx.serialization.Serializable
911
import kotlinx.serialization.decodeFromString
@@ -149,6 +151,64 @@ class MapDecoderTest {
149151
)
150152
}
151153

154+
@Test
155+
fun testMapWithArray() {
156+
@Serializable
157+
data class VersionCatalog(
158+
val bundles: Map<String, List<String>> = emptyMap(),
159+
)
160+
161+
val toml = """
162+
[bundles]
163+
koin = [
164+
"koin-core",
165+
"koin-compose",
166+
"koin-compose-viewmodel"
167+
]
168+
169+
ktor-common = [
170+
"ktor-client-core",
171+
"ktor-client-content-negotiation",
172+
"ktor-serialization-kotlinx-json",
173+
"ktor-client-auth",
174+
"ktor-client-logging"
175+
]
176+
177+
ktor-commond = [
178+
"ktor-client-core",
179+
"ktor-client-content-negotiation",
180+
"ktor-serialization-kotlinx-json",
181+
"ktor-client-auth",
182+
"ktor-client-loggqing"
183+
]
184+
""".trimIndent()
185+
186+
val expected = VersionCatalog(
187+
mapOf(
188+
"koin" to listOf("koin-core", "koin-compose", "koin-compose-viewmodel"),
189+
"ktor-common" to listOf(
190+
"ktor-client-core",
191+
"ktor-client-content-negotiation",
192+
"ktor-serialization-kotlinx-json",
193+
"ktor-client-auth",
194+
"ktor-client-logging"
195+
),
196+
"ktor-commond" to listOf(
197+
"ktor-client-core",
198+
"ktor-client-content-negotiation",
199+
"ktor-serialization-kotlinx-json",
200+
"ktor-client-auth",
201+
"ktor-client-loggqing"
202+
)
203+
)
204+
)
205+
val result = Toml.decodeFromString<VersionCatalog>(toml)
206+
assertEquals(
207+
expected,
208+
result
209+
)
210+
}
211+
152212
@Test
153213
fun decodeMapOfObjects() {
154214
@Serializable

0 commit comments

Comments
 (0)