From 261c415d3dee09d2628e00ff6e408eba0cac8a41 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sun, 29 Oct 2023 21:39:40 +0300 Subject: [PATCH] Meta converters return non-nullables --- CHANGELOG.md | 1 + build.gradle.kts | 2 +- .../meta/transformations/MetaConverter.kt | 39 ++++++++++--------- gradle.properties | 2 +- 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 20930f82..5a451615 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Added separate `Meta`, `SealedMeta` and `ObservableMutableMeta` builders. ### Changed +- Meta converter `metaToObject` returns a non-nullable type. Additional method `metaToObjectOrNull` for nullable return. - Kotlin 1.9.20. - Migrated from ktor-io to kotlinx-io. - `MutableMeta` builder now returns a simplified version of meta that does not hold listeners. diff --git a/build.gradle.kts b/build.gradle.kts index 1005929b..11231531 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -8,7 +8,7 @@ plugins { allprojects { group = "space.kscience" - version = "0.6.3-dev-1" + version = "0.7.0-dev-1" } subprojects { diff --git a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/transformations/MetaConverter.kt b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/transformations/MetaConverter.kt index 2ecc3393..2d66daf2 100644 --- a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/transformations/MetaConverter.kt +++ b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/transformations/MetaConverter.kt @@ -6,74 +6,77 @@ import space.kscience.dataforge.meta.* * A converter of generic object to and from [Meta] */ public interface MetaConverter { - public fun metaToObject(meta: Meta): T? + + /** + * Attempt conversion of [meta] to an object or return null if conversion failed + */ + public fun metaToObjectOrNull(meta: Meta): T? + + public fun metaToObject(meta: Meta): T = + metaToObjectOrNull(meta) ?: error("Meta $meta could not be interpreted by $this") + public fun objectToMeta(obj: T): Meta public companion object { public val meta: MetaConverter = object : MetaConverter { - override fun metaToObject(meta: Meta): Meta = meta + override fun metaToObjectOrNull(meta: Meta): Meta = meta override fun objectToMeta(obj: Meta): Meta = obj } public val value: MetaConverter = object : MetaConverter { - override fun metaToObject(meta: Meta): Value? = meta.value + override fun metaToObjectOrNull(meta: Meta): Value? = meta.value override fun objectToMeta(obj: Value): Meta = Meta(obj) } public val string: MetaConverter = object : MetaConverter { - override fun metaToObject(meta: Meta): String? = meta.string + override fun metaToObjectOrNull(meta: Meta): String? = meta.string override fun objectToMeta(obj: String): Meta = Meta(obj.asValue()) } public val boolean: MetaConverter = object : MetaConverter { - override fun metaToObject(meta: Meta): Boolean? = meta.boolean + override fun metaToObjectOrNull(meta: Meta): Boolean? = meta.boolean override fun objectToMeta(obj: Boolean): Meta = Meta(obj.asValue()) } public val number: MetaConverter = object : MetaConverter { - override fun metaToObject(meta: Meta): Number? = meta.number + override fun metaToObjectOrNull(meta: Meta): Number? = meta.number override fun objectToMeta(obj: Number): Meta = Meta(obj.asValue()) } public val double: MetaConverter = object : MetaConverter { - override fun metaToObject(meta: Meta): Double? = meta.double - + override fun metaToObjectOrNull(meta: Meta): Double? = meta.double override fun objectToMeta(obj: Double): Meta = Meta(obj.asValue()) } public val float: MetaConverter = object : MetaConverter { - override fun metaToObject(meta: Meta): Float? = meta.float - + override fun metaToObjectOrNull(meta: Meta): Float? = meta.float override fun objectToMeta(obj: Float): Meta = Meta(obj.asValue()) } public val int: MetaConverter = object : MetaConverter { - override fun metaToObject(meta: Meta): Int? = meta.int - + override fun metaToObjectOrNull(meta: Meta): Int? = meta.int override fun objectToMeta(obj: Int): Meta = Meta(obj.asValue()) } public val long: MetaConverter = object : MetaConverter { - override fun metaToObject(meta: Meta): Long? = meta.long - + override fun metaToObjectOrNull(meta: Meta): Long? = meta.long override fun objectToMeta(obj: Long): Meta = Meta(obj.asValue()) } public inline fun > enum(): MetaConverter = object : MetaConverter { @Suppress("USELESS_CAST") - override fun metaToObject(meta: Meta): E = meta.enum() as? E ?: error("The Item is not a Enum") + override fun metaToObjectOrNull(meta: Meta): E = meta.enum() as? E ?: error("The Item is not a Enum") override fun objectToMeta(obj: E): Meta = Meta(obj.asValue()) } public fun valueList( writer: (T) -> Value = { Value.of(it) }, - reader: (Value) -> T + reader: (Value) -> T, ): MetaConverter> = object : MetaConverter> { - override fun metaToObject(meta: Meta): List = - meta.value?.list?.map(reader) ?: error("The item is not a value list") + override fun metaToObjectOrNull(meta: Meta): List? = meta.value?.list?.map(reader) override fun objectToMeta(obj: List): Meta = Meta(obj.map(writer).asValue()) } diff --git a/gradle.properties b/gradle.properties index 18f24486..c3c5b2a3 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,5 +6,5 @@ kotlin.mpp.stability.nowarn=true kotlin.incremental.js.ir=true kotlin.native.ignoreDisabledTargets=true -toolsVersion=0.15.0-kotlin-1.9.20-RC +toolsVersion=0.15.0-kotlin-1.9.20-RC2 #kotlin.experimental.tryK2=true \ No newline at end of file