[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:
+4
-12
@@ -72,18 +72,10 @@ fun createEvaluationConfigurationFromTemplate(
|
|||||||
private fun ScriptCompilationConfiguration.Builder.propertiesFromTemplate(
|
private fun ScriptCompilationConfiguration.Builder.propertiesFromTemplate(
|
||||||
templateClass: KClass<*>, baseClassType: KotlinType, mainAnnotation: KotlinScript
|
templateClass: KClass<*>, baseClassType: KotlinType, mainAnnotation: KotlinScript
|
||||||
) {
|
) {
|
||||||
if (baseClass() == null) {
|
baseClass.replaceOnlyDefault(if (templateClass == baseClassType.fromClass) baseClassType else KotlinType(templateClass))
|
||||||
baseClass(if (templateClass == baseClassType.fromClass) baseClassType else KotlinType(templateClass))
|
fileExtension.replaceOnlyDefault(mainAnnotation.fileExtension)
|
||||||
}
|
filePathPattern.replaceOnlyDefault(mainAnnotation.filePathPattern)
|
||||||
if (fileExtension() == null) {
|
displayName.replaceOnlyDefault(mainAnnotation.displayName)
|
||||||
fileExtension(mainAnnotation.fileExtension)
|
|
||||||
}
|
|
||||||
if (filePathPattern() == null) {
|
|
||||||
filePathPattern(mainAnnotation.filePathPattern)
|
|
||||||
}
|
|
||||||
if (displayName() == null) {
|
|
||||||
displayName(mainAnnotation.displayName)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private val KClass<*>.kotlinScriptAnnotation: KotlinScript
|
private val KClass<*>.kotlinScriptAnnotation: KotlinScript
|
||||||
|
|||||||
+14
-8
@@ -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>?) {
|
fun <T> PropertiesCollection.Key<in List<T>>.putIfAny(vals: Iterable<T>?) {
|
||||||
if (vals?.any() == true) {
|
if (vals?.any() == true) {
|
||||||
data[this] = if (vals is List) vals else vals.toList()
|
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")
|
@JvmName("putIfAny_map")
|
||||||
fun <K, V> PropertiesCollection.Key<in Map<K, V>>.putIfAny(vals: Iterable<Pair<K, V>>?) {
|
fun <K, V> PropertiesCollection.Key<in Map<K, V>>.putIfAny(vals: Iterable<Pair<K, V>>?) {
|
||||||
if (vals?.any() == true) {
|
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>) {
|
operator fun <K, V> PropertiesCollection.Key<Map<K, V>>.invoke(vararg vs: Pair<K, V>) {
|
||||||
append(vs.asIterable())
|
append(vs.asIterable())
|
||||||
}
|
}
|
||||||
|
|||||||
+35
@@ -125,6 +125,41 @@ class ConfigurationDslTest : TestCase() {
|
|||||||
|
|
||||||
Assert.assertEquals(propAnn1 + propAnn12, (evalRes.returnValue as ResultValue.Value).value)
|
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)
|
@Target(AnnotationTarget.FILE)
|
||||||
|
|||||||
Reference in New Issue
Block a user