Meta converters return non-nullables

This commit is contained in:
Alexander Nozik 2023-10-29 21:39:40 +03:00
parent e52d509c2b
commit 261c415d3d
4 changed files with 24 additions and 20 deletions

View File

@ -6,6 +6,7 @@
- Added separate `Meta`, `SealedMeta` and `ObservableMutableMeta` builders. - Added separate `Meta`, `SealedMeta` and `ObservableMutableMeta` builders.
### Changed ### Changed
- Meta converter `metaToObject` returns a non-nullable type. Additional method `metaToObjectOrNull` for nullable return.
- Kotlin 1.9.20. - Kotlin 1.9.20.
- Migrated from ktor-io to kotlinx-io. - Migrated from ktor-io to kotlinx-io.
- `MutableMeta` builder now returns a simplified version of meta that does not hold listeners. - `MutableMeta` builder now returns a simplified version of meta that does not hold listeners.

View File

@ -8,7 +8,7 @@ plugins {
allprojects { allprojects {
group = "space.kscience" group = "space.kscience"
version = "0.6.3-dev-1" version = "0.7.0-dev-1"
} }
subprojects { subprojects {

View File

@ -6,74 +6,77 @@ import space.kscience.dataforge.meta.*
* A converter of generic object to and from [Meta] * A converter of generic object to and from [Meta]
*/ */
public interface MetaConverter<T> { public interface MetaConverter<T> {
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 fun objectToMeta(obj: T): Meta
public companion object { public companion object {
public val meta: MetaConverter<Meta> = object : MetaConverter<Meta> { public val meta: MetaConverter<Meta> = object : MetaConverter<Meta> {
override fun metaToObject(meta: Meta): Meta = meta override fun metaToObjectOrNull(meta: Meta): Meta = meta
override fun objectToMeta(obj: Meta): Meta = obj override fun objectToMeta(obj: Meta): Meta = obj
} }
public val value: MetaConverter<Value> = object : MetaConverter<Value> { public val value: MetaConverter<Value> = object : MetaConverter<Value> {
override fun metaToObject(meta: Meta): Value? = meta.value override fun metaToObjectOrNull(meta: Meta): Value? = meta.value
override fun objectToMeta(obj: Value): Meta = Meta(obj) override fun objectToMeta(obj: Value): Meta = Meta(obj)
} }
public val string: MetaConverter<String> = object : MetaConverter<String> { public val string: MetaConverter<String> = object : MetaConverter<String> {
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()) override fun objectToMeta(obj: String): Meta = Meta(obj.asValue())
} }
public val boolean: MetaConverter<Boolean> = object : MetaConverter<Boolean> { public val boolean: MetaConverter<Boolean> = object : MetaConverter<Boolean> {
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()) override fun objectToMeta(obj: Boolean): Meta = Meta(obj.asValue())
} }
public val number: MetaConverter<Number> = object : MetaConverter<Number> { public val number: MetaConverter<Number> = object : MetaConverter<Number> {
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()) override fun objectToMeta(obj: Number): Meta = Meta(obj.asValue())
} }
public val double: MetaConverter<Double> = object : MetaConverter<Double> { public val double: MetaConverter<Double> = object : MetaConverter<Double> {
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()) override fun objectToMeta(obj: Double): Meta = Meta(obj.asValue())
} }
public val float: MetaConverter<Float> = object : MetaConverter<Float> { public val float: MetaConverter<Float> = object : MetaConverter<Float> {
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()) override fun objectToMeta(obj: Float): Meta = Meta(obj.asValue())
} }
public val int: MetaConverter<Int> = object : MetaConverter<Int> { public val int: MetaConverter<Int> = object : MetaConverter<Int> {
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()) override fun objectToMeta(obj: Int): Meta = Meta(obj.asValue())
} }
public val long: MetaConverter<Long> = object : MetaConverter<Long> { public val long: MetaConverter<Long> = object : MetaConverter<Long> {
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()) override fun objectToMeta(obj: Long): Meta = Meta(obj.asValue())
} }
public inline fun <reified E : Enum<E>> enum(): MetaConverter<E> = object : MetaConverter<E> { public inline fun <reified E : Enum<E>> enum(): MetaConverter<E> = object : MetaConverter<E> {
@Suppress("USELESS_CAST") @Suppress("USELESS_CAST")
override fun metaToObject(meta: Meta): E = meta.enum<E>() as? E ?: error("The Item is not a Enum") override fun metaToObjectOrNull(meta: Meta): E = meta.enum<E>() as? E ?: error("The Item is not a Enum")
override fun objectToMeta(obj: E): Meta = Meta(obj.asValue()) override fun objectToMeta(obj: E): Meta = Meta(obj.asValue())
} }
public fun <T> valueList( public fun <T> valueList(
writer: (T) -> Value = { Value.of(it) }, writer: (T) -> Value = { Value.of(it) },
reader: (Value) -> T reader: (Value) -> T,
): MetaConverter<List<T>> = ): MetaConverter<List<T>> =
object : MetaConverter<List<T>> { object : MetaConverter<List<T>> {
override fun metaToObject(meta: Meta): List<T> = override fun metaToObjectOrNull(meta: Meta): List<T>? = meta.value?.list?.map(reader)
meta.value?.list?.map(reader) ?: error("The item is not a value list")
override fun objectToMeta(obj: List<T>): Meta = Meta(obj.map(writer).asValue()) override fun objectToMeta(obj: List<T>): Meta = Meta(obj.map(writer).asValue())
} }

View File

@ -6,5 +6,5 @@ kotlin.mpp.stability.nowarn=true
kotlin.incremental.js.ir=true kotlin.incremental.js.ir=true
kotlin.native.ignoreDisabledTargets=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 #kotlin.experimental.tryK2=true