Specialized non-null delegates for spec

This commit is contained in:
Alexander Nozik 2019-12-28 16:06:32 +03:00
parent e532e8358e
commit 1b879eccc7
4 changed files with 27 additions and 10 deletions

View File

@ -6,7 +6,7 @@ plugins {
id("scientifik.publish") version toolsVersion apply false
}
val dataforgeVersion by extra("0.1.5-dev-5")
val dataforgeVersion by extra("0.1.5-dev-6")
val bintrayRepo by extra("dataforge")
val githubProject by extra("dataforge-core")

View File

@ -1,7 +1,7 @@
package hep.dataforge.descriptors
import hep.dataforge.descriptors.Described.Companion.DESCRIPTOR_NODE
import hep.dataforge.meta.Meta
import hep.dataforge.meta.MetaRepr
import hep.dataforge.meta.get
import hep.dataforge.meta.node
@ -19,11 +19,11 @@ interface Described {
/**
* If meta node supplies explicit descriptor, return it, otherwise try to use descriptor node from meta itself
*/
val Meta.descriptor: NodeDescriptor?
val MetaRepr.descriptor: NodeDescriptor?
get() {
return if (this is Described) {
descriptor
} else {
get(DESCRIPTOR_NODE).node?.let { NodeDescriptor.wrap(it) }
toMeta()[DESCRIPTOR_NODE].node?.let { NodeDescriptor.wrap(it) }
}
}

View File

@ -1,7 +1,10 @@
package hep.dataforge.meta
import hep.dataforge.names.Name
import hep.dataforge.names.asName
import kotlin.jvm.JvmName
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
/**
* Marker interface for classes with specifications
@ -61,13 +64,27 @@ fun <C : Specific, S : Specification<C>> Specific.update(spec: S, action: C.() -
fun <C : Specific, S : Specification<C>> S.createStyle(action: C.() -> Unit): Meta =
Config().also { update(it, action) }
class SpecDelegate<T : Specific, S : Specification<T>>(
val target: Specific,
val spec: S,
val key: Name? = null
) : ReadWriteProperty<Any?, T> {
override fun getValue(thisRef: Any?, property: KProperty<*>): T {
return target.config[key ?: property.name.asName()]?.node?.let { spec.wrap(it) } ?: spec.empty()
}
fun <C : Specific> Specific.spec(
spec: Specification<C>,
override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
target.config[key ?: property.name.asName()] = value.config
}
}
fun <T : Specific, S : Specification<T>> Specific.spec(
spec: S,
key: Name? = null
): MutableMorphDelegate<Config, C> = MutableMorphDelegate(config, key) { spec.wrap(it) }
): SpecDelegate<T, S> = SpecDelegate(this, spec, key)
fun <T : Specific> MetaItem<*>.spec(spec: Specification<T>): T? = node?.let { spec.wrap(it) }
@JvmName("configSpec")
fun <T : Specific> MetaItem<Config>.spec(spec: Specification<T>): T? = node?.let { spec.wrap(it) }

View File

@ -350,8 +350,8 @@ class MutableNodeDelegate<M : MutableMeta<M>>(
}
}
class MutableMorphDelegate<M : MutableMeta<M>, T : Configurable>(
val meta: M,
class MutableMorphDelegate<T : Configurable>(
val meta: MutableMeta<*>,
private val key: Name? = null,
private val converter: (Meta) -> T
) : ReadWriteProperty<Any?, T?> {