Skip to content

Commit

Permalink
Meta converters return non-nullables
Browse files Browse the repository at this point in the history
  • Loading branch information
altavir committed Oct 29, 2023
1 parent e52d509 commit 261c415
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {

allprojects {
group = "space.kscience"
version = "0.6.3-dev-1"
version = "0.7.0-dev-1"
}

subprojects {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,74 +6,77 @@ import space.kscience.dataforge.meta.*
* A converter of generic object to and from [Meta]
*/
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 companion object {

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
}

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)
}

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())
}

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())
}

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())
}

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())
}

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())
}

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())
}

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())
}

public inline fun <reified E : Enum<E>> enum(): MetaConverter<E> = object : MetaConverter<E> {
@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())
}

public fun <T> valueList(
writer: (T) -> Value = { Value.of(it) },
reader: (Value) -> T
reader: (Value) -> T,
): MetaConverter<List<T>> =
object : MetaConverter<List<T>> {
override fun metaToObject(meta: Meta): List<T> =
meta.value?.list?.map(reader) ?: error("The item is not a value list")
override fun metaToObjectOrNull(meta: Meta): List<T>? = meta.value?.list?.map(reader)

override fun objectToMeta(obj: List<T>): Meta = Meta(obj.map(writer).asValue())
}
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 261c415

Please sign in to comment.