From 5a75b05acd8893371fefb60e8190e5e688c3a014 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Tue, 4 Jun 2024 17:44:44 +0300 Subject: [PATCH] 0.9-RC --- CHANGELOG.md | 5 + build.gradle.kts | 10 +- .../dataforge/properties/metaAsFlow.kt | 2 +- .../descriptors/reflectiveDescriptors.kt | 129 +++++++++--------- .../descriptors/TestAutoDescriptors.kt | 53 ++++--- .../kscience/dataforge/data/dataTransform.kt | 18 +++ .../kscience/dataforge/data/ActionsTest.kt | 1 + .../space/kscience/dataforge/meta/Laminate.kt | 2 + .../space/kscience/dataforge/meta/Meta.kt | 8 +- .../kscience/dataforge/meta/MetaConverter.kt | 2 +- .../kscience/dataforge/meta/MetaDelegate.kt | 12 +- .../meta/{MetaSpec.kt => MetaReader.kt} | 8 +- .../space/kscience/dataforge/meta/MetaRef.kt | 64 +++++++++ .../kscience/dataforge/meta/MutableMeta.kt | 3 + .../kscience/dataforge/meta/ObservableMeta.kt | 3 + .../dataforge/meta/ObservableMetaWrapper.kt | 3 + .../space/kscience/dataforge/meta/Scheme.kt | 11 +- .../kscience/dataforge/meta/SealedMeta.kt | 3 + .../meta/descriptors/MetaDescriptorBuilder.kt | 2 +- .../space/kscience/dataforge/misc/cast.kt | 3 - .../kscience/dataforge/meta/DynamicMeta.kt | 3 + .../space/kscience/dataforge/misc/castJs.kt | 5 - .../space/kscience/dataforge/misc/castJvm.kt | 4 - .../kscience/dataforge/misc/castNative.kt | 4 - .../space/kscience/dataforge/misc/castWasm.kt | 4 - .../kscience/dataforge/workspace/Task.kt | 12 +- .../dataforge/workspace/WorkspaceBuilder.kt | 4 +- gradle.properties | 7 +- 28 files changed, 234 insertions(+), 151 deletions(-) rename dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/{MetaSpec.kt => MetaReader.kt} (59%) create mode 100644 dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MetaRef.kt delete mode 100644 dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/misc/cast.kt delete mode 100644 dataforge-meta/src/jsMain/kotlin/space/kscience/dataforge/misc/castJs.kt delete mode 100644 dataforge-meta/src/jvmMain/kotlin/space/kscience/dataforge/misc/castJvm.kt delete mode 100644 dataforge-meta/src/nativeMain/kotlin/space/kscience/dataforge/misc/castNative.kt delete mode 100644 dataforge-meta/src/wasmJsMain/kotlin/space/kscience/dataforge/misc/castWasm.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index a94cb585..d75dd1a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,12 +3,17 @@ ## Unreleased ### Added +- Custom CoroutineContext during `Context` creation. ### Changed +- Kotlin 2.0 +- `MetaSpec` renamed to `MetaReader`. MetaSpec is now reserved for builder-based generation of meta descriptors. +- Add self-type for Meta. Remove unsafe cast method for meta instances. ### Deprecated ### Removed +- Automatic descriptors for schema. It is not possible to implement them without heavy reflection. ### Fixed diff --git a/build.gradle.kts b/build.gradle.kts index 4db098be..815a7077 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -3,21 +3,21 @@ import space.kscience.gradle.useApache2Licence import space.kscience.gradle.useSPCTeam plugins { - id("space.kscience.gradle.project") - id("org.jetbrains.kotlinx.kover") version "0.7.6" + alias(spclibs.plugins.kscience.project) + alias(spclibs.plugins.kotlinx.kover) } allprojects { group = "space.kscience" - version = "0.8.2" + version = "0.9.0-dev-1" } subprojects { apply(plugin = "maven-publish") tasks.withType { - kotlinOptions { - freeCompilerArgs = freeCompilerArgs + "-Xcontext-receivers" + compilerOptions { + freeCompilerArgs.add("-Xcontext-receivers") } } } diff --git a/dataforge-context/src/commonMain/kotlin/space/kscience/dataforge/properties/metaAsFlow.kt b/dataforge-context/src/commonMain/kotlin/space/kscience/dataforge/properties/metaAsFlow.kt index 90fafc5e..da539fcb 100644 --- a/dataforge-context/src/commonMain/kotlin/space/kscience/dataforge/properties/metaAsFlow.kt +++ b/dataforge-context/src/commonMain/kotlin/space/kscience/dataforge/properties/metaAsFlow.kt @@ -10,7 +10,7 @@ import space.kscience.dataforge.meta.* import space.kscience.dataforge.misc.DFExperimental @DFExperimental -public fun ObservableMeta.asFlow(converter: MetaSpec): Flow = callbackFlow { +public fun ObservableMeta.asFlow(converter: MetaReader): Flow = callbackFlow { onChange(this){ trySend(converter.read(this)) } diff --git a/dataforge-context/src/jvmMain/kotlin/space/kscience/dataforge/descriptors/reflectiveDescriptors.kt b/dataforge-context/src/jvmMain/kotlin/space/kscience/dataforge/descriptors/reflectiveDescriptors.kt index b2953018..590324d7 100644 --- a/dataforge-context/src/jvmMain/kotlin/space/kscience/dataforge/descriptors/reflectiveDescriptors.kt +++ b/dataforge-context/src/jvmMain/kotlin/space/kscience/dataforge/descriptors/reflectiveDescriptors.kt @@ -4,18 +4,12 @@ import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.json.Json import kotlinx.serialization.json.decodeFromStream import org.slf4j.LoggerFactory -import space.kscience.dataforge.meta.Scheme -import space.kscience.dataforge.meta.SchemeSpec -import space.kscience.dataforge.meta.ValueType import space.kscience.dataforge.meta.descriptors.MetaDescriptor import space.kscience.dataforge.meta.descriptors.MetaDescriptorBuilder -import space.kscience.dataforge.meta.descriptors.node import space.kscience.dataforge.misc.DFExperimental import java.net.URL -import kotlin.reflect.KClass -import kotlin.reflect.full.isSubclassOf -import kotlin.reflect.full.memberProperties - +import kotlin.reflect.KAnnotatedElement +import kotlin.reflect.KProperty /** * Description text for meta property, node or whole object @@ -59,18 +53,8 @@ private fun MetaDescriptorBuilder.loadDescriptorFromResource(resource: Descripto } @DFExperimental -public fun MetaDescriptor.Companion.forClass( - kClass: KClass, - mod: MetaDescriptorBuilder.() -> Unit = {}, -): MetaDescriptor = MetaDescriptor { - when { - kClass.isSubclassOf(Number::class) -> valueType(ValueType.NUMBER) - kClass == String::class -> ValueType.STRING - kClass == Boolean::class -> ValueType.BOOLEAN - kClass == DoubleArray::class -> ValueType.LIST - } - - kClass.annotations.forEach { +public fun MetaDescriptorBuilder.forAnnotatedElement(element: KAnnotatedElement) { + element.annotations.forEach { when (it) { is Description -> description = it.value @@ -79,47 +63,70 @@ public fun MetaDescriptor.Companion.forClass( is DescriptorUrl -> loadDescriptorFromUrl(URL(it.url)) } } - kClass.memberProperties.forEach { property -> - - var flag = false - - val descriptor = MetaDescriptor { - //use base type descriptor as a base - (property.returnType.classifier as? KClass<*>)?.let { - from(forClass(it)) - } - - property.annotations.forEach { - when (it) { - is Description -> { - description = it.value - flag = true - } - - is Multiple -> { - multiple = true - flag = true - } - - is DescriptorResource -> { - loadDescriptorFromResource(it) - flag = true - } - - is DescriptorUrl -> { - loadDescriptorFromUrl(URL(it.url)) - flag = true - } - } - } - } - if (flag) { - node(property.name, descriptor) - } - } - mod() } @DFExperimental -public inline fun SchemeSpec.autoDescriptor(noinline mod: MetaDescriptorBuilder.() -> Unit = {}): MetaDescriptor = - MetaDescriptor.forClass(T::class, mod) \ No newline at end of file +public fun MetaDescriptorBuilder.forProperty(property: KProperty<*>) { + property.annotations.forEach { + when (it) { + is Description -> description = it.value + + is DescriptorResource -> loadDescriptorFromResource(it) + + is DescriptorUrl -> loadDescriptorFromUrl(URL(it.url)) + } + } +} +// +//@DFExperimental +//public fun MetaDescriptor.Companion.forScheme( +// spec: SchemeSpec, +// mod: MetaDescriptorBuilder.() -> Unit = {}, +//): MetaDescriptor = MetaDescriptor { +// val scheme = spec.empty() +// val kClass: KClass = scheme::class as KClass +// when { +// kClass.isSubclassOf(Number::class) -> valueType(ValueType.NUMBER) +// kClass == String::class -> ValueType.STRING +// kClass == Boolean::class -> ValueType.BOOLEAN +// kClass == DoubleArray::class -> ValueType.LIST +// kClass == ByteArray::class -> ValueType.LIST +// } +// +// forAnnotatedElement(kClass) +// kClass.memberProperties.forEach { property -> +// node(property.name) { +// +// (property.getDelegate(scheme) as? MetaDelegate<*>)?.descriptor?.let { +// from(it) +// } +// +// property.annotations.forEach { +// when (it) { +// is Description -> { +// description = it.value +// } +// +// is Multiple -> { +// multiple = true +// } +// +// is DescriptorResource -> { +// loadDescriptorFromResource(it) +// } +// +// is DescriptorUrl -> { +// loadDescriptorFromUrl(URL(it.url)) +// } +// } +// } +// } +// +// } +// mod() +//} +// +//@DFExperimental +//public inline fun SchemeSpec.autoDescriptor( +// noinline mod: MetaDescriptorBuilder.() -> Unit = {}, +//): MetaDescriptor = MetaDescriptor.forScheme(this, mod) \ No newline at end of file diff --git a/dataforge-context/src/jvmTest/kotlin/space/kscience/dataforge/descriptors/TestAutoDescriptors.kt b/dataforge-context/src/jvmTest/kotlin/space/kscience/dataforge/descriptors/TestAutoDescriptors.kt index df849ce5..3b1fce3d 100644 --- a/dataforge-context/src/jvmTest/kotlin/space/kscience/dataforge/descriptors/TestAutoDescriptors.kt +++ b/dataforge-context/src/jvmTest/kotlin/space/kscience/dataforge/descriptors/TestAutoDescriptors.kt @@ -2,35 +2,28 @@ package space.kscience.dataforge.descriptors -import kotlinx.serialization.encodeToString -import kotlinx.serialization.json.Json -import org.junit.jupiter.api.Test -import space.kscience.dataforge.meta.Scheme -import space.kscience.dataforge.meta.SchemeSpec -import space.kscience.dataforge.meta.descriptors.MetaDescriptor -import space.kscience.dataforge.meta.int -import space.kscience.dataforge.meta.string import space.kscience.dataforge.misc.DFExperimental -private class TestScheme : Scheme() { - - @Description("A") - val a by string() - - @Description("B") - val b by int() - - val c by int() - - companion object : SchemeSpec(::TestScheme) { - override val descriptor: MetaDescriptor = autoDescriptor() - } -} - -class TestAutoDescriptors { - @Test - fun autoDescriptor() { - val autoDescriptor = MetaDescriptor.forClass(TestScheme::class) - println(Json { prettyPrint = true }.encodeToString(autoDescriptor)) - } -} \ No newline at end of file +// +//class TestScheme : Scheme() { +// +// @Description("A") +// val a by string() +// +// @Description("B") +// val b by int() +// +// val c by int() +// +// companion object : SchemeSpec(::TestScheme) { +// override val descriptor: MetaDescriptor = autoDescriptor() +// } +//} +// +//class TestAutoDescriptors { +// @Test +// fun autoDescriptor() { +// val autoDescriptor = MetaDescriptor.forScheme(TestScheme) +// println(Json { prettyPrint = true }.encodeToString(autoDescriptor)) +// } +//} \ No newline at end of file diff --git a/dataforge-data/src/commonMain/kotlin/space/kscience/dataforge/data/dataTransform.kt b/dataforge-data/src/commonMain/kotlin/space/kscience/dataforge/data/dataTransform.kt index ab54eb3e..c0d92e9e 100644 --- a/dataforge-data/src/commonMain/kotlin/space/kscience/dataforge/data/dataTransform.kt +++ b/dataforge-data/src/commonMain/kotlin/space/kscience/dataforge/data/dataTransform.kt @@ -18,6 +18,24 @@ public data class NamedValueWithMeta(val name: Name, val value: T, val meta: public suspend fun NamedData.awaitWithMeta(): NamedValueWithMeta = NamedValueWithMeta(name, await(), meta) +/** + * Lazily transform this data to another data. By convention [block] should not use external data (be pure). + * @param type explicit type of the resulting [Data] + * @param coroutineContext additional [CoroutineContext] elements used for data computation. + * @param meta for the resulting data. By default equals input data. + * @param block the transformation itself + */ +@UnsafeKType +public fun Data.transform( + type: KType, + meta: Meta = this.meta, + coroutineContext: CoroutineContext = EmptyCoroutineContext, + block: suspend (T) -> R, +): Data = Data(type, meta, coroutineContext, listOf(this)) { + block(await()) +} + + /** * Lazily transform this data to another data. By convention [block] should not use external data (be pure). diff --git a/dataforge-data/src/jvmTest/kotlin/space/kscience/dataforge/data/ActionsTest.kt b/dataforge-data/src/jvmTest/kotlin/space/kscience/dataforge/data/ActionsTest.kt index 477ca592..13660eee 100644 --- a/dataforge-data/src/jvmTest/kotlin/space/kscience/dataforge/data/ActionsTest.kt +++ b/dataforge-data/src/jvmTest/kotlin/space/kscience/dataforge/data/ActionsTest.kt @@ -43,6 +43,7 @@ internal class ActionsTest { repeat(10) { source.updateValue(it.toString(), it) } + result.updates.take(10).onEach { println(it.name) }.collect() assertEquals(2, result["1"]?.await()) diff --git a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/Laminate.kt b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/Laminate.kt index 87284107..0ae84bb6 100644 --- a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/Laminate.kt +++ b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/Laminate.kt @@ -9,6 +9,8 @@ import space.kscience.dataforge.names.NameToken */ public class Laminate internal constructor(public val layers: List) : TypedMeta { + override val self: Laminate get() = this + override val value: Value? = layers.firstNotNullOfOrNull { it.value } override val items: Map by lazy { diff --git a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/Meta.kt b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/Meta.kt index 5cf53c75..fd953085 100644 --- a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/Meta.kt +++ b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/Meta.kt @@ -3,7 +3,6 @@ package space.kscience.dataforge.meta import kotlinx.serialization.Serializable import kotlinx.serialization.json.Json import space.kscience.dataforge.misc.DfType -import space.kscience.dataforge.misc.unsafeCast import space.kscience.dataforge.names.* import kotlin.jvm.JvmName @@ -151,6 +150,8 @@ public interface TypedMeta> : Meta { override val items: Map + public val self: M + override fun get(name: Name): M? { tailrec fun M.find(name: Name): M? = if (name.isEmpty()) { this @@ -164,11 +165,6 @@ public interface TypedMeta> : Meta { override fun toMeta(): Meta = this } -/** - * Access self as a recursive type instance - */ -public inline val > TypedMeta.self: M get() = unsafeCast() - //public typealias Meta = TypedMeta<*> public operator fun > TypedMeta?.get(token: NameToken): M? = this?.items?.get(token) diff --git a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MetaConverter.kt b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MetaConverter.kt index 124f49f5..9baf0087 100644 --- a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MetaConverter.kt +++ b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MetaConverter.kt @@ -11,7 +11,7 @@ import space.kscience.dataforge.misc.DFExperimental /** * A converter of generic object to and from [Meta] */ -public interface MetaConverter: MetaSpec { +public interface MetaConverter: MetaReader { /** * A descriptor for resulting meta 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 8c5a738f..1b506b44 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 @@ -25,16 +25,16 @@ public fun MetaProvider.node( } /** - * Use [metaSpec] to read the Meta node + * Use [metaReader] to read the Meta node */ public fun MetaProvider.spec( - metaSpec: MetaSpec, + metaReader: MetaReader, key: Name? = null, ): MetaDelegate = object : MetaDelegate { - override val descriptor: MetaDescriptor? get() = metaSpec.descriptor + override val descriptor: MetaDescriptor? get() = metaReader.descriptor override fun getValue(thisRef: Any?, property: KProperty<*>): T? { - return get(key ?: property.name.asName())?.let { metaSpec.read(it) } + return get(key ?: property.name.asName())?.let { metaReader.read(it) } } } @@ -50,14 +50,14 @@ public inline fun MetaProvider.serializable( @Deprecated("Use convertable", ReplaceWith("convertable(converter, key)")) public fun MetaProvider.node( key: Name? = null, - converter: MetaSpec, + converter: MetaReader, ): ReadOnlyProperty = spec(converter, key) /** * Use [converter] to convert a list of same name siblings meta to object */ public fun Meta.listOfSpec( - converter: MetaSpec, + converter: MetaReader, key: Name? = null, ): MetaDelegate> = object : MetaDelegate> { override fun getValue(thisRef: Any?, property: KProperty<*>): List { diff --git a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MetaSpec.kt b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MetaReader.kt similarity index 59% rename from dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MetaSpec.kt rename to dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MetaReader.kt index 9918d504..a8514d63 100644 --- a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MetaSpec.kt +++ b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MetaReader.kt @@ -2,7 +2,7 @@ package space.kscience.dataforge.meta import space.kscience.dataforge.meta.descriptors.Described -public interface MetaSpec : Described { +public interface MetaReader : Described { /** * Read the source meta into an object and return null if Meta could not be interpreted as a target type @@ -10,12 +10,12 @@ public interface MetaSpec : Described { public fun readOrNull(source: Meta): T? /** - * Read generic read-only meta with this [MetaSpec] producing instance of the desired type. + * Read generic read-only meta with this [MetaReader] producing instance of the desired type. * Throws an error if conversion could not be done. */ public fun read(source: Meta): T = readOrNull(source) ?: error("Meta $source could not be interpreted by $this") } -public fun MetaSpec.readNullable(item: Meta?): T? = item?.let { read(it) } -public fun MetaSpec.readValue(value: Value): T? = read(Meta(value)) +public fun MetaReader.readNullable(item: Meta?): T? = item?.let { read(it) } +public fun MetaReader.readValue(value: Value): T? = read(Meta(value)) diff --git a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MetaRef.kt b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MetaRef.kt new file mode 100644 index 00000000..413fe404 --- /dev/null +++ b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MetaRef.kt @@ -0,0 +1,64 @@ +package space.kscience.dataforge.meta + +import space.kscience.dataforge.meta.descriptors.Described +import space.kscience.dataforge.meta.descriptors.MetaDescriptor +import space.kscience.dataforge.meta.descriptors.MetaDescriptorBuilder +import space.kscience.dataforge.misc.DFExperimental +import space.kscience.dataforge.names.Name +import space.kscience.dataforge.names.asName +import kotlin.properties.PropertyDelegateProvider +import kotlin.properties.ReadOnlyProperty + + +/** + * A reference to a read-only value of type [T] inside [MetaProvider] + */ +@DFExperimental +public data class MetaRef( + public val name: Name, + public val converter: MetaConverter, + override val descriptor: MetaDescriptor? = converter.descriptor, +) : Described + +@DFExperimental +public operator fun MetaProvider.get(ref: MetaRef): T? = get(ref.name)?.let { ref.converter.readOrNull(it) } + +@DFExperimental +public operator fun MutableMetaProvider.set(ref: MetaRef, value: T) { + set(ref.name, ref.converter.convert(value)) +} + +@DFExperimental +public class MetaSpec( + private val configuration: MetaDescriptorBuilder.() -> Unit = {}, +) : Described { + private val refs: MutableList> = mutableListOf() + + private fun registerRef(ref: MetaRef<*>) { + refs.add(ref) + } + + public fun item( + converter: MetaConverter, + descriptor: MetaDescriptor? = converter.descriptor, + key: Name? = null, + ): PropertyDelegateProvider>> = + PropertyDelegateProvider { _, property -> + val ref = MetaRef(key ?: property.name.asName(), converter, descriptor) + registerRef(ref) + ReadOnlyProperty { _, _ -> + ref + } + } + + override val descriptor: MetaDescriptor by lazy { + MetaDescriptor { + refs.forEach { ref -> + ref.descriptor?.let { + node(ref.name, ref.descriptor) + } + } + configuration() + } + } +} \ No newline at end of file diff --git a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MutableMeta.kt b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MutableMeta.kt index 69e221f5..7be16cc9 100644 --- a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MutableMeta.kt +++ b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MutableMeta.kt @@ -253,6 +253,9 @@ private class MutableMetaImpl( value: Value?, children: Map = emptyMap(), ) : AbstractObservableMeta(), ObservableMutableMeta { + + override val self get() = this + override var value = value @ThreadSafe set(value) { val oldValue = field diff --git a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/ObservableMeta.kt b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/ObservableMeta.kt index b481962e..7cd28746 100644 --- a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/ObservableMeta.kt +++ b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/ObservableMeta.kt @@ -39,6 +39,9 @@ public interface ObservableMeta : Meta { * A [Meta] which is both observable and mutable */ public interface ObservableMutableMeta : ObservableMeta, MutableMeta, MutableTypedMeta { + + override val self: ObservableMutableMeta get() = this + override fun getOrCreate(name: Name): ObservableMutableMeta override fun get(name: Name): ObservableMutableMeta? { diff --git a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/ObservableMetaWrapper.kt b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/ObservableMetaWrapper.kt index abd2deb6..7c62f692 100644 --- a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/ObservableMetaWrapper.kt +++ b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/ObservableMetaWrapper.kt @@ -14,6 +14,9 @@ private class ObservableMetaWrapper( val nodeName: Name, val listeners: MutableSet, ) : ObservableMutableMeta { + + override val self get() = this + override val items: Map get() = root[nodeName]?.items?.keys?.associateWith { ObservableMetaWrapper(root, nodeName + it, listeners) diff --git a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/Scheme.kt b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/Scheme.kt index 85b5087a..fe121c42 100644 --- a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/Scheme.kt +++ b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/Scheme.kt @@ -12,7 +12,7 @@ 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 [MetaReader]. * * @param prototype default values provided by this scheme */ @@ -80,6 +80,9 @@ public open class Scheme( override fun toString(): String = meta.toString() private inner class SchemeMeta(val pathName: Name) : ObservableMutableMeta { + + override val self get() = this + override var value: Value? get() = target[pathName]?.value ?: prototype?.get(pathName)?.value @@ -216,7 +219,7 @@ public fun Configurable.updateWith( /** - * A delegate that uses a [MetaSpec] to wrap a child of this provider + * A delegate that uses a [MetaReader] to wrap a child of this provider */ public fun MutableMeta.scheme( spec: SchemeSpec, @@ -239,7 +242,7 @@ public fun Scheme.scheme( ): ReadWriteProperty = meta.scheme(spec, key) /** - * A delegate that uses a [MetaSpec] to wrap a child of this provider. + * A delegate that uses a [MetaReader] to wrap a child of this provider. * Returns null if meta with given name does not exist. */ public fun MutableMeta.schemeOrNull( @@ -264,7 +267,7 @@ public fun Scheme.schemeOrNull( ): ReadWriteProperty = meta.schemeOrNull(spec, key) /** - * A delegate that uses a [MetaSpec] to wrap a list of child providers. + * A delegate that uses a [MetaReader] to wrap a list of child providers. * If children are mutable, the changes in list elements are reflected on them. * The list is a snapshot of children state, so change in structure is not reflected on its composition. */ diff --git a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/SealedMeta.kt b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/SealedMeta.kt index b3fdf062..b218fad6 100644 --- a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/SealedMeta.kt +++ b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/SealedMeta.kt @@ -13,6 +13,9 @@ public class SealedMeta( override val value: Value?, override val items: Map, ) : TypedMeta { + + override val self: SealedMeta get() = this + override fun toString(): String = Meta.toString(this) override fun equals(other: Any?): Boolean = Meta.equals(this, other as? Meta) diff --git a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/descriptors/MetaDescriptorBuilder.kt b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/descriptors/MetaDescriptorBuilder.kt index dfdbfeab..2590273e 100644 --- a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/descriptors/MetaDescriptorBuilder.kt +++ b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/descriptors/MetaDescriptorBuilder.kt @@ -58,7 +58,7 @@ public class MetaDescriptorBuilder @PublishedApi internal constructor() { } } - internal fun node( + public fun node( name: Name, descriptorBuilder: MetaDescriptor, ): Unit { diff --git a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/misc/cast.kt b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/misc/cast.kt deleted file mode 100644 index e714d596..00000000 --- a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/misc/cast.kt +++ /dev/null @@ -1,3 +0,0 @@ -package space.kscience.dataforge.misc - -public expect inline fun Any?.unsafeCast(): T \ No newline at end of file diff --git a/dataforge-meta/src/jsMain/kotlin/space/kscience/dataforge/meta/DynamicMeta.kt b/dataforge-meta/src/jsMain/kotlin/space/kscience/dataforge/meta/DynamicMeta.kt index b38d5891..57d324c7 100644 --- a/dataforge-meta/src/jsMain/kotlin/space/kscience/dataforge/meta/DynamicMeta.kt +++ b/dataforge-meta/src/jsMain/kotlin/space/kscience/dataforge/meta/DynamicMeta.kt @@ -31,6 +31,9 @@ public fun Meta.toDynamic(): dynamic { } public class DynamicMeta(internal val obj: dynamic) : TypedMeta { + + override val self: DynamicMeta get() = this + private fun keys(): Array = js("Object").keys(obj) as Array private fun isArray(obj: dynamic): Boolean = diff --git a/dataforge-meta/src/jsMain/kotlin/space/kscience/dataforge/misc/castJs.kt b/dataforge-meta/src/jsMain/kotlin/space/kscience/dataforge/misc/castJs.kt deleted file mode 100644 index b057bcbe..00000000 --- a/dataforge-meta/src/jsMain/kotlin/space/kscience/dataforge/misc/castJs.kt +++ /dev/null @@ -1,5 +0,0 @@ -package space.kscience.dataforge.misc -import kotlin.js.unsafeCast as unsafeCastJs - -@Suppress("NOTHING_TO_INLINE") -public actual inline fun Any?.unsafeCast(): T = unsafeCastJs() \ No newline at end of file diff --git a/dataforge-meta/src/jvmMain/kotlin/space/kscience/dataforge/misc/castJvm.kt b/dataforge-meta/src/jvmMain/kotlin/space/kscience/dataforge/misc/castJvm.kt deleted file mode 100644 index 27d399fe..00000000 --- a/dataforge-meta/src/jvmMain/kotlin/space/kscience/dataforge/misc/castJvm.kt +++ /dev/null @@ -1,4 +0,0 @@ -package space.kscience.dataforge.misc - -@Suppress("UNCHECKED_CAST", "NOTHING_TO_INLINE") -public actual inline fun Any?.unsafeCast(): T = this as T \ No newline at end of file diff --git a/dataforge-meta/src/nativeMain/kotlin/space/kscience/dataforge/misc/castNative.kt b/dataforge-meta/src/nativeMain/kotlin/space/kscience/dataforge/misc/castNative.kt deleted file mode 100644 index 4d9aa758..00000000 --- a/dataforge-meta/src/nativeMain/kotlin/space/kscience/dataforge/misc/castNative.kt +++ /dev/null @@ -1,4 +0,0 @@ -package space.kscience.dataforge.misc - -@Suppress("UNCHECKED_CAST") -public actual inline fun Any?.unsafeCast(): T = this as T \ No newline at end of file diff --git a/dataforge-meta/src/wasmJsMain/kotlin/space/kscience/dataforge/misc/castWasm.kt b/dataforge-meta/src/wasmJsMain/kotlin/space/kscience/dataforge/misc/castWasm.kt deleted file mode 100644 index 27d399fe..00000000 --- a/dataforge-meta/src/wasmJsMain/kotlin/space/kscience/dataforge/misc/castWasm.kt +++ /dev/null @@ -1,4 +0,0 @@ -package space.kscience.dataforge.misc - -@Suppress("UNCHECKED_CAST", "NOTHING_TO_INLINE") -public actual inline fun Any?.unsafeCast(): T = this as T \ No newline at end of file diff --git a/dataforge-workspace/src/commonMain/kotlin/space/kscience/dataforge/workspace/Task.kt b/dataforge-workspace/src/commonMain/kotlin/space/kscience/dataforge/workspace/Task.kt index 372b119f..a1a754a4 100644 --- a/dataforge-workspace/src/commonMain/kotlin/space/kscience/dataforge/workspace/Task.kt +++ b/dataforge-workspace/src/commonMain/kotlin/space/kscience/dataforge/workspace/Task.kt @@ -5,8 +5,8 @@ import space.kscience.dataforge.data.DataSink import space.kscience.dataforge.data.GoalExecutionRestriction import space.kscience.dataforge.data.MutableDataTree import space.kscience.dataforge.meta.Meta +import space.kscience.dataforge.meta.MetaReader import space.kscience.dataforge.meta.MetaRepr -import space.kscience.dataforge.meta.MetaSpec import space.kscience.dataforge.meta.descriptors.Described import space.kscience.dataforge.meta.descriptors.MetaDescriptor import space.kscience.dataforge.misc.DfType @@ -44,10 +44,10 @@ public interface Task : Described { } /** - * A [Task] with [MetaSpec] for wrapping and unwrapping task configuration + * A [Task] with [MetaReader] for wrapping and unwrapping task configuration */ public interface TaskWithSpec : Task { - public val spec: MetaSpec + public val spec: MetaReader override val descriptor: MetaDescriptor? get() = spec.descriptor public suspend fun execute(workspace: Workspace, taskName: Name, configuration: C): TaskResult @@ -122,10 +122,10 @@ public inline fun Task( @Suppress("FunctionName") public fun Task( resultType: KType, - specification: MetaSpec, + specification: MetaReader, builder: suspend TaskResultBuilder.(C) -> Unit, ): TaskWithSpec = object : TaskWithSpec { - override val spec: MetaSpec = specification + override val spec: MetaReader = specification override suspend fun execute( workspace: Workspace, @@ -143,6 +143,6 @@ public fun Task( } public inline fun Task( - specification: MetaSpec, + specification: MetaReader, noinline builder: suspend TaskResultBuilder.(C) -> Unit, ): Task = Task(typeOf(), specification, builder) \ No newline at end of file diff --git a/dataforge-workspace/src/commonMain/kotlin/space/kscience/dataforge/workspace/WorkspaceBuilder.kt b/dataforge-workspace/src/commonMain/kotlin/space/kscience/dataforge/workspace/WorkspaceBuilder.kt index cf263a46..013c0171 100644 --- a/dataforge-workspace/src/commonMain/kotlin/space/kscience/dataforge/workspace/WorkspaceBuilder.kt +++ b/dataforge-workspace/src/commonMain/kotlin/space/kscience/dataforge/workspace/WorkspaceBuilder.kt @@ -71,10 +71,10 @@ public inline fun TaskContainer.task( } /** - * Create a task based on [MetaSpec] + * Create a task based on [MetaReader] */ public inline fun TaskContainer.task( - specification: MetaSpec, + specification: MetaReader, noinline builder: suspend TaskResultBuilder.(C) -> Unit, ): PropertyDelegateProvider>> = PropertyDelegateProvider { _, property -> val taskName = Name.parse(property.name) diff --git a/gradle.properties b/gradle.properties index 3734d13e..ffc318d9 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,10 +1,9 @@ +kotlin.code.style=official + org.gradle.parallel=true org.gradle.jvmargs=-Xmx4096m -kotlin.code.style=official kotlin.mpp.stability.nowarn=true -kotlin.incremental.js.ir=true kotlin.native.ignoreDisabledTargets=true -toolsVersion=0.15.2-kotlin-1.9.21 -#kotlin.experimental.tryK2=true \ No newline at end of file +toolsVersion=0.15.4-kotlin-2.0.0 \ No newline at end of file