Implement copied key with ability to take default from source config

This commit is contained in:
Ilya Chernikov
2020-05-13 21:22:44 +02:00
parent 3c9207a2cd
commit 20bbcd5d5a
2 changed files with 39 additions and 5 deletions
@@ -37,6 +37,18 @@ open class PropertiesCollection(protected var properties: Map<Key<*>, Any?> = em
getDefaultValue: PropertiesCollection.() -> T?
) : 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) {
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)
}
class PropertyKeyCopyDelegate<T>(val source: Key<T>) {
operator fun getValue(thisRef: Any?, property: KProperty<*>): Key<T> = source
class PropertyKeyCopyDelegate<T>(
val source: Key<T>, val getSourceProperties: PropertiesCollection.() -> PropertiesCollection? = { null }
) {
operator fun getValue(thisRef: Any?, property: KProperty<*>): Key<T> = CopiedKey(source, getSourceProperties)
}
@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> =
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
private val serialVersionUID = 1L
@@ -7,6 +7,8 @@ package kotlin.script.experimental.test
import junit.framework.TestCase
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.ScriptingHostConfigurationKeys
import kotlin.script.experimental.host.withDefaultsFrom
@@ -23,6 +25,7 @@ class ConfigurationsTest : TestCase() {
val c2 = ScriptingHostConfiguration {
p1(2)
p2("yes")
p4("local")
}
assertEquals(c1, c1.withDefaultsFrom(c0))
@@ -35,8 +38,20 @@ class ConfigurationsTest : TestCase() {
assertEquals(1, c1c2[ScriptingHostConfiguration.p1])
assertEquals("yes", c1c2[ScriptingHostConfiguration.p2])
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.p2 by PropertiesCollection.key<String>("-")
private val ScriptingHostConfigurationKeys.p1 by PropertiesCollection.key(-1)
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 })