v0.8.2 #80

Open
altavir wants to merge 63 commits from dev into master
28 changed files with 234 additions and 151 deletions
Showing only changes of commit 5a75b05acd - Show all commits

View File

@ -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

View File

@ -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<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = freeCompilerArgs + "-Xcontext-receivers"
compilerOptions {
freeCompilerArgs.add("-Xcontext-receivers")
}
}
}

View File

@ -10,7 +10,7 @@ import space.kscience.dataforge.meta.*
import space.kscience.dataforge.misc.DFExperimental
@DFExperimental
public fun <T> ObservableMeta.asFlow(converter: MetaSpec<T>): Flow<T> = callbackFlow {
public fun <T> ObservableMeta.asFlow(converter: MetaReader<T>): Flow<T> = callbackFlow {
onChange(this){
trySend(converter.read(this))
}

View File

@ -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<out Any>,
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 <reified T : Scheme> SchemeSpec<T>.autoDescriptor(noinline mod: MetaDescriptorBuilder.() -> Unit = {}): MetaDescriptor =
MetaDescriptor.forClass(T::class, mod)
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 <T : Scheme> MetaDescriptor.Companion.forScheme(
// spec: SchemeSpec<T>,
// mod: MetaDescriptorBuilder.() -> Unit = {},
//): MetaDescriptor = MetaDescriptor {
// val scheme = spec.empty()
// val kClass: KClass<T> = scheme::class as KClass<T>
// 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 <reified T : Scheme> SchemeSpec<T>.autoDescriptor(
// noinline mod: MetaDescriptorBuilder.() -> Unit = {},
//): MetaDescriptor = MetaDescriptor.forScheme(this, mod)

View File

@ -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>(::TestScheme) {
override val descriptor: MetaDescriptor = autoDescriptor()
}
}
class TestAutoDescriptors {
@Test
fun autoDescriptor() {
val autoDescriptor = MetaDescriptor.forClass(TestScheme::class)
println(Json { prettyPrint = true }.encodeToString(autoDescriptor))
}
}
//
//class TestScheme : Scheme() {
//
// @Description("A")
// val a by string()
//
// @Description("B")
// val b by int()
//
// val c by int()
//
// companion object : SchemeSpec<TestScheme>(::TestScheme) {
// override val descriptor: MetaDescriptor = autoDescriptor()
// }
//}
//
//class TestAutoDescriptors {
// @Test
// fun autoDescriptor() {
// val autoDescriptor = MetaDescriptor.forScheme(TestScheme)
// println(Json { prettyPrint = true }.encodeToString(autoDescriptor))
// }
//}

View File

@ -18,6 +18,24 @@ public data class NamedValueWithMeta<T>(val name: Name, val value: T, val meta:
public suspend fun <T> NamedData<T>.awaitWithMeta(): NamedValueWithMeta<T> =
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 <T, R> Data<T>.transform(
type: KType,
meta: Meta = this.meta,
coroutineContext: CoroutineContext = EmptyCoroutineContext,
block: suspend (T) -> R,
): Data<R> = 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).

View File

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

View File

@ -9,6 +9,8 @@ import space.kscience.dataforge.names.NameToken
*/
public class Laminate internal constructor(public val layers: List<Meta>) : TypedMeta<Laminate> {
override val self: Laminate get() = this
override val value: Value? = layers.firstNotNullOfOrNull { it.value }
override val items: Map<NameToken, Laminate> by lazy {

View File

@ -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<out M : TypedMeta<M>> : Meta {
override val items: Map<NameToken, M>
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<out M : TypedMeta<M>> : Meta {
override fun toMeta(): Meta = this
}
/**
* Access self as a recursive type instance
*/
public inline val <M : TypedMeta<M>> TypedMeta<M>.self: M get() = unsafeCast()
//public typealias Meta = TypedMeta<*>
public operator fun <M : TypedMeta<M>> TypedMeta<M>?.get(token: NameToken): M? = this?.items?.get(token)

View File

@ -11,7 +11,7 @@ import space.kscience.dataforge.misc.DFExperimental
/**
* A converter of generic object to and from [Meta]
*/
public interface MetaConverter<T>: MetaSpec<T> {
public interface MetaConverter<T>: MetaReader<T> {
/**
* A descriptor for resulting meta

View File

@ -25,16 +25,16 @@ public fun MetaProvider.node(
}
/**
* Use [metaSpec] to read the Meta node
* Use [metaReader] to read the Meta node
*/
public fun <T> MetaProvider.spec(
metaSpec: MetaSpec<T>,
metaReader: MetaReader<T>,
key: Name? = null,
): MetaDelegate<T?> = object : MetaDelegate<T?> {
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 <reified T> MetaProvider.serializable(
@Deprecated("Use convertable", ReplaceWith("convertable(converter, key)"))
public fun <T> MetaProvider.node(
key: Name? = null,
converter: MetaSpec<T>,
converter: MetaReader<T>,
): ReadOnlyProperty<Any?, T?> = spec(converter, key)
/**
* Use [converter] to convert a list of same name siblings meta to object
*/
public fun <T> Meta.listOfSpec(
converter: MetaSpec<T>,
converter: MetaReader<T>,
key: Name? = null,
): MetaDelegate<List<T>> = object : MetaDelegate<List<T>> {
override fun getValue(thisRef: Any?, property: KProperty<*>): List<T> {

View File

@ -2,7 +2,7 @@ package space.kscience.dataforge.meta
import space.kscience.dataforge.meta.descriptors.Described
public interface MetaSpec<out T> : Described {
public interface MetaReader<out T> : 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<out T> : 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 <T : Any> MetaSpec<T>.readNullable(item: Meta?): T? = item?.let { read(it) }
public fun <T> MetaSpec<T>.readValue(value: Value): T? = read(Meta(value))
public fun <T : Any> MetaReader<T>.readNullable(item: Meta?): T? = item?.let { read(it) }
public fun <T> MetaReader<T>.readValue(value: Value): T? = read(Meta(value))

View File

@ -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<T>(
public val name: Name,
public val converter: MetaConverter<T>,
override val descriptor: MetaDescriptor? = converter.descriptor,
) : Described
@DFExperimental
public operator fun <T> MetaProvider.get(ref: MetaRef<T>): T? = get(ref.name)?.let { ref.converter.readOrNull(it) }
@DFExperimental
public operator fun <T> MutableMetaProvider.set(ref: MetaRef<T>, value: T) {
set(ref.name, ref.converter.convert(value))
}
@DFExperimental
public class MetaSpec(
private val configuration: MetaDescriptorBuilder.() -> Unit = {},
) : Described {
private val refs: MutableList<MetaRef<*>> = mutableListOf()
private fun registerRef(ref: MetaRef<*>) {
refs.add(ref)
}
public fun <T> item(
converter: MetaConverter<T>,
descriptor: MetaDescriptor? = converter.descriptor,
key: Name? = null,
): PropertyDelegateProvider<MetaSpec, ReadOnlyProperty<MetaSpec, MetaRef<T>>> =
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()
}
}
}

View File

@ -253,6 +253,9 @@ private class MutableMetaImpl(
value: Value?,
children: Map<NameToken, Meta> = emptyMap(),
) : AbstractObservableMeta(), ObservableMutableMeta {
override val self get() = this
override var value = value
@ThreadSafe set(value) {
val oldValue = field

View File

@ -39,6 +39,9 @@ public interface ObservableMeta : Meta {
* A [Meta] which is both observable and mutable
*/
public interface ObservableMutableMeta : ObservableMeta, MutableMeta, MutableTypedMeta<ObservableMutableMeta> {
override val self: ObservableMutableMeta get() = this
override fun getOrCreate(name: Name): ObservableMutableMeta
override fun get(name: Name): ObservableMutableMeta? {

View File

@ -14,6 +14,9 @@ private class ObservableMetaWrapper(
val nodeName: Name,
val listeners: MutableSet<MetaListener>,
) : ObservableMutableMeta {
override val self get() = this
override val items: Map<NameToken, ObservableMutableMeta>
get() = root[nodeName]?.items?.keys?.associateWith {
ObservableMetaWrapper(root, nodeName + it, listeners)

View File

@ -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 <T : Scheme> 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 <T : Scheme> MutableMeta.scheme(
spec: SchemeSpec<T>,
@ -239,7 +242,7 @@ public fun <T : Scheme> Scheme.scheme(
): ReadWriteProperty<Any?, T> = 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 <T : Scheme> MutableMeta.schemeOrNull(
@ -264,7 +267,7 @@ public fun <T : Scheme> Scheme.schemeOrNull(
): ReadWriteProperty<Any?, T?> = 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.
*/

View File

@ -13,6 +13,9 @@ public class SealedMeta(
override val value: Value?,
override val items: Map<NameToken, SealedMeta>,
) : TypedMeta<SealedMeta> {
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)

View File

@ -58,7 +58,7 @@ public class MetaDescriptorBuilder @PublishedApi internal constructor() {
}
}
internal fun node(
public fun node(
name: Name,
descriptorBuilder: MetaDescriptor,
): Unit {

View File

@ -1,3 +0,0 @@
package space.kscience.dataforge.misc
public expect inline fun <T> Any?.unsafeCast(): T

View File

@ -31,6 +31,9 @@ public fun Meta.toDynamic(): dynamic {
}
public class DynamicMeta(internal val obj: dynamic) : TypedMeta<DynamicMeta> {
override val self: DynamicMeta get() = this
private fun keys(): Array<String> = js("Object").keys(obj) as Array<String>
private fun isArray(obj: dynamic): Boolean =

View File

@ -1,5 +0,0 @@
package space.kscience.dataforge.misc
import kotlin.js.unsafeCast as unsafeCastJs
@Suppress("NOTHING_TO_INLINE")
public actual inline fun <T> Any?.unsafeCast(): T = unsafeCastJs<T>()

View File

@ -1,4 +0,0 @@
package space.kscience.dataforge.misc
@Suppress("UNCHECKED_CAST", "NOTHING_TO_INLINE")
public actual inline fun <T> Any?.unsafeCast(): T = this as T

View File

@ -1,4 +0,0 @@
package space.kscience.dataforge.misc
@Suppress("UNCHECKED_CAST")
public actual inline fun <T> Any?.unsafeCast(): T = this as T

View File

@ -1,4 +0,0 @@
package space.kscience.dataforge.misc
@Suppress("UNCHECKED_CAST", "NOTHING_TO_INLINE")
public actual inline fun <T> Any?.unsafeCast(): T = this as T

View File

@ -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<T> : Described {
}
/**
* A [Task] with [MetaSpec] for wrapping and unwrapping task configuration
* A [Task] with [MetaReader] for wrapping and unwrapping task configuration
*/
public interface TaskWithSpec<T, C : Any> : Task<T> {
public val spec: MetaSpec<C>
public val spec: MetaReader<C>
override val descriptor: MetaDescriptor? get() = spec.descriptor
public suspend fun execute(workspace: Workspace, taskName: Name, configuration: C): TaskResult<T>
@ -122,10 +122,10 @@ public inline fun <reified T : Any> Task(
@Suppress("FunctionName")
public fun <T : Any, C : MetaRepr> Task(
resultType: KType,
specification: MetaSpec<C>,
specification: MetaReader<C>,
builder: suspend TaskResultBuilder<T>.(C) -> Unit,
): TaskWithSpec<T, C> = object : TaskWithSpec<T, C> {
override val spec: MetaSpec<C> = specification
override val spec: MetaReader<C> = specification
override suspend fun execute(
workspace: Workspace,
@ -143,6 +143,6 @@ public fun <T : Any, C : MetaRepr> Task(
}
public inline fun <reified T : Any, C : MetaRepr> Task(
specification: MetaSpec<C>,
specification: MetaReader<C>,
noinline builder: suspend TaskResultBuilder<T>.(C) -> Unit,
): Task<T> = Task(typeOf<T>(), specification, builder)

View File

@ -71,10 +71,10 @@ public inline fun <reified T : Any> TaskContainer.task(
}
/**
* Create a task based on [MetaSpec]
* Create a task based on [MetaReader]
*/
public inline fun <reified T : Any, C : MetaRepr> TaskContainer.task(
specification: MetaSpec<C>,
specification: MetaReader<C>,
noinline builder: suspend TaskResultBuilder<T>.(C) -> Unit,
): PropertyDelegateProvider<Any?, ReadOnlyProperty<Any?, TaskReference<T>>> = PropertyDelegateProvider { _, property ->
val taskName = Name.parse(property.name)

View File

@ -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
toolsVersion=0.15.4-kotlin-2.0.0