diff --git a/CHANGELOG.md b/CHANGELOG.md index fc102680..40e80dd5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - Yaml meta format based on yaml.kt - `Path` builders - Special ValueType for lists +- `copy` method to descriptors ### Changed - `ListValue` and `DoubleArrayValue` implement `Iterable`. diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Config.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Config.kt index d7fc3fe0..3fb557bd 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Config.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Config.kt @@ -94,7 +94,11 @@ public class Config() : AbstractMutableMeta(), ObservableItemProvider { public operator fun Config.get(token: NameToken): MetaItem? = items[token] -public fun Meta.asConfig(): Config = this as? Config ?: Config().also { builder -> +/** + * Create a mutable copy of this [Meta]. The copy is created event if initial [Meta] is a [Config]. + * Listeners are not preserved + */ +public fun Meta.toConfig(): Config = Config().also { builder -> this.items.mapValues { entry -> val item = entry.value builder[entry.key.asName()] = when (item) { @@ -102,4 +106,9 @@ public fun Meta.asConfig(): Config = this as? Config ?: Config().also { builder is MetaItem.NodeItem -> MetaItem.NodeItem(item.node.asConfig()) } } -} \ No newline at end of file +} + +/** + * Return this [Meta] as [Config] if it is [Config] and create a new copy otherwise + */ +public fun Meta.asConfig(): Config = this as? Config ?: toConfig() \ No newline at end of file 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 91ad461f..ac1c4693 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 @@ -38,6 +38,8 @@ public sealed class ItemDescriptor(public val config: Config) { */ public var indexKey: String by config.string(DEFAULT_INDEX_KEY) + public abstract fun copy(): ItemDescriptor + public companion object{ public const val DEFAULT_INDEX_KEY: String = "@index" } @@ -180,6 +182,8 @@ public class NodeDescriptor(config: Config = Config()) : ItemDescriptor(config) value(name.toName(), block) } + override fun copy(): NodeDescriptor = NodeDescriptor(config.toConfig()) + public companion object { internal val ITEM_KEY: Name = "item".asName() @@ -281,6 +285,8 @@ public class ValueDescriptor(config: Config = Config()) : ItemDescriptor(config) public fun allow(vararg v: Any) { this.allowedValues = v.map { Value.of(it) } } + + override fun copy(): ValueDescriptor = ValueDescriptor(config.toConfig()) } /**