@@ -145,7 +145,7 @@ public sealed class Hocon(
145145
146146 }
147147
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>() {
149149 private var ind = - 1
150150
151151 override fun decodeElementIndex (descriptor : SerialDescriptor ): Int {
@@ -161,8 +161,10 @@ public sealed class Hocon(
161161 private fun composeName (parentName : String , childName : String ) =
162162 if (parentName.isEmpty()) childName else " $parentName .$childName "
163163
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+ }
166168
167169 override fun decodeNotNullMark (): Boolean {
168170 // Tag might be null for top-level deserialization
@@ -206,6 +208,27 @@ public sealed class Hocon(
206208 }
207209 }
208210
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+
209232 private inner class ListConfigReader (private val list : ConfigList ) : ConfigConverter<Int>() {
210233 private var ind = - 1
211234
@@ -216,6 +239,7 @@ public sealed class Hocon(
216239
217240 override fun beginStructure (descriptor : SerialDescriptor ): CompositeDecoder =
218241 when {
242+ descriptor.kind is PolymorphicKind -> PolymorphConfigReader ((list[currentTag] as ConfigObject ).toConfig())
219243 descriptor.kind.listLike -> ListConfigReader (list[currentTag] as ConfigList )
220244 descriptor.kind.objLike -> ConfigReader ((list[currentTag] as ConfigObject ).toConfig())
221245 descriptor.kind == StructureKind .MAP -> MapConfigReader (list[currentTag] as ConfigObject )
@@ -256,6 +280,7 @@ public sealed class Hocon(
256280
257281 override fun beginStructure (descriptor : SerialDescriptor ): CompositeDecoder =
258282 when {
283+ descriptor.kind is PolymorphicKind -> PolymorphConfigReader ((values[currentTag / 2 ] as ConfigObject ).toConfig())
259284 descriptor.kind.listLike -> ListConfigReader (values[currentTag / 2 ] as ConfigList )
260285 descriptor.kind.objLike -> ConfigReader ((values[currentTag / 2 ] as ConfigObject ).toConfig())
261286 descriptor.kind == StructureKind .MAP -> MapConfigReader (values[currentTag / 2 ] as ConfigObject )
0 commit comments