diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Meta.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Meta.kt index a89d79c9..d01e9786 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Meta.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Meta.kt @@ -77,6 +77,9 @@ sealed class MetaItem { } } +fun Value.asMetaItem() = ValueItem(this) +fun M.asMetaItem() = NodeItem(this) + /** * 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. @@ -265,7 +268,7 @@ inline fun > MetaItem<*>?.enum(): E? = if (this is ValueItem string?.let { enumValueOf(it) } } -val MetaItem<*>?.stringList get() = value?.list?.map { it.string } ?: emptyList() +val MetaItem<*>.stringList get() = value?.list?.map { it.string } val MetaItem?.node: M? get() = when (this) { diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/MutableItemDelegate.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/MutableItemDelegate.kt index 163408e1..189b9e0f 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/MutableItemDelegate.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/MutableItemDelegate.kt @@ -5,7 +5,8 @@ import hep.dataforge.names.Name import hep.dataforge.names.asName import hep.dataforge.values.DoubleArrayValue 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.reflect.KProperty @@ -71,18 +72,6 @@ inline fun > M.node(key: Name? = null): ReadWriteProp item(key).convert(reader = { it?.let { it.node as M } }, writer = { it?.let { MetaItem.NodeItem(it) } }) -@Deprecated("To be replaced by a converter") -fun MutableItemProvider.item( - default: T? = null, - key: Name? = null, - writer: (T) -> MetaItem<*>? = { MetaItem.of(it) }, - reader: (MetaItem<*>?) -> T -): ReadWriteProperty = MutableItemDelegate( - this, - key, - default?.let { MetaItem.of(it) } -).convert(reader = reader, writer = writer) - fun Configurable.value(key: Name? = null): ReadWriteProperty = item(key).convert(MetaConverter.value) @@ -119,33 +108,39 @@ fun MutableItemProvider.float(default: Float, key: Name? = null): ReadWritePrope /* * Extra delegates for special cases */ -fun MutableItemProvider.stringList(vararg strings: String, key: Name? = null): ReadWriteProperty> = - item(listOf(*strings), key) { - it?.value?.stringList ?: emptyList() - } - -fun MutableItemProvider.stringListOrNull( - vararg strings: String, +fun MutableItemProvider.stringList( + vararg default: String, key: Name? = null -): ReadWriteProperty?> = - item(listOf(*strings), key) { - it?.value?.stringList - } +): ReadWriteProperty> = item(key).convert( + reader = { it?.stringList ?: listOf(*default) }, + writer = { it.map { str -> str.asValue() }.asValue().asMetaItem() } +) -fun MutableItemProvider.numberList(vararg numbers: Number, key: Name? = null): ReadWriteProperty> = - item(listOf(*numbers), key) { item -> - item?.value?.list?.map { it.number } ?: emptyList() - } +fun MutableItemProvider.stringList( + key: Name? = null +): ReadWriteProperty?> = item(key).convert( + reader = { it?.stringList }, + writer = { it?.map { str -> str.asValue() }?.asValue()?.asMetaItem() } +) + +fun MutableItemProvider.numberList( + vararg default: Number, + key: Name? = null +): ReadWriteProperty> = 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 */ -fun MutableItemProvider.doubleArray(vararg doubles: Double, key: Name? = null): ReadWriteProperty = - item(doubleArrayOf(*doubles), key) { - (it.value as? DoubleArrayValue)?.value - ?: it?.value?.list?.map { value -> value.number.toDouble() }?.toDoubleArray() - ?: doubleArrayOf() - } +fun MutableItemProvider.doubleArray( + vararg default: Double, + key: Name? = null +): ReadWriteProperty =item(key).convert( + reader = { it?.value?.doubleArray ?: doubleArrayOf(*default) }, + writer = { DoubleArrayValue(it).asMetaItem() } +) fun MutableItemProvider.listValue( key: Name? = null,