@@ -27,6 +27,17 @@ internal abstract class AbstractMapConfigDecoder(
2727 override fun decodeChar (): Char = decodeString().single()
2828 override fun decodeByte (): Byte = decodeString().toByte()
2929 override fun decodeShort (): Short = decodeString().toShort()
30+ override fun decodeEnum (enumDescriptor : SerialDescriptor ): Int =
31+ enumDescriptor.getElementIndex(decodeString())
32+
33+ protected fun beginStructure (descriptor : SerialDescriptor , path : String ): CompositeDecoder {
34+ val kind = descriptor.kind as ? StructureKind ? : error(" Expected structure but found ${descriptor.kind} " )
35+ return when (kind) {
36+ StructureKind .LIST -> ListMapConfigDecoder (map, path, map.listSize(path), serializersModule)
37+ StructureKind .MAP -> SubMapConfigDecoder (map, path, serializersModule)
38+ else -> MapConfigDecoder (map, path, serializersModule)
39+ }
40+ }
3041}
3142
3243@OptIn(ExperimentalSerializationApi ::class )
@@ -59,17 +70,8 @@ internal class MapConfigDecoder(
5970 override fun decodeString (): String = map[currentPath]
6071 ? : throw SerializationException (" Property $currentPath not found" )
6172
62- override fun decodeEnum (enumDescriptor : SerialDescriptor ): Int =
63- enumDescriptor.getElementIndex(decodeString())
64-
65- override fun beginStructure (descriptor : SerialDescriptor ): CompositeDecoder {
66- val kind = descriptor.kind as StructureKind
67- return when (kind) {
68- StructureKind .LIST -> ListMapConfigDecoder (map, currentPath, map.listSize(currentPath), serializersModule)
69- StructureKind .MAP -> SubMapConfigDecoder (map, currentPath, serializersModule)
70- else -> MapConfigDecoder (map, currentPath, serializersModule)
71- }
72- }
73+ override fun beginStructure (descriptor : SerialDescriptor ): CompositeDecoder =
74+ beginStructure(descriptor, currentPath)
7375}
7476
7577@OptIn(ExperimentalSerializationApi ::class )
@@ -90,18 +92,8 @@ private class ListMapConfigDecoder(
9092 return map[key] ? : throw SerializationException (" Missing list element at \" $key \" " )
9193 }
9294
93- override fun decodeEnum (enumDescriptor : SerialDescriptor ): Int =
94- enumDescriptor.getElementIndex(decodeString())
95-
96- override fun beginStructure (descriptor : SerialDescriptor ): CompositeDecoder {
97- val currentKey = " $path .${index - 1 } "
98- val kind = descriptor.kind as StructureKind
99- return when (kind) {
100- StructureKind .LIST -> ListMapConfigDecoder (map, currentKey, map.listSize(currentKey), serializersModule)
101- StructureKind .MAP -> SubMapConfigDecoder (map, currentKey, serializersModule)
102- else -> MapConfigDecoder (map, currentKey, serializersModule)
103- }
104- }
95+ override fun beginStructure (descriptor : SerialDescriptor ): CompositeDecoder =
96+ beginStructure(descriptor, " $path .${index - 1 } " )
10597}
10698
10799@OptIn(ExperimentalSerializationApi ::class )
@@ -141,15 +133,8 @@ private class SubMapConfigDecoder(
141133 }
142134 }
143135
144- override fun beginStructure (descriptor : SerialDescriptor ): CompositeDecoder {
145- val key = " $path .$currentKey "
146- val kind = descriptor.kind as StructureKind
147- return when (kind) {
148- StructureKind .LIST -> ListMapConfigDecoder (map, key, map.listSize(key), serializersModule)
149- StructureKind .MAP -> SubMapConfigDecoder (map, key, serializersModule)
150- else -> MapConfigDecoder (map, key, serializersModule)
151- }
152- }
136+ override fun beginStructure (descriptor : SerialDescriptor ): CompositeDecoder =
137+ beginStructure(descriptor, " $path .$currentKey " )
153138}
154139
155140private fun Map <String , String >.listSize (path : String ): Int =
0 commit comments