Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use same behaviour for valueOrNull as for valueOrDefault #2319

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -9,17 +9,14 @@ import io.gitlab.arturbosch.detekt.api.internal.validateConfig
class CompositeConfig(private val lookFirst: Config, private val lookSecond: Config) :
Config, ValidatableConfiguration {

override fun subConfig(key: String): Config {
return CompositeConfig(lookFirst.subConfig(key), lookSecond.subConfig(key))
}
override fun subConfig(key: String): Config =
CompositeConfig(lookFirst.subConfig(key), lookSecond.subConfig(key))

override fun <T : Any> valueOrDefault(key: String, default: T): T {
return lookFirst.valueOrNull(key) ?: lookSecond.valueOrDefault(key, default)
}
override fun <T : Any> valueOrDefault(key: String, default: T): T =
lookFirst.valueOrNull(key) ?: lookSecond.valueOrDefault(key, default)

override fun <T : Any> valueOrNull(key: String): T? {
return lookFirst.valueOrNull(key) ?: lookSecond.valueOrNull(key)
}
override fun <T : Any> valueOrNull(key: String): T? =
lookFirst.valueOrNull(key) ?: lookSecond.valueOrNull(key)

override fun toString(): String = "CompositeConfig(lookFirst=$lookFirst, lookSecond=$lookSecond)"

Expand Down
Expand Up @@ -18,7 +18,7 @@ interface Config {
* Retrieves a sub configuration or value based on given key. If configuration property cannot be found
* the specified default value is returned.
*/
fun <T : Any> valueOrDefault(key: String, default: T): T
fun <T : Any> valueOrDefault(key: String, default: T): T = valueOrNull(key) ?: default

/**
* Retrieves a sub configuration or value based on given key.
Expand Down Expand Up @@ -86,9 +86,10 @@ internal object EmptyConfig : HierarchicalConfig {
override fun subConfig(key: String): EmptyConfig = this

@Suppress("UNCHECKED_CAST")
override fun <T : Any> valueOrDefault(key: String, default: T): T = if (key == "active") true as T else default

override fun <T : Any> valueOrNull(key: String): T? = null
override fun <T : Any> valueOrNull(key: String): T? = when (key) {
"active" -> true as? T
arturbosch marked this conversation as resolved.
Show resolved Hide resolved
else -> null
}
}

/**
Expand Down
Expand Up @@ -19,8 +19,8 @@ data class FailFastConfig(private val originalConfig: Config, private val defaul

override fun <T : Any> valueOrNull(key: String): T? {
return when (key) {
"active" -> true as? T
"maxIssues" -> 0 as? T
"active" -> originalConfig.valueOrNull(key) ?: true as? T
"maxIssues" -> originalConfig.valueOrNull(key) ?: 0 as? T
else -> originalConfig.valueOrNull(key) ?: defaultConfig.valueOrNull(key)
}
}
Expand Down
Expand Up @@ -11,16 +11,18 @@ open class TestConfig(

override fun subConfig(key: String) = this

override fun <T : Any> valueOrDefault(key: String, default: T) = if (key == "active") getActiveValue(default) as T
else valueOrDefaultInternal(key, values[key], default) as T
override fun <T : Any> valueOrDefault(key: String, default: T) =
if (key == "active") getActiveValue(default) as T
else valueOrDefaultInternal(key, values[key], default) as T

private fun <T : Any> getActiveValue(default: T): Any {
val active = values["active"]
return if (active != null) valueOrDefaultInternal("active", active, default) else true
}

override fun <T : Any> valueOrNull(key: String): T? = if (key == "active") (values["active"] ?: true) as T?
else values[key] as? T
override fun <T : Any> valueOrNull(key: String): T? =
if (key == "active") (values["active"] ?: true) as T?
else values[key] as? T

override fun tryParseBasedOnDefault(result: String, defaultResult: Any): Any = when (defaultResult) {
is List<*> -> parseList(result)
Expand Down
Expand Up @@ -17,7 +17,7 @@ A configuration holds information about how to configure specific rules.
### Functions

| [subConfig](sub-config.html) | Tries to retrieve part of the configuration based on given key.`abstract fun subConfig(key: `[`String`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)`): `[`Config`](./index.html) |
| [valueOrDefault](value-or-default.html) | Retrieves a sub configuration or value based on given key. If configuration property cannot be found the specified default value is returned.`abstract fun <T : `[`Any`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html)`> valueOrDefault(key: `[`String`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)`, default: T): T` |
| [valueOrDefault](value-or-default.html) | Retrieves a sub configuration or value based on given key. If configuration property cannot be found the specified default value is returned.`open fun <T : `[`Any`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html)`> valueOrDefault(key: `[`String`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)`, default: T): T` |
| [valueOrNull](value-or-null.html) | Retrieves a sub configuration or value based on given key. If the configuration property cannot be found, null is returned.`abstract fun <T : `[`Any`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html)`> valueOrNull(key: `[`String`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)`): T?` |

### Companion Object Properties
Expand Down
Expand Up @@ -6,7 +6,7 @@ title: Config.valueOrDefault - detekt-api

# valueOrDefault

`abstract fun <T : `[`Any`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html)`> valueOrDefault(key: `[`String`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)`, default: T): T`
`open fun <T : `[`Any`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html)`> valueOrDefault(key: `[`String`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)`, default: T): T`

Retrieves a sub configuration or value based on given key. If configuration property cannot be found
the specified default value is returned.
Expand Down