Remove needless item delegate

This commit is contained in:
Alexander Nozik 2020-07-26 17:34:35 +03:00
parent 4fde6c4a48
commit b72f73f75b
2 changed files with 33 additions and 35 deletions

View File

@ -77,6 +77,9 @@ sealed class MetaItem<out M : Meta> {
} }
} }
fun Value.asMetaItem() = ValueItem(this)
fun <M:Meta> M.asMetaItem() = NodeItem(this)
/** /**
* The object that could be represented as [Meta]. Meta provided by [toMeta] method should fully represent object state. * The object that could be represented as [Meta]. Meta provided by [toMeta] method should fully represent object state.
* Meaning that two states with the same meta are equal. * Meaning that two states with the same meta are equal.
@ -265,7 +268,7 @@ inline fun <reified E : Enum<E>> MetaItem<*>?.enum(): E? = if (this is ValueItem
string?.let { enumValueOf<E>(it) } string?.let { enumValueOf<E>(it) }
} }
val MetaItem<*>?.stringList get() = value?.list?.map { it.string } ?: emptyList() val MetaItem<*>.stringList get() = value?.list?.map { it.string }
val <M : Meta> MetaItem<M>?.node: M? val <M : Meta> MetaItem<M>?.node: M?
get() = when (this) { get() = when (this) {

View File

@ -5,7 +5,8 @@ import hep.dataforge.names.Name
import hep.dataforge.names.asName import hep.dataforge.names.asName
import hep.dataforge.values.DoubleArrayValue import hep.dataforge.values.DoubleArrayValue
import hep.dataforge.values.Value import hep.dataforge.values.Value
import hep.dataforge.values.stringList import hep.dataforge.values.asValue
import hep.dataforge.values.doubleArray
import kotlin.properties.ReadWriteProperty import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty import kotlin.reflect.KProperty
@ -71,18 +72,6 @@ inline fun <reified M : MutableMeta<M>> M.node(key: Name? = null): ReadWriteProp
item(key).convert(reader = { it?.let { it.node as M } }, writer = { it?.let { MetaItem.NodeItem(it) } }) item(key).convert(reader = { it?.let { it.node as M } }, writer = { it?.let { MetaItem.NodeItem(it) } })
@Deprecated("To be replaced by a converter")
fun <T> MutableItemProvider.item(
default: T? = null,
key: Name? = null,
writer: (T) -> MetaItem<*>? = { MetaItem.of(it) },
reader: (MetaItem<*>?) -> T
): ReadWriteProperty<Any?, T> = MutableItemDelegate(
this,
key,
default?.let { MetaItem.of(it) }
).convert(reader = reader, writer = writer)
fun Configurable.value(key: Name? = null): ReadWriteProperty<Any?, Value?> = fun Configurable.value(key: Name? = null): ReadWriteProperty<Any?, Value?> =
item(key).convert(MetaConverter.value) item(key).convert(MetaConverter.value)
@ -119,33 +108,39 @@ fun MutableItemProvider.float(default: Float, key: Name? = null): ReadWritePrope
/* /*
* Extra delegates for special cases * Extra delegates for special cases
*/ */
fun MutableItemProvider.stringList(vararg strings: String, key: Name? = null): ReadWriteProperty<Any?, List<String>> = fun MutableItemProvider.stringList(
item(listOf(*strings), key) { vararg default: String,
it?.value?.stringList ?: emptyList()
}
fun MutableItemProvider.stringListOrNull(
vararg strings: String,
key: Name? = null key: Name? = null
): ReadWriteProperty<Any?, List<String>?> = ): ReadWriteProperty<Any?, List<String>> = item(key).convert(
item(listOf(*strings), key) { reader = { it?.stringList ?: listOf(*default) },
it?.value?.stringList writer = { it.map { str -> str.asValue() }.asValue().asMetaItem() }
} )
fun MutableItemProvider.numberList(vararg numbers: Number, key: Name? = null): ReadWriteProperty<Any?, List<Number>> = fun MutableItemProvider.stringList(
item(listOf(*numbers), key) { item -> key: Name? = null
item?.value?.list?.map { it.number } ?: emptyList() ): ReadWriteProperty<Any?, List<String>?> = item(key).convert(
} reader = { it?.stringList },
writer = { it?.map { str -> str.asValue() }?.asValue()?.asMetaItem() }
)
fun MutableItemProvider.numberList(
vararg default: Number,
key: Name? = null
): ReadWriteProperty<Any?, List<Number>> = item(key).convert(
reader = { it?.value?.list?.map { value -> value.number } ?: listOf(*default) },
writer = { it.map { num -> num.asValue() }.asValue().asMetaItem() }
)
/** /**
* A special delegate for double arrays * A special delegate for double arrays
*/ */
fun MutableItemProvider.doubleArray(vararg doubles: Double, key: Name? = null): ReadWriteProperty<Any?, DoubleArray> = fun MutableItemProvider.doubleArray(
item(doubleArrayOf(*doubles), key) { vararg default: Double,
(it.value as? DoubleArrayValue)?.value key: Name? = null
?: it?.value?.list?.map { value -> value.number.toDouble() }?.toDoubleArray() ): ReadWriteProperty<Any?, DoubleArray> =item(key).convert(
?: doubleArrayOf() reader = { it?.value?.doubleArray ?: doubleArrayOf(*default) },
} writer = { DoubleArrayValue(it).asMetaItem() }
)
fun <T> MutableItemProvider.listValue( fun <T> MutableItemProvider.listValue(
key: Name? = null, key: Name? = null,