From e14c0a695ef5a74e4740341cb8a0d27995694db0 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Thu, 11 Aug 2022 17:59:31 +0300 Subject: [PATCH] fix `withDefault` --- CHANGELOG.md | 1 + .../kotlin/space/kscience/dataforge/meta/Meta.kt | 5 +++-- .../kotlin/space/kscience/dataforge/meta/MutableMeta.kt | 9 ++++----- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 904cf28c..9528044d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - More fine-grained types in Action builders. ### Changed +- `withDefault` functions do not add new keys to meta children and are consistent. - `dataforge.meta.values` package is merged into `dataforge.meta` for better star imports - Kotlin 1.7.20 - `Factory` is now `fun interface` and uses `build` instead of `invoke`. `invoke moved to an extension. diff --git a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/Meta.kt b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/Meta.kt index 5cfdded1..0f39942d 100644 --- a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/Meta.kt +++ b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/Meta.kt @@ -248,8 +248,9 @@ public val Meta.stringList: List? get() = value?.list?.map { it.string } /** * Create a provider that uses given provider for default values if those are not found in this provider */ -public fun Meta.withDefault(default: Meta?): Meta = if (default == null) { +public fun Meta.withDefault(default: MetaProvider?): Meta = if (default == null) { this } else { - Laminate(this, default) + //TODO optimize + toMutableMeta().withDefault(default) } \ No newline at end of file diff --git a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MutableMeta.kt b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MutableMeta.kt index 249fad43..ffd1d6e1 100644 --- a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MutableMeta.kt +++ b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MutableMeta.kt @@ -401,12 +401,12 @@ public inline fun Meta.copy(block: MutableMeta.() -> Unit = {}): Meta = private class MutableMetaWithDefault( - val source: MutableMeta, val default: Meta, val rootName: Name + val source: MutableMeta, val default: MetaProvider, val rootName: Name ) : MutableMeta by source { override val items: Map get() { val sourceKeys: Collection = source[rootName]?.items?.keys ?: emptyList() - val defaultKeys: Collection = default[rootName]?.items?.keys ?: emptyList() + val defaultKeys: Collection = default.getMeta(rootName)?.items?.keys ?: emptyList() //merging keys for primary and default node return (sourceKeys + defaultKeys).associateWith { MutableMetaWithDefault(source, default, rootName + it) @@ -414,7 +414,7 @@ private class MutableMetaWithDefault( } override var value: Value? - get() = source[rootName]?.value ?: default[rootName]?.value + get() = source[rootName]?.value ?: default.getMeta(rootName)?.value set(value) { source[rootName] = value } @@ -430,7 +430,6 @@ private class MutableMetaWithDefault( * Create a mutable item provider that uses given provider for default values if those are not found in this provider. * Changes are propagated only to this provider. */ -public fun MutableMeta.withDefault(default: Meta?): MutableMeta = if (default == null || default.isEmpty()) { - //Optimize for use with empty default +public fun MutableMeta.withDefault(default: MetaProvider?): MutableMeta = if (default == null) { this } else MutableMetaWithDefault(this, default, Name.EMPTY)