Cleanup delegates. Fix a lot of bugs

This commit is contained in:
Alexander Nozik 2021-08-01 14:28:11 +03:00
parent 3ba5a9076b
commit 7aec2f3547
15 changed files with 251 additions and 271 deletions

View File

@ -96,7 +96,7 @@ public open class Context internal constructor(
override fun toMeta(): Meta = Meta { override fun toMeta(): Meta = Meta {
"parent" to parent?.name "parent" to parent?.name
properties.layers.firstOrNull()?.let { set("properties", it) } properties.layers.firstOrNull()?.let { set("properties", it) }
"plugins" put plugins.map { it.toMeta() } "plugins" putIndexed plugins.map { it.toMeta() }
} }
public companion object { public companion object {

View File

@ -4,6 +4,7 @@ import kotlinx.coroutines.*
import kotlinx.coroutines.flow.* import kotlinx.coroutines.flow.*
import space.kscience.dataforge.data.Data.Companion.TYPE_OF_NOTHING import space.kscience.dataforge.data.Data.Companion.TYPE_OF_NOTHING
import space.kscience.dataforge.meta.Meta import space.kscience.dataforge.meta.Meta
import space.kscience.dataforge.meta.set
import space.kscience.dataforge.names.* import space.kscience.dataforge.names.*
import kotlin.reflect.KType import kotlin.reflect.KType

View File

@ -14,7 +14,7 @@ import space.kscience.dataforge.names.plus
private class PartDescriptor : Scheme() { private class PartDescriptor : Scheme() {
var offset by int(0) var offset by int(0)
var size by int(0) var size by int(0)
var partMeta by item("meta".asName()) var partMeta by node("meta".asName())
companion object : SchemeSpec<PartDescriptor>(::PartDescriptor) { companion object : SchemeSpec<PartDescriptor>(::PartDescriptor) {
val MULTIPART_KEY = ENVELOPE_NODE_KEY + "multipart" val MULTIPART_KEY = ENVELOPE_NODE_KEY + "multipart"

View File

@ -34,7 +34,15 @@ public interface Meta : MetaRepr, MetaProvider {
public val value: Value? public val value: Value?
public val items: Map<NameToken, Meta> public val items: Map<NameToken, Meta>
override fun getMeta(name: Name): Meta? = find(name) override fun getMeta(name: Name): Meta?{
tailrec fun Meta.find(name: Name): Meta? = if (name.isEmpty()) {
this
} else {
items[name.firstOrNull()!!]?.find(name.cutFirst())
}
return find(name)
}
override fun toMeta(): Meta = this override fun toMeta(): Meta = this
@ -75,12 +83,6 @@ public interface Meta : MetaRepr, MetaProvider {
public fun toString(meta: Meta): String = json.encodeToString(MetaSerializer, meta) public fun toString(meta: Meta): String = json.encodeToString(MetaSerializer, meta)
public val EMPTY: Meta = SealedMeta(null, emptyMap()) public val EMPTY: Meta = SealedMeta(null, emptyMap())
private tailrec fun Meta.find(name: Name): Meta? = if (name.isEmpty()) {
this
} else {
items[name.firstOrNull()!!]?.find(name.cutFirst())
}
} }
} }

View File

@ -3,109 +3,83 @@ package space.kscience.dataforge.meta
import space.kscience.dataforge.meta.transformations.MetaConverter import space.kscience.dataforge.meta.transformations.MetaConverter
import space.kscience.dataforge.names.Name import space.kscience.dataforge.names.Name
import space.kscience.dataforge.names.asName import space.kscience.dataforge.names.asName
import space.kscience.dataforge.values.Value import space.kscience.dataforge.values.*
import kotlin.properties.ReadOnlyProperty import kotlin.properties.ReadOnlyProperty
/* Meta delegates */ /* Meta delegates */
public typealias MetaDelegate = ReadOnlyProperty<Any?, Meta?> public fun MetaProvider.node(key: Name? = null): ReadOnlyProperty<Any?, Meta?> = ReadOnlyProperty { _, property ->
public fun MetaProvider.item(key: Name? = null): MetaDelegate = ReadOnlyProperty { _, property ->
getMeta(key ?: property.name.asName()) getMeta(key ?: property.name.asName())
} }
//TODO add caching for sealed nodes public fun <T> MetaProvider.node(
key: Name? = null,
converter: MetaConverter<T>
/** ): ReadOnlyProperty<Any?, T?> = ReadOnlyProperty { _, property ->
* Apply a converter to this delegate creating a delegate with a custom type getMeta(key ?: property.name.asName())?.let { converter.metaToObject(it) }
*/
public fun <R : Any> MetaDelegate.convert(
converter: MetaConverter<R>,
): ReadOnlyProperty<Any?, R?> = ReadOnlyProperty { thisRef, property ->
this@convert.getValue(thisRef, property)?.let(converter::metaToObject)
} }
/*
*
*/
public fun <R : Any> MetaDelegate.convert(
converter: MetaConverter<R>,
default: () -> R,
): ReadOnlyProperty<Any?, R> = ReadOnlyProperty<Any?, R> { thisRef, property ->
this@convert.getValue(thisRef, property)?.let(converter::metaToObject) ?: default()
}
/**
* A converter with a custom reader transformation
*/
public fun <R> MetaDelegate.convert(
reader: (Meta?) -> R,
): ReadOnlyProperty<Any?, R> = ReadOnlyProperty<Any?, R> { thisRef, property ->
this@convert.getValue(thisRef, property).let(reader)
}
/* Read-only delegates for [Meta] */
/** /**
* A property delegate that uses custom key * A property delegate that uses custom key
*/ */
public fun MetaProvider.value(key: Name? = null): ReadOnlyProperty<Any?, Value?> = public fun MetaProvider.value(key: Name? = null): ReadOnlyProperty<Any?, Value?> = ReadOnlyProperty { _, property ->
item(key).convert(MetaConverter.value) getMeta(key ?: property.name.asName())?.value
}
public fun MetaProvider.string(key: Name? = null): ReadOnlyProperty<Any?, String?> = public fun <R> MetaProvider.value(
item(key).convert(MetaConverter.string) key: Name? = null,
reader: (Value?) -> R
): ReadOnlyProperty<Any?, R> = ReadOnlyProperty { _, property ->
reader(getMeta(key ?: property.name.asName())?.value)
}
public fun MetaProvider.boolean(key: Name? = null): ReadOnlyProperty<Any?, Boolean?> = //TODO add caching for sealed nodes
item(key).convert(MetaConverter.boolean)
public fun MetaProvider.number(key: Name? = null): ReadOnlyProperty<Any?, Number?> = /* Read-only delegates for [Meta] */
item(key).convert(MetaConverter.number)
public fun MetaProvider.double(key: Name? = null): ReadOnlyProperty<Any?, Double?> = public fun MetaProvider.string(key: Name? = null): ReadOnlyProperty<Any?, String?> = value(key) { it?.string }
item(key).convert(MetaConverter.double)
public fun MetaProvider.float(key: Name? = null): ReadOnlyProperty<Any?, Float?> = public fun MetaProvider.boolean(key: Name? = null): ReadOnlyProperty<Any?, Boolean?> = value(key) { it?.boolean }
item(key).convert(MetaConverter.float)
public fun MetaProvider.int(key: Name? = null): ReadOnlyProperty<Any?, Int?> = public fun MetaProvider.number(key: Name? = null): ReadOnlyProperty<Any?, Number?> = value(key) { it?.numberOrNull }
item(key).convert(MetaConverter.int)
public fun MetaProvider.long(key: Name? = null): ReadOnlyProperty<Any?, Long?> = public fun MetaProvider.double(key: Name? = null): ReadOnlyProperty<Any?, Double?> = value(key) { it?.double }
item(key).convert(MetaConverter.long)
public fun MetaProvider.node(key: Name? = null): ReadOnlyProperty<Any?, Meta?> = public fun MetaProvider.float(key: Name? = null): ReadOnlyProperty<Any?, Float?> = value(key) { it?.float }
item(key).convert(MetaConverter.meta)
public fun MetaProvider.int(key: Name? = null): ReadOnlyProperty<Any?, Int?> = value(key) { it?.int }
public fun MetaProvider.long(key: Name? = null): ReadOnlyProperty<Any?, Long?> = value(key) { it?.long }
public fun MetaProvider.string(default: String, key: Name? = null): ReadOnlyProperty<Any?, String> = public fun MetaProvider.string(default: String, key: Name? = null): ReadOnlyProperty<Any?, String> =
item(key).convert(MetaConverter.string) { default } value(key) { it?.string ?: default }
public fun MetaProvider.boolean(default: Boolean, key: Name? = null): ReadOnlyProperty<Any?, Boolean> = public fun MetaProvider.boolean(default: Boolean, key: Name? = null): ReadOnlyProperty<Any?, Boolean> =
item(key).convert(MetaConverter.boolean) { default } value(key) { it?.boolean ?: default }
public fun MetaProvider.number(default: Number, key: Name? = null): ReadOnlyProperty<Any?, Number> = public fun MetaProvider.number(default: Number, key: Name? = null): ReadOnlyProperty<Any?, Number> =
item(key).convert(MetaConverter.number) { default } value(key) { it?.numberOrNull ?: default }
public fun MetaProvider.double(default: Double, key: Name? = null): ReadOnlyProperty<Any?, Double> = public fun MetaProvider.double(default: Double, key: Name? = null): ReadOnlyProperty<Any?, Double> =
item(key).convert(MetaConverter.double) { default } value(key) { it?.double ?: default }
public fun MetaProvider.float(default: Float, key: Name? = null): ReadOnlyProperty<Any?, Float> = public fun MetaProvider.float(default: Float, key: Name? = null): ReadOnlyProperty<Any?, Float> =
item(key).convert(MetaConverter.float) { default } value(key) { it?.float ?: default }
public fun MetaProvider.int(default: Int, key: Name? = null): ReadOnlyProperty<Any?, Int> = public fun MetaProvider.int(default: Int, key: Name? = null): ReadOnlyProperty<Any?, Int> =
item(key).convert(MetaConverter.int) { default } value(key) { it?.int ?: default }
public fun MetaProvider.long(default: Long, key: Name? = null): ReadOnlyProperty<Any?, Long> = public fun MetaProvider.long(default: Long, key: Name? = null): ReadOnlyProperty<Any?, Long> =
item(key).convert(MetaConverter.long) { default } value(key) { it?.long ?: default }
public inline fun <reified E : Enum<E>> MetaProvider.enum(default: E, key: Name? = null): ReadOnlyProperty<Any?, E> = public inline fun <reified E : Enum<E>> MetaProvider.enum(default: E, key: Name? = null): ReadOnlyProperty<Any?, E> =
item(key).convert(MetaConverter.enum()) { default } value<E>(key) { it?.enum<E>() ?: default }
public fun MetaProvider.string(key: Name? = null, default: () -> String): ReadOnlyProperty<Any?, String> = public fun MetaProvider.string(key: Name? = null, default: () -> String): ReadOnlyProperty<Any?, String> =
item(key).convert(MetaConverter.string, default) value(key) { it?.string ?: default() }
public fun MetaProvider.boolean(key: Name? = null, default: () -> Boolean): ReadOnlyProperty<Any?, Boolean> = public fun MetaProvider.boolean(key: Name? = null, default: () -> Boolean): ReadOnlyProperty<Any?, Boolean> =
item(key).convert(MetaConverter.boolean, default) value(key) { it?.boolean ?: default() }
public fun MetaProvider.number(key: Name? = null, default: () -> Number): ReadOnlyProperty<Any?, Number> = public fun MetaProvider.number(key: Name? = null, default: () -> Number): ReadOnlyProperty<Any?, Number> =
item(key).convert(MetaConverter.number, default) value(key) { it?.numberOrNull ?: default() }

View File

@ -15,8 +15,13 @@ import kotlin.jvm.Synchronized
@DslMarker @DslMarker
public annotation class MetaBuilder public annotation class MetaBuilder
/**
* A generic interface that gives access to getting and setting meta notes and values
*/
public interface MutableMetaProvider : MetaProvider { public interface MutableMetaProvider : MetaProvider {
override fun getMeta(name: Name): MutableMeta?
public fun setMeta(name: Name, node: Meta?) public fun setMeta(name: Name, node: Meta?)
public fun setValue(name: Name, value: Value?)
} }
/** /**
@ -34,23 +39,19 @@ public interface MutableMeta : Meta, MutableMetaProvider {
*/ */
override var value: Value? override var value: Value?
/** override fun getMeta(name: Name): MutableMeta? {
* Set or replace node at given [name] tailrec fun MutableMeta.find(name: Name): MutableMeta? = if (name.isEmpty()) {
*/ this
public operator fun set(name: Name, meta: Meta)
override fun setMeta(name: Name, node: Meta?) {
if (node == null) {
remove(name)
} else { } else {
set(name, node) items[name.firstOrNull()!!]?.find(name.cutFirst())
}
} }
/** return find(name)
* Remove a node at a given [name] if it is present }
*/
public fun remove(name: Name) override fun setValue(name: Name, value: Value?) {
getOrCreate(name).value = value
}
/** /**
* Get existing node or create a new one * Get existing node or create a new one
@ -60,82 +61,92 @@ public interface MutableMeta : Meta, MutableMetaProvider {
//TODO to be moved to extensions with multi-receivers //TODO to be moved to extensions with multi-receivers
public infix fun Name.put(value: Value?) { public infix fun Name.put(value: Value?) {
set(this, value) setValue(this, value)
} }
public infix fun Name.put(string: String) { public infix fun Name.put(string: String) {
set(this, string.asValue()) setValue(this, string.asValue())
} }
public infix fun Name.put(number: Number) { public infix fun Name.put(number: Number) {
set(this, number.asValue()) setValue(this, number.asValue())
} }
public infix fun Name.put(boolean: Boolean) { public infix fun Name.put(boolean: Boolean) {
set(this, boolean.asValue()) setValue(this, boolean.asValue())
} }
public infix fun Name.put(enum: Enum<*>) { public infix fun Name.put(enum: Enum<*>) {
set(this, EnumValue(enum)) setValue(this, EnumValue(enum))
} }
public infix fun Name.put(iterable: Iterable<Meta>) { public infix fun Name.putIndexed(iterable: Iterable<Meta>) {
setIndexed(this, iterable) setIndexed(this, iterable)
} }
public infix fun Name.put(meta: Meta) { public infix fun Name.put(meta: Meta) {
set(this, meta) setMeta(this, meta)
} }
public infix fun Name.put(repr: MetaRepr) { public infix fun Name.put(repr: MetaRepr) {
put(repr.toMeta()) setMeta(this, repr.toMeta())
} }
public infix fun Name.put(mutableMeta: MutableMeta.() -> Unit) { public infix fun Name.put(mutableMeta: MutableMeta.() -> Unit) {
set(this, Meta(mutableMeta)) setMeta(this, Meta(mutableMeta))
} }
public infix fun String.put(meta: Meta) { public infix fun String.put(meta: Meta) {
Name.parse(this) put meta setMeta(Name.parse(this), meta)
} }
public infix fun String.put(value: Value?) { public infix fun String.put(value: Value?) {
set(Name.parse(this), value) setValue(Name.parse(this), value)
} }
public infix fun String.put(string: String) { public infix fun String.put(string: String) {
set(Name.parse(this), string.asValue()) setValue(Name.parse(this), string.asValue())
} }
public infix fun String.put(number: Number) { public infix fun String.put(number: Number) {
set(Name.parse(this), number.asValue()) setValue(Name.parse(this), number.asValue())
} }
public infix fun String.put(boolean: Boolean) { public infix fun String.put(boolean: Boolean) {
set(Name.parse(this), boolean.asValue()) setValue(Name.parse(this), boolean.asValue())
} }
public infix fun String.put(enum: Enum<*>) { public infix fun String.put(enum: Enum<*>) {
set(Name.parse(this), EnumValue(enum)) setValue(Name.parse(this), EnumValue(enum))
} }
public infix fun String.put(array: DoubleArray) { public infix fun String.put(array: DoubleArray) {
set(Name.parse(this), array.asValue()) setValue(Name.parse(this), array.asValue())
} }
public infix fun String.put(repr: MetaRepr) { public infix fun String.put(repr: MetaRepr) {
Name.parse(this) put repr.toMeta() setMeta(Name.parse(this), repr.toMeta())
} }
public infix fun String.put(iterable: Iterable<Meta>) { public infix fun String.putIndexed(iterable: Iterable<Meta>) {
setIndexed(Name.parse(this), iterable) setIndexed(Name.parse(this), iterable)
} }
public infix fun String.put(builder: MutableMeta.() -> Unit) { public infix fun String.put(builder: MutableMeta.() -> Unit) {
set(Name.parse(this), MutableMeta(builder)) setMeta(Name.parse(this), MutableMeta(builder))
} }
} }
/**
* Set or replace node at given [name]
*/
public operator fun MutableMeta.set(name: Name, meta: Meta): Unit = setMeta(name, meta)
/**
* Set or replace value at given [name]
*/
public operator fun MutableMeta.set(name: Name, value: Value?): Unit = setValue(name, value)
public fun MutableMeta.getOrCreate(key: String): MutableMeta = getOrCreate(Name.parse(key)) public fun MutableMeta.getOrCreate(key: String): MutableMeta = getOrCreate(Name.parse(key))
@Serializable(MutableMetaSerializer::class) @Serializable(MutableMetaSerializer::class)
@ -151,8 +162,12 @@ public interface MutableTypedMeta<M : MutableTypedMeta<M>> : TypedMeta<M>, Mutab
public fun <M : MutableTypedMeta<M>> M.getOrCreate(key: String): M = getOrCreate(Name.parse(key)) public fun <M : MutableTypedMeta<M>> M.getOrCreate(key: String): M = getOrCreate(Name.parse(key))
public fun MutableMeta.remove(key: String) { public fun MutableMetaProvider.remove(name: Name) {
remove(Name.parse(key)) setMeta(name, null)
}
public fun MutableMetaProvider.remove(key: String) {
setMeta(Name.parse(key), null)
} }
// node setters // node setters
@ -185,35 +200,21 @@ public operator fun MutableMeta.set(key: String, value: List<Value>): Unit = set
// set(key.toName().withIndex(index), value) // set(key.toName().withIndex(index), value)
/**
* Universal unsafe set method
*/
public operator fun MutableMeta.set(name: Name, value: Value?) {
getOrCreate(name).value = Value.of(value)
}
/* Same name siblings generation */ /* Same name siblings generation */
public fun MutableMeta.setIndexedItems(
name: Name,
items: Iterable<Meta>,
indexFactory: (Meta, index: Int) -> String = { _, index -> index.toString() },
) {
val tokens = name.tokens.toMutableList()
val last = tokens.last()
items.forEachIndexed { index, meta ->
val indexedToken = NameToken(last.body, (last.index ?: "") + indexFactory(meta, index))
tokens[tokens.lastIndex] = indexedToken
set(Name(tokens), meta)
}
}
public fun MutableMeta.setIndexed( public fun MutableMeta.setIndexed(
name: Name, name: Name,
metas: Iterable<Meta>, metas: Iterable<Meta>,
indexFactory: (Meta, index: Int) -> String = { _, index -> index.toString() }, indexFactory: (Meta, index: Int) -> String = { _, index -> index.toString() },
) { ) {
setIndexedItems(name, metas) { item, index -> indexFactory(item, index) } val tokens = name.tokens.toMutableList()
val last = tokens.last()
metas.forEachIndexed { index, meta ->
val indexedToken = NameToken(last.body, (last.index ?: "") + indexFactory(meta, index))
tokens[tokens.lastIndex] = indexedToken
set(Name(tokens), meta)
}
} }
public operator fun MutableMeta.set(name: Name, metas: Iterable<Meta>): Unit = public operator fun MutableMeta.set(name: Name, metas: Iterable<Meta>): Unit =
@ -265,8 +266,20 @@ private class MutableMetaImpl(
children: Map<NameToken, Meta> = emptyMap() children: Map<NameToken, Meta> = emptyMap()
) : ObservableMutableMeta { ) : ObservableMutableMeta {
private val listeners = HashSet<MetaListener>()
private fun changed(name: Name) {
listeners.forEach { it.callback(this, name) }
}
override var value = value override var value = value
@Synchronized set @Synchronized set(value) {
val oldValue = field
field = value
if (oldValue != value) {
changed(Name.EMPTY)
}
}
private val children: LinkedHashMap<NameToken, ObservableMutableMeta> = private val children: LinkedHashMap<NameToken, ObservableMutableMeta> =
LinkedHashMap(children.mapValues { (key, meta) -> LinkedHashMap(children.mapValues { (key, meta) ->
@ -275,11 +288,6 @@ private class MutableMetaImpl(
override val items: Map<NameToken, ObservableMutableMeta> get() = children override val items: Map<NameToken, ObservableMutableMeta> get() = children
private val listeners = HashSet<MetaListener>()
private fun changed(name: Name) {
listeners.forEach { it.callback(this, name) }
}
@Synchronized @Synchronized
override fun onChange(owner: Any?, callback: Meta.(name: Name) -> Unit) { override fun onChange(owner: Any?, callback: Meta.(name: Name) -> Unit) {
@ -322,15 +330,8 @@ private class MutableMetaImpl(
else -> getOrCreate(name.first().asName()).getOrCreate(name.cutFirst()) else -> getOrCreate(name.first().asName()).getOrCreate(name.cutFirst())
} }
override fun getOrCreate(name: Name): ObservableMutableMeta = get(name) ?: createNode(name) override fun getOrCreate(name: Name): ObservableMutableMeta =
if (name.isEmpty()) this else get(name) ?: createNode(name)
override fun remove(name: Name) {
when (name.length) {
0 -> error("Can't remove self")
1 -> if (children.remove(name.firstOrNull()!!) != null) changed(name)
else -> get(name.cutLast())?.remove(name.lastOrNull()!!.asName())
}
}
@Synchronized @Synchronized
private fun replaceItem( private fun replaceItem(
@ -354,14 +355,14 @@ private class MutableMetaImpl(
MutableMetaImpl(meta.value, meta.items.mapValuesTo(LinkedHashMap()) { wrapItem(it.value) }) MutableMetaImpl(meta.value, meta.items.mapValuesTo(LinkedHashMap()) { wrapItem(it.value) })
override fun set(name: Name, meta: Meta) { override fun setMeta(name: Name, node: Meta?) {
val oldItem: ObservableMutableMeta? = get(name) val oldItem: ObservableMutableMeta? = get(name)
if (oldItem != meta) { if (oldItem != node) {
when (name.length) { when (name.length) {
0 -> error("Can't set a meta with empty name") 0 -> error("Can't set a meta with empty name")
1 -> { 1 -> {
val token = name.firstOrNull()!! val token = name.firstOrNull()!!
replaceItem(token, oldItem, wrapItem(meta)) replaceItem(token, oldItem, node?.let { wrapItem(node) })
} }
else -> { else -> {
val token = name.firstOrNull()!! val token = name.firstOrNull()!!
@ -369,7 +370,7 @@ private class MutableMetaImpl(
if (items[token] == null) { if (items[token] == null) {
replaceItem(token, null, MutableMetaImpl(null)) replaceItem(token, null, MutableMetaImpl(null))
} }
items[token]?.set(name.cutFirst(), meta) items[token]?.setMeta(name.cutFirst(), node)
} }
} }
changed(name) changed(name)
@ -421,6 +422,7 @@ public fun Meta.toMutableMeta(): ObservableMutableMeta = MutableMetaImpl(value,
public fun Meta.asMutableMeta(): MutableMeta = (this as? MutableMeta) ?: toMutableMeta() public fun Meta.asMutableMeta(): MutableMeta = (this as? MutableMeta) ?: toMutableMeta()
@Suppress("FunctionName")
@JsName("newMutableMeta") @JsName("newMutableMeta")
public fun MutableMeta(): ObservableMutableMeta = MutableMetaImpl(null) public fun MutableMeta(): ObservableMutableMeta = MutableMetaImpl(null)
@ -441,20 +443,25 @@ public inline fun Meta.copy(block: MutableMeta.() -> Unit = {}): Meta =
private class MutableMetaWithDefault( private class MutableMetaWithDefault(
val source: MutableMeta, val default: Meta, val name: Name val source: MutableMeta, val default: Meta, val rootName: Name
) : MutableMeta by source { ) : MutableMeta by source {
override val items: Map<NameToken, MutableMeta> override val items: Map<NameToken, MutableMeta>
get() = (source.items.keys + default.items.keys).associateWith { get() {
MutableMetaWithDefault(source, default, name + it) val sourceKeys: Collection<NameToken> = source[rootName]?.items?.keys ?: emptyList()
val defaultKeys: Collection<NameToken> = default[rootName]?.items?.keys ?: emptyList()
//merging keys for primary and default node
return (sourceKeys + defaultKeys).associateWith {
MutableMetaWithDefault(source, default, rootName + it)
}
} }
override var value: Value? override var value: Value?
get() = source[name]?.value ?: default[name]?.value get() = source[rootName]?.value ?: default[rootName]?.value
set(value) { set(value) {
source[name] = value source[rootName] = value
} }
override fun getMeta(name: Name): Meta? = source.getMeta(name) ?: default.getMeta(name) override fun getMeta(name: Name): MutableMeta = MutableMetaWithDefault(source, default, rootName + name)
override fun toString(): String = Meta.toString(this) override fun toString(): String = Meta.toString(this)
override fun equals(other: Any?): Boolean = Meta.equals(this, other as? Meta) override fun equals(other: Any?): Boolean = Meta.equals(this, other as? Meta)

View File

@ -9,9 +9,8 @@ import kotlin.reflect.KProperty
/* Read-write delegates */ /* Read-write delegates */
public typealias MutableMetaDelegate = ReadWriteProperty<Any?, Meta?> public fun MutableMetaProvider.node(key: Name? = null): ReadWriteProperty<Any?, Meta?> =
object : ReadWriteProperty<Any?, Meta?> {
public fun MutableMetaProvider.item(key: Name? = null): MutableMetaDelegate = object : MutableMetaDelegate {
override fun getValue(thisRef: Any?, property: KProperty<*>): Meta? { override fun getValue(thisRef: Any?, property: KProperty<*>): Meta? {
return getMeta(key ?: property.name.asName()) return getMeta(key ?: property.name.asName())
} }
@ -22,125 +21,103 @@ public fun MutableMetaProvider.item(key: Name? = null): MutableMetaDelegate = ob
} }
} }
/* Mutable converters */ public fun <T> MutableMetaProvider.node(key: Name? = null, converter: MetaConverter<T>): ReadWriteProperty<Any?, T?> =
object : ReadWriteProperty<Any?, T?> {
override fun getValue(thisRef: Any?, property: KProperty<*>): T? {
return getMeta(key ?: property.name.asName())?.let { converter.metaToObject(it) }
}
/** override fun setValue(thisRef: Any?, property: KProperty<*>, value: T?) {
* A type converter for a [MutableMetaDelegate] val name = key ?: property.name.asName()
*/ setMeta(name, value?.let { converter.objectToMeta(it) })
public fun <R : Any> MutableMetaDelegate.convert(
converter: MetaConverter<R>,
): ReadWriteProperty<Any?, R?> = object : ReadWriteProperty<Any?, R?> {
override fun getValue(thisRef: Any?, property: KProperty<*>): R? =
this@convert.getValue(thisRef, property)?.let(converter::metaToObject)
override fun setValue(thisRef: Any?, property: KProperty<*>, value: R?) {
val item = value?.let(converter::objectToMeta)
this@convert.setValue(thisRef, property, item)
} }
} }
public fun <R : Any> MutableMetaDelegate.convert( public fun MutableMetaProvider.value(key: Name? = null): ReadWriteProperty<Any?, Value?> =
converter: MetaConverter<R>, object : ReadWriteProperty<Any?, Value?> {
default: () -> R, override fun getValue(thisRef: Any?, property: KProperty<*>): Value? =
): ReadWriteProperty<Any?, R> = object : ReadWriteProperty<Any?, R> { getMeta(key ?: property.name.asName())?.value
override fun getValue(thisRef: Any?, property: KProperty<*>): R = override fun setValue(thisRef: Any?, property: KProperty<*>, value: Value?) {
this@convert.getValue(thisRef, property)?.let(converter::metaToObject) ?: default() setValue(key ?: property.name.asName(), value)
override fun setValue(thisRef: Any?, property: KProperty<*>, value: R) {
val item = value.let(converter::objectToMeta)
this@convert.setValue(thisRef, property, item)
} }
} }
public fun <R> MutableMetaDelegate.convert( public fun <T> MutableMetaProvider.value(
reader: (Meta?) -> R, key: Name? = null,
writer: (R) -> Meta?, writer: (T) -> Value? = { Value.of(it) },
): ReadWriteProperty<Any?, R> = object : ReadWriteProperty<Any?, R> { reader: (Value?) -> T
): ReadWriteProperty<Any?, T> = object : ReadWriteProperty<Any?, T> {
override fun getValue(thisRef: Any?, property: KProperty<*>): T =
reader(getMeta(key ?: property.name.asName())?.value)
override fun getValue(thisRef: Any?, property: KProperty<*>): R = override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
this@convert.getValue(thisRef, property).let(reader) setValue(key ?: property.name.asName(), writer(value))
override fun setValue(thisRef: Any?, property: KProperty<*>, value: R) {
val item = value?.let(writer)
this@convert.setValue(thisRef, property, item)
} }
} }
/* Read-write delegates for [MutableItemProvider] */ /* Read-write delegates for [MutableItemProvider] */
/**
* A property delegate that uses custom key
*/
public fun MutableMetaProvider.value(key: Name? = null): ReadWriteProperty<Any?, Value?> =
item(key).convert(MetaConverter.value)
public fun MutableMetaProvider.string(key: Name? = null): ReadWriteProperty<Any?, String?> = public fun MutableMetaProvider.string(key: Name? = null): ReadWriteProperty<Any?, String?> =
item(key).convert(MetaConverter.string) value(key) { it?.string }
public fun MutableMetaProvider.boolean(key: Name? = null): ReadWriteProperty<Any?, Boolean?> = public fun MutableMetaProvider.boolean(key: Name? = null): ReadWriteProperty<Any?, Boolean?> =
item(key).convert(MetaConverter.boolean) value(key) { it?.boolean }
public fun MutableMetaProvider.number(key: Name? = null): ReadWriteProperty<Any?, Number?> = public fun MutableMetaProvider.number(key: Name? = null): ReadWriteProperty<Any?, Number?> =
item(key).convert(MetaConverter.number) value(key) { it?.number }
public fun MutableMetaProvider.string(default: String, key: Name? = null): ReadWriteProperty<Any?, String> = public fun MutableMetaProvider.string(default: String, key: Name? = null): ReadWriteProperty<Any?, String> =
item(key).convert(MetaConverter.string) { default } value(key) { it?.string ?: default }
public fun MutableMetaProvider.boolean(default: Boolean, key: Name? = null): ReadWriteProperty<Any?, Boolean> = public fun MutableMetaProvider.boolean(default: Boolean, key: Name? = null): ReadWriteProperty<Any?, Boolean> =
item(key).convert(MetaConverter.boolean) { default } value(key) { it?.boolean ?: default }
public fun MutableMetaProvider.number(default: Number, key: Name? = null): ReadWriteProperty<Any?, Number> = public fun MutableMetaProvider.number(default: Number, key: Name? = null): ReadWriteProperty<Any?, Number> =
item(key).convert(MetaConverter.number) { default } value(key) { it?.number ?: default }
public fun MutableMetaProvider.value(key: Name? = null, default: () -> Value): ReadWriteProperty<Any?, Value> =
item(key).convert(MetaConverter.value, default)
public fun MutableMetaProvider.string(key: Name? = null, default: () -> String): ReadWriteProperty<Any?, String> = public fun MutableMetaProvider.string(key: Name? = null, default: () -> String): ReadWriteProperty<Any?, String> =
item(key).convert(MetaConverter.string, default) value(key) { it?.string ?: default() }
public fun MutableMetaProvider.boolean(key: Name? = null, default: () -> Boolean): ReadWriteProperty<Any?, Boolean> = public fun MutableMetaProvider.boolean(key: Name? = null, default: () -> Boolean): ReadWriteProperty<Any?, Boolean> =
item(key).convert(MetaConverter.boolean, default) value(key) { it?.boolean ?: default() }
public fun MutableMetaProvider.number(key: Name? = null, default: () -> Number): ReadWriteProperty<Any?, Number> = public fun MutableMetaProvider.number(key: Name? = null, default: () -> Number): ReadWriteProperty<Any?, Number> =
item(key).convert(MetaConverter.number, default) value(key) { it?.number ?: default() }
public inline fun <reified E : Enum<E>> MutableMetaProvider.enum( public inline fun <reified E : Enum<E>> MutableMetaProvider.enum(
default: E, default: E,
key: Name? = null, key: Name? = null,
): ReadWriteProperty<Any?, E> = ): ReadWriteProperty<Any?, E> = value(key) { value -> value?.string?.let { enumValueOf<E>(it) } ?: default }
item(key).convert(MetaConverter.enum()) { default }
/* Number delegates */ /* Number delegates */
public fun MutableMetaProvider.int(key: Name? = null): ReadWriteProperty<Any?, Int?> = public fun MutableMetaProvider.int(key: Name? = null): ReadWriteProperty<Any?, Int?> =
item(key).convert(MetaConverter.int) value(key) { it?.int }
public fun MutableMetaProvider.double(key: Name? = null): ReadWriteProperty<Any?, Double?> = public fun MutableMetaProvider.double(key: Name? = null): ReadWriteProperty<Any?, Double?> =
item(key).convert(MetaConverter.double) value(key) { it?.double }
public fun MutableMetaProvider.long(key: Name? = null): ReadWriteProperty<Any?, Long?> = public fun MutableMetaProvider.long(key: Name? = null): ReadWriteProperty<Any?, Long?> =
item(key).convert(MetaConverter.long) value(key) { it?.long }
public fun MutableMetaProvider.float(key: Name? = null): ReadWriteProperty<Any?, Float?> = public fun MutableMetaProvider.float(key: Name? = null): ReadWriteProperty<Any?, Float?> =
item(key).convert(MetaConverter.float) value(key) { it?.float }
/* Safe number delegates*/ /* Safe number delegates*/
public fun MutableMetaProvider.int(default: Int, key: Name? = null): ReadWriteProperty<Any?, Int> = public fun MutableMetaProvider.int(default: Int, key: Name? = null): ReadWriteProperty<Any?, Int> =
item(key).convert(MetaConverter.int) { default } value(key) { it?.int ?: default }
public fun MutableMetaProvider.double(default: Double, key: Name? = null): ReadWriteProperty<Any?, Double> = public fun MutableMetaProvider.double(default: Double, key: Name? = null): ReadWriteProperty<Any?, Double> =
item(key).convert(MetaConverter.double) { default } value(key) { it?.double ?: default }
public fun MutableMetaProvider.long(default: Long, key: Name? = null): ReadWriteProperty<Any?, Long> = public fun MutableMetaProvider.long(default: Long, key: Name? = null): ReadWriteProperty<Any?, Long> =
item(key).convert(MetaConverter.long) { default } value(key) { it?.long ?: default }
public fun MutableMetaProvider.float(default: Float, key: Name? = null): ReadWriteProperty<Any?, Float> = public fun MutableMetaProvider.float(default: Float, key: Name? = null): ReadWriteProperty<Any?, Float> =
item(key).convert(MetaConverter.float) { default } value(key) { it?.float ?: default }
/* Extra delegates for special cases */ /* Extra delegates for special cases */
@ -148,24 +125,27 @@ public fun MutableMetaProvider.float(default: Float, key: Name? = null): ReadWri
public fun MutableMetaProvider.stringList( public fun MutableMetaProvider.stringList(
vararg default: String, vararg default: String,
key: Name? = null, key: Name? = null,
): ReadWriteProperty<Any?, List<String>> = item(key).convert( ): ReadWriteProperty<Any?, List<String>> = value(
key,
writer = { list -> list.map { str -> str.asValue() }.asValue() },
reader = { it?.stringList ?: listOf(*default) }, reader = { it?.stringList ?: listOf(*default) },
writer = { Meta(it.map { str -> str.asValue() }.asValue()) }
) )
public fun MutableMetaProvider.stringList( public fun MutableMetaProvider.stringList(
key: Name? = null, key: Name? = null,
): ReadWriteProperty<Any?, List<String>?> = item(key).convert( ): ReadWriteProperty<Any?, List<String>?> = value(
key,
writer = { it -> it?.map { str -> str.asValue() }?.asValue() },
reader = { it?.stringList }, reader = { it?.stringList },
writer = { it?.map { str -> str.asValue() }?.asValue()?.let { Meta(it) } }
) )
public fun MutableMetaProvider.numberList( public fun MutableMetaProvider.numberList(
vararg default: Number, vararg default: Number,
key: Name? = null, key: Name? = null,
): ReadWriteProperty<Any?, List<Number>> = item(key).convert( ): ReadWriteProperty<Any?, List<Number>> = value(
reader = { it?.value?.list?.map { value -> value.numberOrNull ?: Double.NaN } ?: listOf(*default) }, key,
writer = { Meta(it.map { num -> num.asValue() }.asValue()) } writer = { it.map { num -> num.asValue() }.asValue() },
reader = { it?.list?.map { value -> value.numberOrNull ?: Double.NaN } ?: listOf(*default) },
) )
/* A special delegate for double arrays */ /* A special delegate for double arrays */
@ -174,13 +154,18 @@ public fun MutableMetaProvider.numberList(
public fun MutableMetaProvider.doubleArray( public fun MutableMetaProvider.doubleArray(
vararg default: Double, vararg default: Double,
key: Name? = null, key: Name? = null,
): ReadWriteProperty<Any?, DoubleArray> = item(key).convert( ): ReadWriteProperty<Any?, DoubleArray> = value(
reader = { it?.value?.doubleArray ?: doubleArrayOf(*default) }, key,
writer = { Meta(DoubleArrayValue(it)) } writer = { DoubleArrayValue(it) },
reader = { it?.doubleArray ?: doubleArrayOf(*default) },
) )
public fun <T> MutableMetaProvider.listValue( public fun <T> MutableMetaProvider.listValue(
key: Name? = null, key: Name? = null,
writer: (T) -> Value = { Value.of(it) }, writer: (T) -> Value = { Value.of(it) },
reader: (Value) -> T, reader: (Value) -> T,
): ReadWriteProperty<Any?, List<T>?> = item(key).convert(MetaConverter.valueList(writer, reader)) ): ReadWriteProperty<Any?, List<T>?> = value(
key,
writer = { it?.map(writer)?.asValue() },
reader = { it?.list?.map(reader) }
)

View File

@ -54,7 +54,7 @@ private class ObservableMetaWrapper(
override val items: Map<NameToken, ObservableMetaWrapper> override val items: Map<NameToken, ObservableMetaWrapper>
get() = origin.items.mapValues { ObservableMetaWrapper(it.value) } get() = origin.items.mapValues { ObservableMetaWrapper(it.value) }
override fun getMeta(name: Name): Meta? = origin.getMeta(name) override fun getMeta(name: Name): MutableMeta? = origin.getMeta(name)
override var value: Value? override var value: Value?
get() = origin.value get() = origin.value
@ -66,22 +66,18 @@ private class ObservableMetaWrapper(
override fun getOrCreate(name: Name): ObservableMutableMeta = override fun getOrCreate(name: Name): ObservableMutableMeta =
get(name) ?: ObservableMetaWrapper(origin.getOrCreate(name)) get(name) ?: ObservableMetaWrapper(origin.getOrCreate(name))
override fun setMeta(name: Name, node: Meta?) {
override fun remove(name: Name) {
origin.remove(name)
changed(name)
}
override fun set(name: Name, meta: Meta) {
val oldMeta = get(name) val oldMeta = get(name)
origin[name] = meta origin.setMeta(name, node)
//
// if meta is observable propagate changes from it // if meta is observable propagate changes from it
if(meta is ObservableMeta){ if(node is ObservableMeta){
meta.onChange(this) { changeName ->
setMeta(name + changeName, meta[changeName]) node.onChange(this) { changeName ->
setMeta(name + changeName, node[changeName])
} }
} }
if (oldMeta != meta) { if (oldMeta != node) {
changed(name) changed(name)
} }
} }

View File

@ -2,6 +2,7 @@ package space.kscience.dataforge.meta
import space.kscience.dataforge.meta.descriptors.* import space.kscience.dataforge.meta.descriptors.*
import space.kscience.dataforge.names.Name import space.kscience.dataforge.names.Name
import space.kscience.dataforge.values.Value
/** /**
* A base for delegate-based or descriptor-based scheme. [Scheme] has an empty constructor to simplify usage from [Specification]. * A base for delegate-based or descriptor-based scheme. [Scheme] has an empty constructor to simplify usage from [Specification].
@ -30,7 +31,7 @@ public open class Scheme : Described, MetaRepr, MutableMetaProvider, Configurabl
return descriptor?.validate(meta) ?: true return descriptor?.validate(meta) ?: true
} }
override fun getMeta(name: Name): Meta? = meta.getMeta(name) override fun getMeta(name: Name): MutableMeta? = meta.getMeta(name)
override fun setMeta(name: Name, node: Meta?) { override fun setMeta(name: Name, node: Meta?) {
if (validate(name, meta)) { if (validate(name, meta)) {
@ -40,6 +41,11 @@ public open class Scheme : Described, MetaRepr, MutableMetaProvider, Configurabl
} }
} }
override fun setValue(name: Name, value: Value?) {
//TODO add validation
meta.setValue(name,value)
}
override fun toMeta(): Laminate = Laminate(meta, descriptor?.defaultNode) override fun toMeta(): Laminate = Laminate(meta, descriptor?.defaultNode)
} }

View File

@ -22,9 +22,12 @@ public class SealedMeta internal constructor(
/** /**
* Generate sealed node from [this]. If it is already sealed return it as is * Generate sealed node from [this]. If it is already sealed return it as is
*/ */
public fun Meta.seal(): SealedMeta = this as? SealedMeta ?: SealedMeta(value, items.mapValues { entry -> public fun Meta.seal(): SealedMeta = this as? SealedMeta ?: SealedMeta(
value,
items.mapValues { entry ->
entry.value.seal() entry.value.seal()
}) }
)
@Suppress("FunctionName") @Suppress("FunctionName")
public fun Meta(value: Value): SealedMeta = SealedMeta(value, emptyMap()) public fun Meta(value: Value): SealedMeta = SealedMeta(value, emptyMap())

View File

@ -31,7 +31,7 @@ public fun Meta.toMap(descriptor: MetaDescriptor? = null): Map<String, Any?> = b
* All other values will be converted to [Value]. * All other values will be converted to [Value].
*/ */
@DFExperimental @DFExperimental
public fun Map<String, Any?>.toMeta(descriptor: MetaDescriptor? = null): Meta = Meta { public fun Map<String, Any?>.toMeta(@Suppress("UNUSED_PARAMETER") descriptor: MetaDescriptor? = null): Meta = Meta {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
fun toMeta(value: Any?): Meta = when (value) { fun toMeta(value: Any?): Meta = when (value) {
is Meta -> value is Meta -> value
@ -45,7 +45,7 @@ public fun Map<String, Any?>.toMeta(descriptor: MetaDescriptor? = null): Meta =
if (items.all { it.isLeaf }) { if (items.all { it.isLeaf }) {
set(key, ListValue(items.map { it.value!! })) set(key, ListValue(items.map { it.value!! }))
} else { } else {
setIndexedItems(Name.parse(key), value.map { toMeta(it) }) setIndexed(Name.parse(key), value.map { toMeta(it) })
} }
} else { } else {
set(key, toMeta(value)) set(key, toMeta(value))

View File

@ -24,6 +24,13 @@ public val Value.float: Float get() = number.toFloat()
public val Value.short: Short get() = number.toShort() public val Value.short: Short get() = number.toShort()
public val Value.long: Long get() = number.toLong() public val Value.long: Long get() = number.toLong()
public inline fun <reified E : Enum<E>> Value.enum(): E = if (this is EnumValue<*>) {
value as E
} else {
enumValueOf<E>(string)
}
public val Value.stringList: List<String> get() = list.map { it.string } public val Value.stringList: List<String> get() = list.map { it.string }
public val Value.doubleArray: DoubleArray public val Value.doubleArray: DoubleArray

View File

@ -1,6 +1,5 @@
package space.kscience.dataforge.meta package space.kscience.dataforge.meta
import space.kscience.dataforge.values.asValue
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
@ -30,7 +29,7 @@ class MetaDelegateTest {
fun delegateTest() { fun delegateTest() {
val testObject = TestScheme.empty() val testObject = TestScheme.empty()
testObject.meta["myValue"] = "theString".asValue() testObject.meta["myValue"] = "theString"
testObject.enumValue = TestEnum.NO testObject.enumValue = TestEnum.NO
testObject.inner = InnerScheme { innerValue = "ddd" } testObject.inner = InnerScheme { innerValue = "ddd" }

View File

@ -40,7 +40,7 @@ class MetaTest {
"b" put { "b" put {
"c" put "ddd" "c" put "ddd"
} }
"list" put (0..4).map { "list" putIndexed (0..4).map {
Meta { Meta {
"value" put it "value" put it
} }