mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-04-19 08:31:29 +00:00
[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:
@@ -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())
|
||||
}
|
||||
|
||||
@@ -125,6 +125,41 @@ class ConfigurationDslTest : TestCase() {
|
||||
|
||||
Assert.assertEquals(propAnn1 + propAnn12, (evalRes.returnValue as ResultValue.Value).value)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDefaultConfiguration() {
|
||||
val script = "val x = 1".toScriptSource()
|
||||
|
||||
val evalRes = runBlocking {
|
||||
JvmScriptCompiler(defaultJvmScriptingHostConfiguration).invoke(script, ScriptCompilationConfiguration()).onSuccess {
|
||||
BasicJvmScriptEvaluator().invoke(it, ScriptEvaluationConfiguration())
|
||||
}.valueOrThrow()
|
||||
}
|
||||
|
||||
val scriptObj = evalRes.returnValue.scriptInstance!!
|
||||
|
||||
Assert.assertEquals(Any::class.java, scriptObj::class.java.superclass)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testReplaceOnlyDefault() {
|
||||
val conf = ScriptCompilationConfiguration {
|
||||
displayName("1")
|
||||
baseClass(KotlinType(Any::class))
|
||||
}
|
||||
|
||||
val conf2 = conf.with {
|
||||
displayName.replaceOnlyDefault("2")
|
||||
baseClass.replaceOnlyDefault(KotlinType(Int::class))
|
||||
fileExtension.replaceOnlyDefault("ktx")
|
||||
filePathPattern.replaceOnlyDefault("x.*x")
|
||||
}
|
||||
|
||||
Assert.assertEquals("1", conf2[ScriptCompilationConfiguration.displayName])
|
||||
Assert.assertEquals(KotlinType(Int::class), conf2[ScriptCompilationConfiguration.baseClass])
|
||||
Assert.assertEquals("ktx", conf2[ScriptCompilationConfiguration.fileExtension])
|
||||
Assert.assertEquals("x.*x", conf2[ScriptCompilationConfiguration.filePathPattern])
|
||||
}
|
||||
}
|
||||
|
||||
@Target(AnnotationTarget.FILE)
|
||||
|
||||
Reference in New Issue
Block a user