@@ -145,7 +145,7 @@ public sealed class Hocon(
145
145
146
146
}
147
147
148
- private inner class ConfigReader (val conf : Config ) : ConfigConverter<String>() {
148
+ private inner class ConfigReader (val conf : Config , private val isPolymorphic : Boolean = false ) : ConfigConverter<String>() {
149
149
private var ind = - 1
150
150
151
151
override fun decodeElementIndex (descriptor : SerialDescriptor ): Int {
@@ -161,8 +161,10 @@ public sealed class Hocon(
161
161
private fun composeName (parentName : String , childName : String ) =
162
162
if (parentName.isEmpty()) childName else " $parentName .$childName "
163
163
164
- override fun SerialDescriptor.getTag (index : Int ): String =
165
- composeName(currentTagOrNull.orEmpty(), getConventionElementName(index, useConfigNamingConvention))
164
+ override fun SerialDescriptor.getTag (index : Int ): String {
165
+ val conventionName = getConventionElementName(index, useConfigNamingConvention)
166
+ return if (! isPolymorphic) composeName(currentTagOrNull.orEmpty(), conventionName) else conventionName
167
+ }
166
168
167
169
override fun decodeNotNullMark (): Boolean {
168
170
// Tag might be null for top-level deserialization
@@ -206,6 +208,27 @@ public sealed class Hocon(
206
208
}
207
209
}
208
210
211
+ private inner class PolymorphConfigReader (private val conf : Config ) : ConfigConverter<String>() {
212
+ private var ind = - 1
213
+
214
+ override fun beginStructure (descriptor : SerialDescriptor ): CompositeDecoder =
215
+ when {
216
+ descriptor.kind.objLike -> ConfigReader (conf, isPolymorphic = true )
217
+ else -> this
218
+ }
219
+
220
+ override fun SerialDescriptor.getTag (index : Int ): String = getElementName(index)
221
+
222
+ override fun decodeElementIndex (descriptor : SerialDescriptor ): Int {
223
+ ind++
224
+ return if (ind >= descriptor.elementsCount) DECODE_DONE else ind
225
+ }
226
+
227
+ override fun <E > getValueFromTaggedConfig (tag : String , valueResolver : (Config , String ) -> E ): E {
228
+ return valueResolver(conf, tag)
229
+ }
230
+ }
231
+
209
232
private inner class ListConfigReader (private val list : ConfigList ) : ConfigConverter<Int>() {
210
233
private var ind = - 1
211
234
@@ -216,6 +239,7 @@ public sealed class Hocon(
216
239
217
240
override fun beginStructure (descriptor : SerialDescriptor ): CompositeDecoder =
218
241
when {
242
+ descriptor.kind is PolymorphicKind -> PolymorphConfigReader ((list[currentTag] as ConfigObject ).toConfig())
219
243
descriptor.kind.listLike -> ListConfigReader (list[currentTag] as ConfigList )
220
244
descriptor.kind.objLike -> ConfigReader ((list[currentTag] as ConfigObject ).toConfig())
221
245
descriptor.kind == StructureKind .MAP -> MapConfigReader (list[currentTag] as ConfigObject )
@@ -256,6 +280,7 @@ public sealed class Hocon(
256
280
257
281
override fun beginStructure (descriptor : SerialDescriptor ): CompositeDecoder =
258
282
when {
283
+ descriptor.kind is PolymorphicKind -> PolymorphConfigReader ((values[currentTag / 2 ] as ConfigObject ).toConfig())
259
284
descriptor.kind.listLike -> ListConfigReader (values[currentTag / 2 ] as ConfigList )
260
285
descriptor.kind.objLike -> ConfigReader ((values[currentTag / 2 ] as ConfigObject ).toConfig())
261
286
descriptor.kind == StructureKind .MAP -> MapConfigReader (values[currentTag / 2 ] as ConfigObject )
0 commit comments