forked from detekt/detekt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeprecatedPropertiesConfigValidator.kt
44 lines (39 loc) · 1.58 KB
/
DeprecatedPropertiesConfigValidator.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package io.gitlab.arturbosch.detekt.core.config.validation
import io.gitlab.arturbosch.detekt.api.Notification
import io.gitlab.arturbosch.detekt.api.internal.SimpleNotification
import io.gitlab.arturbosch.detekt.core.config.YamlConfig
internal class DeprecatedPropertiesConfigValidator(
private val deprecatedProperties: Map<String, String>
) : AbstractYamlConfigValidator() {
override fun validate(
configToValidate: YamlConfig,
settings: ValidationSettings
): Collection<Notification> {
val configAsMap = configToValidate.properties
return deprecatedProperties
.map { (path, description) -> path.split(">") to description }
.filter { (path, _) -> configAsMap.hasValue(path) }
.map { (path, description) -> createNotification(path, description) }
}
@Suppress("UNCHECKED_CAST")
private fun Map<String, Any>.hasValue(propertyPath: List<String>): Boolean {
if (propertyPath.isEmpty()) {
return false
}
if (propertyPath.size == 1) {
return this.containsKey(propertyPath.first())
}
val subMap = this[propertyPath.first()] as? Map<String, Any> ?: return false
return subMap.hasValue(propertyPath.drop(1))
}
private fun createNotification(
propertyPath: List<String>,
deprecationDescription: String
): Notification {
val prop = propertyPath.joinToString(">")
return SimpleNotification(
"Property '$prop' is deprecated. $deprecationDescription.",
Notification.Level.Warning
)
}
}