Repository: ktorio/ktor
File: ktor-server/ktor-server-core/common/src/io/ktor/server/config/MapApplicationConfig.kt (Kotlin)

Code before:
        when {
            map.containsKey(path) -> ApplicationConfigValue.Type.SINGLE
            map[combine(path, "size")] != null -> ApplicationConfigValue.Type.LIST
            map.containsPrefix(path) -> ApplicationConfigValue.Type.OBJECT
            else -> ApplicationConfigValue.Type.NULL
        }
    }
    override fun getString(): String = map[path]!!
    override fun getList(): List<String> {
        val size =
            map[combine(path, "size")] ?: throw ApplicationConfigurationException("Property $path.size not found.")
        return (0 until size.toInt()).map { map[combine(path, it.toString())]!! }
    }

    override fun getMap(): Map<String, Any?> {
        return MapApplicationConfig(map, path).toMap()
    }

    @OptIn(InternalAPI::class)
    override fun getAs(type: TypeInfo): Any? {
        return type.serializer()
            .deserialize(MapConfigDecoder(map, path))
    }
}

private fun combine(root: String, relative: Any): String =
    if (root.isEmpty()) relative.toString() else "$root.$relative"

private fun findListElements(input: String, listElements: MutableMap<String, Int>) {
    var pointBegin = input.indexOf('.')
    while (pointBegin != input.length) {
        val pointEnd = input.indexOf('.', pointBegin + 1).let { if (it == -1) input.length else it }

        input.substring(pointBegin + 1, pointEnd).toIntOrNull()?.let { pos ->
            val element = input.take(pointBegin)
            val newSize = pos + 1
            listElements[element] = listElements[element]?.let { maxOf(it, newSize) } ?: newSize
        }
        pointBegin = pointEnd
    }
}


Review comment (suggestion):
Investigating why changing the signature from `combine(String, String)` to `combine(String, Any)` caused ProGuard warning...

Code after:
            map.containsKey(path) -> ApplicationConfigValue.Type.SINGLE
            map.containsKey(combine(path, "size")) -> ApplicationConfigValue.Type.LIST
            map.containsPrefix(path) -> ApplicationConfigValue.Type.OBJECT
            else -> ApplicationConfigValue.Type.NULL
        }
    }
    override fun getString(): String = map[path]!!
    override fun getList(): List<String> {
        val size =
            map[combine(path, "size")] ?: throw ApplicationConfigurationException("Property $path.size not found.")
        return (0 until size.toInt()).map { map[combine(path, it)]!! }
    }

    override fun getMap(): Map<String, Any?> {
        return MapApplicationConfig(map, path).toMap()
    }

    @OptIn(InternalAPI::class)
    override fun getAs(type: TypeInfo): Any? {
        return type.serializer()
            .deserialize(MapConfigDecoder(map, path))
    }
}

private fun combine(root: String, relative: Int): String = combine(root, relative.toString())
private fun combine(root: String, relative: String): String = if (root.isEmpty()) relative else "$root.$relative"

private fun findListElements(input: String, listElements: MutableMap<String, Int>) {
    var pointBegin = input.indexOf('.')
    while (pointBegin != input.length) {
        val pointEnd = input.indexOf('.', pointBegin + 1).let { if (it == -1) input.length else it }

        input.substring(pointBegin + 1, pointEnd).toIntOrNull()?.let { pos ->
            val element = input.take(pointBegin)
            val newSize = pos + 1
            listElements[element] = listElements[element]?.let { maxOf(it, newSize) } ?: newSize
        }
        pointBegin = pointEnd
    }
}