All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.nlopez.compose.rules.DetektComposeKtConfig.kt Maven / Gradle / Ivy

There is a newer version: 0.4.19
Show newest version
// Copyright 2023 Nacho Lopez
// SPDX-License-Identifier: Apache-2.0
package io.nlopez.compose.rules

import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.api.internal.valueOrDefaultCommaSeparated
import io.nlopez.compose.core.ComposeKtConfig

/**
 * Manages the configuration for detekt rules. Results will be memoized, as config shouldn't be changing
 * during the lifetime of a rule.
 */
internal class DetektComposeKtConfig(private val config: Config) : ComposeKtConfig {
    private val cache = mutableMapOf()

    @Suppress("UNCHECKED_CAST")
    private fun  valueOrPut(key: String, value: () -> T?): T? = cache.getOrPut(key) { value() } as? T

    override fun getInt(key: String, default: Int): Int =
        valueOrPut(key) { config.valueOrDefault(key, default) } ?: default

    override fun getString(key: String, default: String?): String? = valueOrPut(key) {
        if (default == null) {
            config.valueOrNull(key)
        } else {
            config.valueOrDefault(key, default)
        }
    }

    override fun getList(key: String, default: List): List =
        valueOrPut(key) { config.valueOrDefaultCommaSeparated(key, default) } ?: default

    override fun getSet(key: String, default: Set): Set =
        valueOrPut(key) { getList(key, default.toList()).toSet() } ?: default

    override fun getBoolean(key: String, default: Boolean): Boolean =
        valueOrPut(key) { config.valueOrDefault(key, default) } ?: default
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy