Merge MetaConverter and MetaSpec
This commit is contained in:
parent
fd1d98aa87
commit
991f77c45a
@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
- Descriptor `children` renamed to `nodes`
|
- Descriptor `children` renamed to `nodes`
|
||||||
|
- `MetaConverter` now inherits `MetaSpec` (former `Specifiction`). So `MetaConverter` could be used more universally.
|
||||||
|
|
||||||
### Deprecated
|
### Deprecated
|
||||||
- `node(key,converter)` in favor of `serializable` delegate
|
- `node(key,converter)` in favor of `serializable` delegate
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package space.kscience.dataforge.properties
|
package space.kscience.dataforge.properties
|
||||||
|
|
||||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
import space.kscience.dataforge.misc.DFExperimental
|
import space.kscience.dataforge.misc.DFExperimental
|
||||||
@ -14,7 +13,6 @@ public interface Property<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@DFExperimental
|
@DFExperimental
|
||||||
@OptIn(ExperimentalCoroutinesApi::class)
|
|
||||||
public fun <T> Property<T>.toFlow(): StateFlow<T> = MutableStateFlow(value).also { stateFlow ->
|
public fun <T> Property<T>.toFlow(): StateFlow<T> = MutableStateFlow(value).also { stateFlow ->
|
||||||
onChange {
|
onChange {
|
||||||
stateFlow.value = it
|
stateFlow.value = it
|
||||||
|
@ -183,7 +183,6 @@ public interface MetaConverter<T>: MetaSpec<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun <T : Any> MetaConverter<T>.readNullable(item: Meta?): T? = item?.let { read(it) }
|
|
||||||
public fun <T : Any> MetaConverter<T>.convertNullable(obj: T?): Meta? = obj?.let { convert(it) }
|
public fun <T : Any> MetaConverter<T>.convertNullable(obj: T?): Meta? = obj?.let { convert(it) }
|
||||||
|
|
||||||
public fun <T> MetaConverter<T>.readValue(value: Value): T? = read(Meta(value))
|
|
||||||
|
@ -13,13 +13,13 @@ public fun MetaProvider.node(key: Name? = null): ReadOnlyProperty<Any?, Meta?> =
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Use [converter] to read the Meta node
|
* Use [metaSpec] to read the Meta node
|
||||||
*/
|
*/
|
||||||
public fun <T> MetaProvider.convertable(
|
public fun <T> MetaProvider.spec(
|
||||||
converter: MetaConverter<T>,
|
metaSpec: MetaSpec<T>,
|
||||||
key: Name? = null,
|
key: Name? = null,
|
||||||
): ReadOnlyProperty<Any?, T?> = ReadOnlyProperty { _, property ->
|
): ReadOnlyProperty<Any?, T?> = ReadOnlyProperty { _, property ->
|
||||||
get(key ?: property.name.asName())?.let { converter.read(it) }
|
get(key ?: property.name.asName())?.let { metaSpec.read(it) }
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -29,19 +29,19 @@ public fun <T> MetaProvider.convertable(
|
|||||||
public inline fun <reified T> MetaProvider.serializable(
|
public inline fun <reified T> MetaProvider.serializable(
|
||||||
descriptor: MetaDescriptor? = null,
|
descriptor: MetaDescriptor? = null,
|
||||||
key: Name? = null,
|
key: Name? = null,
|
||||||
): ReadOnlyProperty<Any?, T?> = convertable(MetaConverter.serializable(descriptor), key)
|
): ReadOnlyProperty<Any?, T?> = spec(MetaConverter.serializable(descriptor), key)
|
||||||
|
|
||||||
@Deprecated("Use convertable", ReplaceWith("convertable(converter, key)"))
|
@Deprecated("Use convertable", ReplaceWith("convertable(converter, key)"))
|
||||||
public fun <T> MetaProvider.node(
|
public fun <T> MetaProvider.node(
|
||||||
key: Name? = null,
|
key: Name? = null,
|
||||||
converter: MetaConverter<T>,
|
converter: MetaSpec<T>,
|
||||||
): ReadOnlyProperty<Any?, T?> = convertable(converter, key)
|
): ReadOnlyProperty<Any?, T?> = spec(converter, key)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Use [converter] to convert a list of same name siblings meta to object
|
* Use [converter] to convert a list of same name siblings meta to object
|
||||||
*/
|
*/
|
||||||
public fun <T> Meta.listOfConvertable(
|
public fun <T> Meta.listOfSpec(
|
||||||
converter: MetaConverter<T>,
|
converter: MetaSpec<T>,
|
||||||
key: Name? = null,
|
key: Name? = null,
|
||||||
): ReadOnlyProperty<Any?, List<T>> = ReadOnlyProperty{_, property ->
|
): ReadOnlyProperty<Any?, List<T>> = ReadOnlyProperty{_, property ->
|
||||||
val name = key ?: property.name.asName()
|
val name = key ?: property.name.asName()
|
||||||
@ -52,7 +52,7 @@ public fun <T> Meta.listOfConvertable(
|
|||||||
public inline fun <reified T> Meta.listOfSerializable(
|
public inline fun <reified T> Meta.listOfSerializable(
|
||||||
descriptor: MetaDescriptor? = null,
|
descriptor: MetaDescriptor? = null,
|
||||||
key: Name? = null,
|
key: Name? = null,
|
||||||
): ReadOnlyProperty<Any?, List<T>> = listOfConvertable(MetaConverter.serializable(descriptor), key)
|
): ReadOnlyProperty<Any?, List<T>> = listOfSpec(MetaConverter.serializable(descriptor), key)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A property delegate that uses custom key
|
* A property delegate that uses custom key
|
||||||
|
@ -16,3 +16,6 @@ public interface MetaSpec<out T> : Described {
|
|||||||
public fun read(source: Meta): T = readOrNull(source) ?: error("Meta $source could not be interpreted by $this")
|
public fun read(source: Meta): T = readOrNull(source) ?: error("Meta $source could not be interpreted by $this")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public fun <T : Any> MetaSpec<T>.readNullable(item: Meta?): T? = item?.let { read(it) }
|
||||||
|
public fun <T> MetaSpec<T>.readValue(value: Value): T? = read(Meta(value))
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
package space.kscience.dataforge.meta
|
package space.kscience.dataforge.meta
|
||||||
|
|
||||||
import space.kscience.dataforge.misc.ThreadSafe
|
import space.kscience.dataforge.misc.ThreadSafe
|
||||||
import space.kscience.dataforge.names.*
|
import space.kscience.dataforge.names.Name
|
||||||
import kotlin.reflect.KProperty1
|
import space.kscience.dataforge.names.cutFirst
|
||||||
|
import space.kscience.dataforge.names.firstOrNull
|
||||||
|
import space.kscience.dataforge.names.isEmpty
|
||||||
|
|
||||||
|
|
||||||
internal data class MetaListener(
|
internal data class MetaListener(
|
||||||
@ -68,23 +70,3 @@ internal abstract class AbstractObservableMeta : ObservableMeta {
|
|||||||
override fun equals(other: Any?): Boolean = Meta.equals(this, other as? Meta)
|
override fun equals(other: Any?): Boolean = Meta.equals(this, other as? Meta)
|
||||||
override fun hashCode(): Int = Meta.hashCode(this)
|
override fun hashCode(): Int = Meta.hashCode(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Use the value of the property in a [callBack].
|
|
||||||
* The callback is called once immediately after subscription to pass the initial value.
|
|
||||||
*
|
|
||||||
* Optional [owner] property is used for
|
|
||||||
*/
|
|
||||||
public fun <S : Scheme, T> S.useProperty(
|
|
||||||
property: KProperty1<S, T>,
|
|
||||||
owner: Any? = null,
|
|
||||||
callBack: S.(T) -> Unit,
|
|
||||||
) {
|
|
||||||
//Pass initial value.
|
|
||||||
callBack(property.get(this))
|
|
||||||
meta.onChange(owner) { name ->
|
|
||||||
if (name.startsWith(property.name.asName())) {
|
|
||||||
callBack(property.get(this@useProperty))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -9,6 +9,7 @@ import space.kscience.dataforge.misc.ThreadSafe
|
|||||||
import space.kscience.dataforge.names.*
|
import space.kscience.dataforge.names.*
|
||||||
import kotlin.properties.ReadWriteProperty
|
import kotlin.properties.ReadWriteProperty
|
||||||
import kotlin.reflect.KProperty
|
import kotlin.reflect.KProperty
|
||||||
|
import kotlin.reflect.KProperty1
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A base for delegate-based or descriptor-based scheme. [Scheme] has an empty constructor to simplify usage from [MetaSpec].
|
* A base for delegate-based or descriptor-based scheme. [Scheme] has an empty constructor to simplify usage from [MetaSpec].
|
||||||
@ -288,3 +289,24 @@ public fun <T : Scheme> Scheme.listOfScheme(
|
|||||||
spec: SchemeSpec<T>,
|
spec: SchemeSpec<T>,
|
||||||
key: Name? = null,
|
key: Name? = null,
|
||||||
): ReadWriteProperty<Any?, List<T>> = meta.listOfScheme(spec, key)
|
): ReadWriteProperty<Any?, List<T>> = meta.listOfScheme(spec, key)
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Use the value of the property in a [callBack].
|
||||||
|
* The callback is called once immediately after subscription to pass the initial value.
|
||||||
|
*
|
||||||
|
* Optional [owner] property is used for
|
||||||
|
*/
|
||||||
|
public fun <S : Scheme, T> S.useProperty(
|
||||||
|
property: KProperty1<S, T>,
|
||||||
|
owner: Any? = null,
|
||||||
|
callBack: S.(T) -> Unit,
|
||||||
|
) {
|
||||||
|
//Pass initial value.
|
||||||
|
callBack(property.get(this))
|
||||||
|
meta.onChange(owner) { name ->
|
||||||
|
if (name.startsWith(property.name.asName())) {
|
||||||
|
callBack(property.get(this@useProperty))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -3,9 +3,11 @@ package space.kscience.dataforge.meta.descriptors
|
|||||||
import space.kscience.dataforge.meta.Scheme
|
import space.kscience.dataforge.meta.Scheme
|
||||||
import space.kscience.dataforge.meta.SchemeSpec
|
import space.kscience.dataforge.meta.SchemeSpec
|
||||||
import space.kscience.dataforge.meta.ValueType
|
import space.kscience.dataforge.meta.ValueType
|
||||||
|
import space.kscience.dataforge.misc.DFExperimental
|
||||||
import kotlin.reflect.KProperty1
|
import kotlin.reflect.KProperty1
|
||||||
import kotlin.reflect.typeOf
|
import kotlin.reflect.typeOf
|
||||||
|
|
||||||
|
@DFExperimental
|
||||||
public inline fun <S : Scheme, reified T> MetaDescriptorBuilder.value(
|
public inline fun <S : Scheme, reified T> MetaDescriptorBuilder.value(
|
||||||
property: KProperty1<S, T>,
|
property: KProperty1<S, T>,
|
||||||
noinline block: MetaDescriptorBuilder.() -> Unit = {},
|
noinline block: MetaDescriptorBuilder.() -> Unit = {},
|
||||||
@ -37,6 +39,7 @@ public inline fun <S : Scheme, reified T> MetaDescriptorBuilder.value(
|
|||||||
else -> node(property.name, block)
|
else -> node(property.name, block)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@DFExperimental
|
||||||
public inline fun <S : Scheme, reified T : Scheme> MetaDescriptorBuilder.scheme(
|
public inline fun <S : Scheme, reified T : Scheme> MetaDescriptorBuilder.scheme(
|
||||||
property: KProperty1<S, T>,
|
property: KProperty1<S, T>,
|
||||||
spec: SchemeSpec<T>,
|
spec: SchemeSpec<T>,
|
||||||
|
Loading…
Reference in New Issue
Block a user