/
ConfigProperty.kt
212 lines (195 loc) · 10.1 KB
/
ConfigProperty.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package io.gitlab.arturbosch.detekt.api
import io.gitlab.arturbosch.detekt.api.internal.valueOrDefaultCommaSeparated
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
import kotlin.reflect.KProperty0
/**
* Creates a delegated read-only property that can be used in [ConfigAware] objects. The name of the property is the
* key that is used during configuration lookup. The value of the property is evaluated only once.
*
* @param defaultValue the value that the property evaluates to when there is no key with the name of the property in
* the config. Although [T] is defined as [Any], only [String], [Int], [Boolean] and [List<String>] are supported.
*/
fun <T : Any> config(
defaultValue: T
): ReadOnlyProperty<ConfigAware, T> = config(defaultValue) { it }
/**
* Creates a delegated read-only property that can be used in [ConfigAware] objects. The name of the property is the
* key that is used during configuration lookup. The value of the property is evaluated and transformed only once.
*
* @param defaultValue the value that the property evaluates to when there is no key with the name of the property in
* the config. Although [T] is defined as [Any], only [String], [Int], [Boolean] and [List<String>] are supported.
* @param transformer a function that transforms the value from the configuration (or the default) into its final
* value. A typical use case for this is a conversion from a [String] into a [Regex].
*/
fun <T : Any, U : Any> config(
defaultValue: T,
transformer: (T) -> U
): ReadOnlyProperty<ConfigAware, U> = TransformedConfigProperty(defaultValue, transformer)
/**
* Creates a delegated read-only property that can be used in [ConfigAware] objects. The name of the property is the
* key that is used during configuration lookup. If there is no such property, the value of the
* supplied [fallbackProperty] is also considered before using the [defaultValue].
* The value of the property is evaluated only once.
*
* This method is only intended to be used in migration scenarios where there is no way to update all configuration
* files immediately.
*
* @param fallbackProperty The reference to the configuration key to fall back to. This property must be defined as a
* configuration delegate.
* @param defaultValue the value that the property evaluates to when there is no key with the name of the property in
* the config. Although [T] is defined as [Any], only [String], [Int], [Boolean] and [List<String>] are supported.
*/
@UnstableApi("fallback property handling is still under discussion")
fun <T : Any> configWithFallback(
fallbackProperty: KProperty0<T>,
defaultValue: T
): ReadOnlyProperty<ConfigAware, T> = configWithFallback(fallbackProperty, defaultValue) { it }
/**
* Creates a delegated read-only property that can be used in [ConfigAware] objects. The name of the property is the
* key that is used during configuration lookup. If there is no such property, the value of the
* supplied [fallbackProperty] is also considered before using the [defaultValue].
* The value of the property is evaluated and transformed only once.
*
* This method is only intended to be used in migration scenarios where there is no way to update all configuration
* files immediately.
*
* @param fallbackProperty The reference to the configuration key to fall back to. This property must be defined as a
* configuration delegate.
* @param defaultValue the value that the property evaluates to when there is no key with the name of the property in
* the config. Although [T] is defined as [Any], only [String], [Int], [Boolean] and [List<String>] are supported.
* @param transformer a function that transforms the value from the configuration (or the default) into its final
* value.
*/
@UnstableApi("fallback property handling is still under discussion")
fun <T : Any, U : Any> configWithFallback(
fallbackProperty: KProperty0<U>,
defaultValue: T,
transformer: (T) -> U
): ReadOnlyProperty<ConfigAware, U> =
FallbackConfigProperty(fallbackProperty, defaultValue, transformer)
/**
* Creates a delegated read-only property that can be used in [ConfigAware] objects. The name of the property is the
* key that is used during configuration lookup. The value of the property is evaluated only once.
*
* @param defaultValue the value that the property evaluates to when there is no key with the name of the property in
* the config. Although [T] is defined as [Any], only [String], [Int], [Boolean] and [List<String>] are supported.
* @param defaultAndroidValue the value that the property evaluates to when there is no key with the name of the
* property in the config and there is a configuration property in the rule set named "android" that is set to
* <code>true</code>.
*/
fun <T : Any> configWithAndroidVariants(
defaultValue: T,
defaultAndroidValue: T,
): ReadOnlyProperty<ConfigAware, T> = configWithAndroidVariants(defaultValue, defaultAndroidValue) { it }
/**
* Creates a delegated read-only property that can be used in [ConfigAware] objects. The name of the property is the
* key that is used during configuration lookup. The value of the property is evaluated and transformed only once.
*
* @param defaultValue the value that the property evaluates to when there is no key with the name of the property in
* the config. Although [T] is defined as [Any], only [String], [Int], [Boolean] and [List<String>] are supported.
* @param defaultAndroidValue the value that the property evaluates to when there is no key with the name of the
* property in the config and there is a configuration property in the rule set named "android" that is set to
* <code>true</code>.
* @param transformer a function that transforms the value from the configuration (or the default) into its final
* value.
*/
fun <T : Any, U : Any> configWithAndroidVariants(
defaultValue: T,
defaultAndroidValue: T,
transformer: (T) -> U
): ReadOnlyProperty<ConfigAware, U> =
TransformedConfigPropertyWithAndroidVariants(defaultValue, defaultAndroidValue, transformer)
private fun <T : Any> getValueOrDefault(configAware: ConfigAware, propertyName: String, defaultValue: T): T {
@Suppress("UNCHECKED_CAST")
return when (defaultValue) {
is ValuesWithReason -> configAware.getValuesWithReasonOrDefault(propertyName, defaultValue) as T
is List<*> -> configAware.getListOrDefault(propertyName, defaultValue) as T
is String,
is Boolean,
is Int -> configAware.valueOrDefault(propertyName, defaultValue)
else -> error(
"${defaultValue.javaClass} is not supported for delegated config property '$propertyName'. " +
"Use one of String, Boolean, Int or List<String> instead."
)
}
}
private fun ConfigAware.getListOrDefault(propertyName: String, defaultValue: List<*>): List<String> {
return if (defaultValue.all { it is String }) {
@Suppress("UNCHECKED_CAST")
val defaultValueAsListOfStrings = defaultValue as List<String>
valueOrDefaultCommaSeparated(propertyName, defaultValueAsListOfStrings)
} else {
error("Only lists of strings are supported. '$propertyName' is invalid. ")
}
}
private fun ConfigAware.getValuesWithReasonOrDefault(
propertyName: String,
defaultValue: ValuesWithReason
): ValuesWithReason {
val valuesAsList: List<*> = valueOrNull(propertyName) ?: return defaultValue
if (valuesAsList.all { it is String }) {
return ValuesWithReason(values = valuesAsList.map { ValueWithReason(it as String) })
}
if (valuesAsList.all { it is Map<*, *> }) {
return ValuesWithReason(
valuesAsList
.map { it as Map<*, *> }
.map { dict ->
try {
ValueWithReason(
value = dict["value"] as String,
reason = dict["reason"] as String?
)
} catch (e: ClassCastException) {
throw Config.InvalidConfigurationError(e)
} catch (@Suppress("TooGenericExceptionCaught") e: NullPointerException) {
throw Config.InvalidConfigurationError(e)
}
}
)
}
error("Only lists of strings or maps with keys 'value' and 'reason' are supported. '$propertyName' is invalid.")
}
private abstract class MemoizedConfigProperty<U : Any> : ReadOnlyProperty<ConfigAware, U> {
private var value: U? = null
override fun getValue(thisRef: ConfigAware, property: KProperty<*>): U {
return value ?: doGetValue(thisRef, property).also { value = it }
}
abstract fun doGetValue(thisRef: ConfigAware, property: KProperty<*>): U
}
private class TransformedConfigPropertyWithAndroidVariants<T : Any, U : Any>(
private val defaultValue: T,
private val defaultAndroidValue: T,
private val transform: (T) -> U
) : MemoizedConfigProperty<U>() {
override fun doGetValue(thisRef: ConfigAware, property: KProperty<*>): U {
val isAndroid = getValueOrDefault(thisRef, "android", false)
val value = if (isAndroid) defaultAndroidValue else defaultValue
return transform(getValueOrDefault(thisRef, property.name, value))
}
}
private class TransformedConfigProperty<T : Any, U : Any>(
private val defaultValue: T,
private val transform: (T) -> U
) : MemoizedConfigProperty<U>() {
override fun doGetValue(thisRef: ConfigAware, property: KProperty<*>): U {
return transform(getValueOrDefault(thisRef, property.name, defaultValue))
}
}
private class FallbackConfigProperty<T : Any, U : Any>(
private val fallbackProperty: KProperty0<U>,
private val defaultValue: T,
private val transform: (T) -> U
) : MemoizedConfigProperty<U>() {
override fun doGetValue(thisRef: ConfigAware, property: KProperty<*>): U {
if (thisRef.isConfigured(property.name)) {
return transform(getValueOrDefault(thisRef, property.name, defaultValue))
}
if (thisRef.isConfigured(fallbackProperty.name)) {
return fallbackProperty.get()
}
return transform(defaultValue)
}
private fun ConfigAware.isConfigured(propertyName: String) = valueOrNull<Any>(propertyName) != null
}