Implement copied key with ability to take default from source config
This commit is contained in:
+22
-3
@@ -37,6 +37,18 @@ open class PropertiesCollection(protected var properties: Map<Key<*>, Any?> = em
|
|||||||
getDefaultValue: PropertiesCollection.() -> T?
|
getDefaultValue: PropertiesCollection.() -> T?
|
||||||
) : Key<T>(name, getDefaultValue)
|
) : Key<T>(name, getDefaultValue)
|
||||||
|
|
||||||
|
class CopiedKey<T>(
|
||||||
|
source: Key<T>,
|
||||||
|
getSourceProperties: PropertiesCollection.() -> PropertiesCollection?
|
||||||
|
) : Key<T>(
|
||||||
|
source.name,
|
||||||
|
{
|
||||||
|
val sourceProperties = getSourceProperties()
|
||||||
|
if (sourceProperties == null) source.getDefaultValue(this)
|
||||||
|
else sourceProperties.get(source)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
class PropertyKeyDelegate<T>(private val getDefaultValue: PropertiesCollection.() -> T?, val isTransient: Boolean = false) {
|
class PropertyKeyDelegate<T>(private val getDefaultValue: PropertiesCollection.() -> T?, val isTransient: Boolean = false) {
|
||||||
constructor(defaultValue: T?, isTransient: Boolean = false) : this({ defaultValue }, isTransient)
|
constructor(defaultValue: T?, isTransient: Boolean = false) : this({ defaultValue }, isTransient)
|
||||||
|
|
||||||
@@ -45,8 +57,10 @@ open class PropertiesCollection(protected var properties: Map<Key<*>, Any?> = em
|
|||||||
else Key(property.name, getDefaultValue)
|
else Key(property.name, getDefaultValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
class PropertyKeyCopyDelegate<T>(val source: Key<T>) {
|
class PropertyKeyCopyDelegate<T>(
|
||||||
operator fun getValue(thisRef: Any?, property: KProperty<*>): Key<T> = source
|
val source: Key<T>, val getSourceProperties: PropertiesCollection.() -> PropertiesCollection? = { null }
|
||||||
|
) {
|
||||||
|
operator fun getValue(thisRef: Any?, property: KProperty<*>): Key<T> = CopiedKey(source, getSourceProperties)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
@@ -87,7 +101,12 @@ open class PropertiesCollection(protected var properties: Map<Key<*>, Any?> = em
|
|||||||
fun <T> key(getDefaultValue: PropertiesCollection.() -> T?, isTransient: Boolean = false): PropertyKeyDelegate<T> =
|
fun <T> key(getDefaultValue: PropertiesCollection.() -> T?, isTransient: Boolean = false): PropertyKeyDelegate<T> =
|
||||||
PropertyKeyDelegate(getDefaultValue, isTransient)
|
PropertyKeyDelegate(getDefaultValue, isTransient)
|
||||||
|
|
||||||
fun <T> keyCopy(source: Key<T>): PropertyKeyCopyDelegate<T> = PropertyKeyCopyDelegate(source)
|
// fun <T> keyCopy(source: Key<T>): PropertyKeyCopyDelegate<T> = PropertyKeyCopyDelegate(source)
|
||||||
|
|
||||||
|
fun <T> keyCopy(
|
||||||
|
source: Key<T>, getSourceProperties: PropertiesCollection.() -> PropertiesCollection? = { null }
|
||||||
|
): PropertyKeyCopyDelegate<T> =
|
||||||
|
PropertyKeyCopyDelegate(source, getSourceProperties)
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
private val serialVersionUID = 1L
|
private val serialVersionUID = 1L
|
||||||
|
|||||||
+17
-2
@@ -7,6 +7,8 @@ package kotlin.script.experimental.test
|
|||||||
|
|
||||||
import junit.framework.TestCase
|
import junit.framework.TestCase
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
|
import kotlin.script.experimental.api.ScriptCompilationConfiguration
|
||||||
|
import kotlin.script.experimental.api.ScriptCompilationConfigurationKeys
|
||||||
import kotlin.script.experimental.host.ScriptingHostConfiguration
|
import kotlin.script.experimental.host.ScriptingHostConfiguration
|
||||||
import kotlin.script.experimental.host.ScriptingHostConfigurationKeys
|
import kotlin.script.experimental.host.ScriptingHostConfigurationKeys
|
||||||
import kotlin.script.experimental.host.withDefaultsFrom
|
import kotlin.script.experimental.host.withDefaultsFrom
|
||||||
@@ -23,6 +25,7 @@ class ConfigurationsTest : TestCase() {
|
|||||||
val c2 = ScriptingHostConfiguration {
|
val c2 = ScriptingHostConfiguration {
|
||||||
p1(2)
|
p1(2)
|
||||||
p2("yes")
|
p2("yes")
|
||||||
|
p4("local")
|
||||||
}
|
}
|
||||||
|
|
||||||
assertEquals(c1, c1.withDefaultsFrom(c0))
|
assertEquals(c1, c1.withDefaultsFrom(c0))
|
||||||
@@ -35,8 +38,20 @@ class ConfigurationsTest : TestCase() {
|
|||||||
assertEquals(1, c1c2[ScriptingHostConfiguration.p1])
|
assertEquals(1, c1c2[ScriptingHostConfiguration.p1])
|
||||||
assertEquals("yes", c1c2[ScriptingHostConfiguration.p2])
|
assertEquals("yes", c1c2[ScriptingHostConfiguration.p2])
|
||||||
assertEquals(2, c2c1[ScriptingHostConfiguration.p1])
|
assertEquals(2, c2c1[ScriptingHostConfiguration.p1])
|
||||||
|
|
||||||
|
assertEquals("from delegated", c1[ScriptingHostConfiguration.p4])
|
||||||
|
assertEquals("local", c2[ScriptingHostConfiguration.p4])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private val ScriptingHostConfigurationKeys.p1 by PropertiesCollection.key<Int>(-1)
|
private val ScriptingHostConfigurationKeys.p1 by PropertiesCollection.key(-1)
|
||||||
private val ScriptingHostConfigurationKeys.p2 by PropertiesCollection.key<String>("-")
|
private val ScriptingHostConfigurationKeys.p2 by PropertiesCollection.key("-")
|
||||||
|
|
||||||
|
private val ScriptCompilationConfigurationKeys.p3 by PropertiesCollection.key<String>()
|
||||||
|
|
||||||
|
private val delegatedConfig = ScriptCompilationConfiguration {
|
||||||
|
p3("from delegated")
|
||||||
|
}
|
||||||
|
|
||||||
|
private val ScriptingHostConfigurationKeys.p4 by PropertiesCollection.keyCopy(ScriptCompilationConfiguration.p3, { delegatedConfig })
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user