Skip to content

Commit 798ad39

Browse files
authored
Support list of enums deserialization (#345)
* Support list of enums deserialization
1 parent dbc5278 commit 798ad39

File tree

3 files changed

+52
-38
lines changed

3 files changed

+52
-38
lines changed

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

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.akuleshov7.ktoml.decoders
22

33
import com.akuleshov7.ktoml.Toml.Default.serializersModule
44
import com.akuleshov7.ktoml.TomlInputConfig
5+
import com.akuleshov7.ktoml.tree.nodes.TomlFile
56
import com.akuleshov7.ktoml.tree.nodes.TomlKeyValue
67
import com.akuleshov7.ktoml.tree.nodes.TomlKeyValueArray
78
import com.akuleshov7.ktoml.tree.nodes.TomlKeyValuePrimitive
@@ -42,32 +43,45 @@ public class TomlArrayDecoder(
4243

4344
currentPrimitiveElementOfArray = list[nextElementIndex]
4445

45-
currentElementDecoder = if (currentPrimitiveElementOfArray is TomlArray) {
46-
TomlArrayDecoder(
47-
TomlKeyValueArray(
48-
rootNode.key,
49-
currentPrimitiveElementOfArray,
50-
rootNode.lineNo,
51-
comments = emptyList(),
52-
inlineComment = "",
53-
),
54-
config,
55-
serializersModule,
56-
)
46+
if (currentPrimitiveElementOfArray is TomlArray) {
47+
setArrayDecoder()
5748
} else {
58-
TomlPrimitiveDecoder(
59-
// a small hack that creates a PrimitiveKeyValue node that is used in the decoder
49+
setPrimitiveDecoder()
50+
}
51+
return nextElementIndex++
52+
}
53+
54+
private fun setArrayDecoder() {
55+
currentElementDecoder = TomlArrayDecoder(
56+
TomlKeyValueArray(
57+
rootNode.key,
58+
currentPrimitiveElementOfArray,
59+
rootNode.lineNo,
60+
comments = emptyList(),
61+
inlineComment = "",
62+
),
63+
config,
64+
serializersModule,
65+
)
66+
}
67+
68+
private fun setPrimitiveDecoder() {
69+
val primitiveRoot = TomlFile().also { root ->
70+
root.appendChild(
6071
TomlKeyValuePrimitive(
6172
rootNode.key,
6273
currentPrimitiveElementOfArray,
6374
rootNode.lineNo,
6475
comments = emptyList(),
6576
inlineComment = "",
66-
),
67-
serializersModule,
77+
)
6878
)
6979
}
70-
return nextElementIndex++
80+
currentElementDecoder = TomlMainDecoder(
81+
rootNode = primitiveRoot,
82+
config = config,
83+
serializersModule = serializersModule,
84+
)
7185
}
7286

7387
override fun beginStructure(descriptor: SerialDescriptor): CompositeDecoder {

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

Lines changed: 0 additions & 21 deletions
This file was deleted.

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,25 @@ class SimpleArrayDecoderTest {
200200
Toml.decodeFromString<ArrayWrapper>(toml)
201201
}
202202
}
203+
204+
enum class Enum {
205+
A,
206+
B,
207+
C
208+
}
209+
210+
@Serializable
211+
data class Enums(
212+
val enums: List<Enum>
213+
)
214+
215+
@Test
216+
fun decodeEnumList() {
217+
val test = """
218+
enums = ["A", "B", "C"]
219+
""".trimIndent()
220+
221+
val decoded = Toml.decodeFromString<Enums>(test)
222+
assertEquals(Enums(listOf(Enum.A, Enum.B, Enum.C)), decoded)
223+
}
203224
}

0 commit comments

Comments
 (0)