From e835d811833a19ed7a94f6559f2143f602c66e4a Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Mon, 16 Mar 2020 22:52:54 +0300 Subject: [PATCH] Utility methods to access Configurable properties --- build.gradle.kts | 4 +- .../meta/descriptors/ItemDescriptor.kt | 1 + .../hep/dataforge/meta/scheme/Configurable.kt | 39 +++++++++--------- .../meta/scheme/ConfigurableDelegate.kt | 40 +++++++++++++------ .../hep/dataforge/meta/scheme/Scheme.kt | 8 ++-- 5 files changed, 55 insertions(+), 37 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 7e41aa3b..1fe59eff 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,12 +1,12 @@ plugins { - val toolsVersion = "0.4.0-dev" + val toolsVersion = "0.4.0" id("scientifik.mpp") version toolsVersion apply false id("scientifik.jvm") version toolsVersion apply false id("scientifik.publish") version toolsVersion apply false } -val dataforgeVersion by extra("0.1.5-dev-11") +val dataforgeVersion by extra("0.1.5") val bintrayRepo by extra("dataforge") val githubProject by extra("dataforge-core") diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/descriptors/ItemDescriptor.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/descriptors/ItemDescriptor.kt index 4ff5de11..84d0d5c0 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/descriptors/ItemDescriptor.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/descriptors/ItemDescriptor.kt @@ -67,6 +67,7 @@ fun ItemDescriptor.validateItem(item: MetaItem<*>?): Boolean { * * @author Alexander Nozik */ +@DFBuilder class NodeDescriptor : ItemDescriptor() { /** diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/scheme/Configurable.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/scheme/Configurable.kt index df8c8e93..4ae5e180 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/scheme/Configurable.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/scheme/Configurable.kt @@ -32,34 +32,37 @@ interface Configurable : Described { } override val descriptor: NodeDescriptor? get() = null -} -/** - * Get a property with default - */ -fun Configurable.getProperty(name: Name): MetaItem<*>? = - config[name] ?: getDefaultItem(name) ?: descriptor?.get(name)?.defaultItem() + /** + * Get a property with default + */ + fun getProperty(name: Name): MetaItem<*>? = + config[name] ?: getDefaultItem(name) ?: descriptor?.get(name)?.defaultItem() -fun Configurable.getProperty(key: String) = getProperty(key.toName()) - -/** - * Set a configurable property - */ -fun Configurable.setProperty(name: Name, item: MetaItem<*>?) { - if (validateItem(name, item)) { - config[name] = item - } else { - error("Validation failed for property $name with value $item") + /** + * Set a configurable property + */ + fun setProperty(name: Name, item: MetaItem<*>?) { + if (validateItem(name, item)) { + config[name] = item + } else { + error("Validation failed for property $name with value $item") + } } } -fun Configurable.setProperty(name: Name, value: Value) = setProperty(name, MetaItem.ValueItem(value)) -fun Configurable.setProperty(name: Name, meta: Meta) = setProperty(name, MetaItem.NodeItem(meta)) +fun Configurable.getProperty(key: String) = getProperty(key.toName()) + +fun Configurable.setProperty(name: Name, value: Value?) = setProperty(name, value?.let { MetaItem.ValueItem(value) }) +fun Configurable.setProperty(name: Name, meta: Meta?) = setProperty(name, meta?.let { MetaItem.NodeItem(meta) }) fun Configurable.setProperty(key: String, item: MetaItem<*>?) { setProperty(key.toName(), item) } +fun Configurable.setProperty(key: String, value: Value?) = setProperty(key, value?.let { MetaItem.ValueItem(value) }) +fun Configurable.setProperty(key: String, meta: Meta?) = setProperty(key, meta?.let { MetaItem.NodeItem(meta) }) + fun T.configure(meta: Meta): T = this.apply { config.update(meta) } inline fun T.configure(action: Config.() -> Unit): T = apply { config.apply(action) } diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/scheme/ConfigurableDelegate.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/scheme/ConfigurableDelegate.kt index a83bc62c..c582282b 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/scheme/ConfigurableDelegate.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/scheme/ConfigurableDelegate.kt @@ -170,7 +170,7 @@ fun Configurable.float(default: Float, key: Name? = null): ReadWriteProperty> Configurable.enum( default: E, key: Name? = null, resolve: MetaItem<*>.() -> E? -): ReadWriteProperty = item(default, key).transform {it?.resolve() ?: default } +): ReadWriteProperty = item(default, key).transform { it?.resolve() ?: default } /* * Extra delegates for special cases @@ -206,17 +206,31 @@ fun Configurable.node(key: Name? = null): ReadWriteProperty = item( writer = { it?.let { MetaItem.NodeItem(it) } } ) -fun Configurable.spec(spec: Specification, key: Name? = null): ReadWriteProperty = - object : ReadWriteProperty { - override fun getValue(thisRef: Any?, property: KProperty<*>): T? { - val name = key ?: property.name.asName() - return config[name].node?.let { spec.wrap(it) } - } - - override fun setValue(thisRef: Any?, property: KProperty<*>, value: T?) { - val name = key ?: property.name.asName() - config[name] = value?.config - } - +fun Configurable.spec( + spec: Specification, key: Name? = null +): ReadWriteProperty = object : ReadWriteProperty { + override fun getValue(thisRef: Any?, property: KProperty<*>): T? { + val name = key ?: property.name.asName() + return config[name].node?.let { spec.wrap(it) } } + override fun setValue(thisRef: Any?, property: KProperty<*>, value: T?) { + val name = key ?: property.name.asName() + config[name] = value?.config + } +} + +fun Configurable.spec( + spec: Specification, default: T, key: Name? = null +): ReadWriteProperty = object : ReadWriteProperty { + override fun getValue(thisRef: Any?, property: KProperty<*>): T { + val name = key ?: property.name.asName() + return config[name].node?.let { spec.wrap(it) }?:default + } + + override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) { + val name = key ?: property.name.asName() + config[name] = value.config + } +} + diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/scheme/Scheme.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/scheme/Scheme.kt index 8e74ba7b..b8d6257f 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/scheme/Scheme.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/scheme/Scheme.kt @@ -18,11 +18,10 @@ open class Scheme() : Configurable, Described, MetaRepr { //constructor(config: Config, default: Meta) : this(config, { default[it] }) constructor(config: Config) : this(config, { null }) - final override var config: Config = - Config() + final override var config: Config = Config() internal set - lateinit var defaultProvider: (Name) -> MetaItem<*>? + var defaultProvider: (Name) -> MetaItem<*>? = { null } internal set final override var descriptor: NodeDescriptor? = null @@ -53,9 +52,10 @@ open class Scheme() : Configurable, Described, MetaRepr { token to item } ?: emptyMap() } - } +inline operator fun T.invoke(block: T.() -> Unit) = apply(block) + /** * A specification for simplified generation of wrappers */