fix withDefault

This commit is contained in:
Alexander Nozik 2022-08-11 17:59:31 +03:00
parent 3c1fe23366
commit e14c0a695e
No known key found for this signature in database
GPG Key ID: F7FCF2DD25C71357
3 changed files with 8 additions and 7 deletions

View File

@ -8,6 +8,7 @@
- More fine-grained types in Action builders. - More fine-grained types in Action builders.
### Changed ### 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 - `dataforge.meta.values` package is merged into `dataforge.meta` for better star imports
- Kotlin 1.7.20 - Kotlin 1.7.20
- `Factory` is now `fun interface` and uses `build` instead of `invoke`. `invoke moved to an extension. - `Factory` is now `fun interface` and uses `build` instead of `invoke`. `invoke moved to an extension.

View File

@ -248,8 +248,9 @@ public val Meta.stringList: List<String>? get() = value?.list?.map { it.string }
/** /**
* Create a provider that uses given provider for default values if those are not found in this provider * 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 this
} else { } else {
Laminate(this, default) //TODO optimize
toMutableMeta().withDefault(default)
} }

View File

@ -401,12 +401,12 @@ public inline fun Meta.copy(block: MutableMeta.() -> Unit = {}): Meta =
private class MutableMetaWithDefault( private class MutableMetaWithDefault(
val source: MutableMeta, val default: Meta, val rootName: Name val source: MutableMeta, val default: MetaProvider, val rootName: Name
) : MutableMeta by source { ) : MutableMeta by source {
override val items: Map<NameToken, MutableMeta> override val items: Map<NameToken, MutableMeta>
get() { get() {
val sourceKeys: Collection<NameToken> = source[rootName]?.items?.keys ?: emptyList() val sourceKeys: Collection<NameToken> = source[rootName]?.items?.keys ?: emptyList()
val defaultKeys: Collection<NameToken> = default[rootName]?.items?.keys ?: emptyList() val defaultKeys: Collection<NameToken> = default.getMeta(rootName)?.items?.keys ?: emptyList()
//merging keys for primary and default node //merging keys for primary and default node
return (sourceKeys + defaultKeys).associateWith { return (sourceKeys + defaultKeys).associateWith {
MutableMetaWithDefault(source, default, rootName + it) MutableMetaWithDefault(source, default, rootName + it)
@ -414,7 +414,7 @@ private class MutableMetaWithDefault(
} }
override var value: Value? override var value: Value?
get() = source[rootName]?.value ?: default[rootName]?.value get() = source[rootName]?.value ?: default.getMeta(rootName)?.value
set(value) { set(value) {
source[rootName] = 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. * 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. * Changes are propagated only to this provider.
*/ */
public fun MutableMeta.withDefault(default: Meta?): MutableMeta = if (default == null || default.isEmpty()) { public fun MutableMeta.withDefault(default: MetaProvider?): MutableMeta = if (default == null) {
//Optimize for use with empty default
this this
} else MutableMetaWithDefault(this, default, Name.EMPTY) } else MutableMetaWithDefault(this, default, Name.EMPTY)