From bb9afb329fbb9dcea3859a07f3a4718675d7c454 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Wed, 5 Jun 2024 10:53:35 +0300 Subject: [PATCH] MetaProvider `spec` is replaced by `readable`. `listOfSpec` replaced with `listOfReadable` --- CHANGELOG.md | 1 + build.gradle.kts | 2 +- .../kscience/dataforge/meta/MetaDelegate.kt | 68 +++++++++++++++---- .../dataforge/meta/MutableMetaDelegate.kt | 31 +++++++-- 4 files changed, 84 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f59fbae..788a7374 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ ### Changed ### Deprecated +- MetaProvider `spec` is replaced by `readable`. `listOfSpec` replaced with `listOfReadable` ### Removed diff --git a/build.gradle.kts b/build.gradle.kts index 72f61abc..9f9632de 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -9,7 +9,7 @@ plugins { allprojects { group = "space.kscience" - version = "0.9.0" + version = "0.9.1-dev-1" } subprojects { diff --git a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MetaDelegate.kt b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MetaDelegate.kt index 1b506b44..2fa0f528 100644 --- a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MetaDelegate.kt +++ b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MetaDelegate.kt @@ -24,20 +24,45 @@ public fun MetaProvider.node( } } + /** - * Use [metaReader] to read the Meta node + * Use [reader] to read the Meta node */ -public fun MetaProvider.spec( - metaReader: MetaReader, +public fun MetaProvider.readable( + reader: MetaReader, key: Name? = null, ): MetaDelegate = object : MetaDelegate { - override val descriptor: MetaDescriptor? get() = metaReader.descriptor + override val descriptor: MetaDescriptor? get() = reader.descriptor override fun getValue(thisRef: Any?, property: KProperty<*>): T? { - return get(key ?: property.name.asName())?.let { metaReader.read(it) } + return get(key ?: property.name.asName())?.let { reader.read(it) } } } +/** + * Use [reader] to read the Meta node or return [default] if node does not exist + */ +public fun MetaProvider.readable( + reader: MetaReader, + default: T, + key: Name? = null, +): MetaDelegate = object : MetaDelegate { + override val descriptor: MetaDescriptor? get() = reader.descriptor + + override fun getValue(thisRef: Any?, property: KProperty<*>): T { + return get(key ?: property.name.asName())?.let { reader.read(it) } ?: default + } +} + +/** + * Use [reader] to read the Meta node + */ +@Deprecated("Replace with reading", ReplaceWith("reading(metaReader, key)")) +public fun MetaProvider.spec( + reader: MetaReader, + key: Name? = null, +): MetaDelegate = readable(reader, key) + /** * Use object serializer to transform it to Meta and back */ @@ -45,34 +70,51 @@ public fun MetaProvider.spec( public inline fun MetaProvider.serializable( key: Name? = null, descriptor: MetaDescriptor? = null, -): MetaDelegate = spec(MetaConverter.serializable(descriptor), key) +): MetaDelegate = readable(MetaConverter.serializable(descriptor), key) + +@DFExperimental +public inline fun MetaProvider.serializable( + key: Name? = null, + default: T, + descriptor: MetaDescriptor? = null, +): MetaDelegate = readable(MetaConverter.serializable(descriptor), default, key) @Deprecated("Use convertable", ReplaceWith("convertable(converter, key)")) public fun MetaProvider.node( key: Name? = null, converter: MetaReader, -): ReadOnlyProperty = spec(converter, key) +): ReadOnlyProperty = readable(converter, key) /** - * Use [converter] to convert a list of same name siblings meta to object + * Use [reader] to convert a list of same name siblings meta to object */ -public fun Meta.listOfSpec( - converter: MetaReader, +public fun Meta.listOfReadable( + reader: MetaReader, key: Name? = null, ): MetaDelegate> = object : MetaDelegate> { override fun getValue(thisRef: Any?, property: KProperty<*>): List { val name = key ?: property.name.asName() - return getIndexed(name).values.map { converter.read(it) } + return getIndexed(name).values.map { reader.read(it) } } - override val descriptor: MetaDescriptor? = converter.descriptor?.copy(multiple = true) + override val descriptor: MetaDescriptor? = reader.descriptor?.copy(multiple = true) } + +/** + * Use [converter] to convert a list of same name siblings meta to object + */ +@Deprecated("Replace with readingList", ReplaceWith("readingList(converter, key)")) +public fun Meta.listOfSpec( + converter: MetaReader, + key: Name? = null, +): MetaDelegate> = listOfReadable(converter, key) + @DFExperimental public inline fun Meta.listOfSerializable( key: Name? = null, descriptor: MetaDescriptor? = null, -): MetaDelegate> = listOfSpec(MetaConverter.serializable(descriptor), key) +): MetaDelegate> = listOfReadable(MetaConverter.serializable(descriptor), key) /** * A property delegate that uses custom key diff --git a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MutableMetaDelegate.kt b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MutableMetaDelegate.kt index 37140c6f..28a550d8 100644 --- a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MutableMetaDelegate.kt +++ b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MutableMetaDelegate.kt @@ -54,9 +54,25 @@ public fun MutableMetaProvider.convertable( } } -@Deprecated("Use convertable", ReplaceWith("convertable(converter, key)")) -public fun MutableMetaProvider.node(key: Name? = null, converter: MetaConverter): MutableMetaDelegate = - convertable(converter, key) +public fun MutableMetaProvider.convertable( + converter: MetaConverter, + default: T, + key: Name? = null, +): MutableMetaDelegate = object : MutableMetaDelegate { + + override val descriptor: MetaDescriptor? get() = converter.descriptor + + + override fun getValue(thisRef: Any?, property: KProperty<*>): T { + val name = key ?: property.name.asName() + return get(name)?.let { converter.read(it) } ?: default + } + + override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) { + val name = key ?: property.name.asName() + set(name, value?.let { converter.convert(it) }) + } +} /** * Use object serializer to transform it to Meta and back. @@ -66,7 +82,14 @@ public fun MutableMetaProvider.node(key: Name? = null, converter: MetaConver public inline fun MutableMetaProvider.serializable( descriptor: MetaDescriptor? = null, key: Name? = null, -): MutableMetaDelegate = convertable(MetaConverter.serializable(descriptor), key) +): MutableMetaDelegate = convertable(MetaConverter.serializable(descriptor), key) + +@DFExperimental +public inline fun MutableMetaProvider.serializable( + descriptor: MetaDescriptor? = null, + default: T, + key: Name? = null, +): MutableMetaDelegate = convertable(MetaConverter.serializable(descriptor), default, key) /** * Use [converter] to convert a list of same name siblings meta to object and back.