[minor] Refactor properties collection builder:

add a helper to replace the value only if it is absent or default
rearrange some functions
This commit is contained in:
Ilya Chernikov
2019-07-23 15:08:20 +02:00
parent 0af52f2fa6
commit 961607673d
3 changed files with 53 additions and 20 deletions
@@ -72,18 +72,10 @@ fun createEvaluationConfigurationFromTemplate(
private fun ScriptCompilationConfiguration.Builder.propertiesFromTemplate(
templateClass: KClass<*>, baseClassType: KotlinType, mainAnnotation: KotlinScript
) {
if (baseClass() == null) {
baseClass(if (templateClass == baseClassType.fromClass) baseClassType else KotlinType(templateClass))
}
if (fileExtension() == null) {
fileExtension(mainAnnotation.fileExtension)
}
if (filePathPattern() == null) {
filePathPattern(mainAnnotation.filePathPattern)
}
if (displayName() == null) {
displayName(mainAnnotation.displayName)
}
baseClass.replaceOnlyDefault(if (templateClass == baseClassType.fromClass) baseClassType else KotlinType(templateClass))
fileExtension.replaceOnlyDefault(mainAnnotation.fileExtension)
filePathPattern.replaceOnlyDefault(mainAnnotation.filePathPattern)
displayName.replaceOnlyDefault(mainAnnotation.displayName)
}
private val KClass<*>.kotlinScriptAnnotation: KotlinScript
@@ -89,12 +89,26 @@ open class PropertiesCollection(private val properties: Map<Key<*>, Any?> = empt
}
}
fun <T> PropertiesCollection.Key<T>.replaceOnlyDefault(v: T?) {
if (!data.containsKey(this) || data[this] == this.getDefaultValue(PropertiesCollection(data))) {
data[this] = v
}
}
// generic for lists
fun <T> PropertiesCollection.Key<in List<T>>.putIfAny(vals: Iterable<T>?) {
if (vals?.any() == true) {
data[this] = if (vals is List) vals else vals.toList()
}
}
operator fun <T> PropertiesCollection.Key<in List<T>>.invoke(vararg vals: T) {
append(vals.asIterable())
}
// generic for maps:
@JvmName("putIfAny_map")
fun <K, V> PropertiesCollection.Key<in Map<K, V>>.putIfAny(vals: Iterable<Pair<K, V>>?) {
if (vals?.any() == true) {
@@ -108,14 +122,6 @@ open class PropertiesCollection(private val properties: Map<Key<*>, Any?> = empt
}
}
// generic for lists
operator fun <T> PropertiesCollection.Key<in List<T>>.invoke(vararg vals: T) {
append(vals.asIterable())
}
// generic for maps:
operator fun <K, V> PropertiesCollection.Key<Map<K, V>>.invoke(vararg vs: Pair<K, V>) {
append(vs.asIterable())
}