From eaa9d40d60f9fbb90881f91f74ff6e72bd1c62f3 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sun, 24 Apr 2022 09:19:14 +0300 Subject: [PATCH] Change `suspend DataSet.getData` to `operator DataSet.get` --- CHANGELOG.md | 1 + .../kscience/dataforge/actions/ReduceAction.kt | 2 +- .../space/kscience/dataforge/data/DataSet.kt | 6 +++--- .../space/kscience/dataforge/data/DataTree.kt | 6 +++--- .../space/kscience/dataforge/data/GroupRule.kt | 2 +- .../space/kscience/dataforge/data/dataFilter.kt | 14 +++++++------- .../dataforge/data/dataSetBuilderInContext.kt | 2 +- .../kotlin/space/kscience/dataforge/data/select.kt | 12 ++++++------ .../kotlin/space/kscience/dataforge/io/fileIO.kt | 5 ++--- .../kscience/dataforge/workspace/TaskResult.kt | 4 ++-- .../kscience/dataforge/workspace/Workspace.kt | 2 +- .../dataforge/workspace/SimpleWorkspaceTest.kt | 2 +- 12 files changed, 29 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b677019b..94aafcc1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ - `Factory` is now `fun interface` and uses `build` instead of `invoke`. `invoke moved to an extension. - KTor 2.0 - DataTree `items` call is blocking. +- DataSet `getData` is no longer suspended and renamed to `get` ### Deprecated diff --git a/dataforge-data/src/commonMain/kotlin/space/kscience/dataforge/actions/ReduceAction.kt b/dataforge-data/src/commonMain/kotlin/space/kscience/dataforge/actions/ReduceAction.kt index 39f26dd6..126f1f46 100644 --- a/dataforge-data/src/commonMain/kotlin/space/kscience/dataforge/actions/ReduceAction.kt +++ b/dataforge-data/src/commonMain/kotlin/space/kscience/dataforge/actions/ReduceAction.kt @@ -48,7 +48,7 @@ public class ReduceGroupBuilder( public fun group( groupName: String, - filter: suspend (Name, Data) -> Boolean, + filter: (Name, Data) -> Boolean, action: JoinGroup.() -> Unit, ) { groupRules += { source -> diff --git a/dataforge-data/src/commonMain/kotlin/space/kscience/dataforge/data/DataSet.kt b/dataforge-data/src/commonMain/kotlin/space/kscience/dataforge/data/DataSet.kt index 1f319668..f4838c85 100644 --- a/dataforge-data/src/commonMain/kotlin/space/kscience/dataforge/data/DataSet.kt +++ b/dataforge-data/src/commonMain/kotlin/space/kscience/dataforge/data/DataSet.kt @@ -28,7 +28,7 @@ public interface DataSet { /** * Get data with given name. */ - public suspend fun getData(name: Name): Data? + public operator fun get(name: Name): Data? /** @@ -52,7 +52,7 @@ public interface DataSet { override fun flowData(): Flow> = emptyFlow() - override suspend fun getData(name: Name): Data? = null + override fun get(name: Name): Data? = null } } } @@ -101,4 +101,4 @@ public suspend fun DataSet<*>.toMeta(): Meta = Meta { } } -public val DataSet.updatesWithData: Flow> get() = updates.mapNotNull { getData(it)?.named(it) } \ No newline at end of file +public val DataSet.updatesWithData: Flow> get() = updates.mapNotNull { get(it)?.named(it) } \ No newline at end of file diff --git a/dataforge-data/src/commonMain/kotlin/space/kscience/dataforge/data/DataTree.kt b/dataforge-data/src/commonMain/kotlin/space/kscience/dataforge/data/DataTree.kt index ca51ce2e..8eee0113 100644 --- a/dataforge-data/src/commonMain/kotlin/space/kscience/dataforge/data/DataTree.kt +++ b/dataforge-data/src/commonMain/kotlin/space/kscience/dataforge/data/DataTree.kt @@ -57,10 +57,10 @@ public interface DataTree : DataSet { override suspend fun listTop(prefix: Name): List = getItem(prefix).tree?.items?.keys?.map { prefix + it } ?: emptyList() - override suspend fun getData(name: Name): Data? = when (name.length) { + override fun get(name: Name): Data? = when (name.length) { 0 -> null 1 -> items[name.firstOrNull()!!].data - else -> items[name.firstOrNull()!!].tree?.getData(name.cutFirst()) + else -> items[name.firstOrNull()!!].tree?.get(name.cutFirst()) } public companion object { @@ -73,7 +73,7 @@ public interface DataTree : DataSet { } } -public suspend fun DataSet.getData(name: String): Data? = getData(Name.parse(name)) +public suspend fun DataSet.getData(name: String): Data? = get(Name.parse(name)) /** * Get a [DataTreeItem] with given [name] or null if the item does not exist diff --git a/dataforge-data/src/commonMain/kotlin/space/kscience/dataforge/data/GroupRule.kt b/dataforge-data/src/commonMain/kotlin/space/kscience/dataforge/data/GroupRule.kt index c87f01d2..beaa3da0 100644 --- a/dataforge-data/src/commonMain/kotlin/space/kscience/dataforge/data/GroupRule.kt +++ b/dataforge-data/src/commonMain/kotlin/space/kscience/dataforge/data/GroupRule.kt @@ -50,7 +50,7 @@ public interface GroupRule { scope.launch { set.updates.collect { name -> - val data = set.getData(name) + val data = set.get(name) @Suppress("NULLABLE_EXTENSION_OPERATOR_WITH_SAFE_CALL_RECEIVER") val tagValue = data?.meta?.get(key)?.string ?: defaultTagValue diff --git a/dataforge-data/src/commonMain/kotlin/space/kscience/dataforge/data/dataFilter.kt b/dataforge-data/src/commonMain/kotlin/space/kscience/dataforge/data/dataFilter.kt index f9e4bb47..46f07260 100644 --- a/dataforge-data/src/commonMain/kotlin/space/kscience/dataforge/data/dataFilter.kt +++ b/dataforge-data/src/commonMain/kotlin/space/kscience/dataforge/data/dataFilter.kt @@ -17,7 +17,7 @@ import kotlin.reflect.KType * A stateless filtered [DataSet] */ public fun DataSet.filter( - predicate: suspend (Name, Data) -> Boolean, + predicate: (Name, Data) -> Boolean, ): ActiveDataSet = object : ActiveDataSet { override val dataType: KType get() = this@filter.dataType @@ -27,12 +27,12 @@ public fun DataSet.filter( override fun flowData(): Flow> = this@filter.flowData().filter { predicate(it.name, it.data) } - override suspend fun getData(name: Name): Data? = this@filter.getData(name)?.takeIf { + override fun get(name: Name): Data? = this@filter.get(name)?.takeIf { predicate(name, it) } override val updates: Flow = this@filter.updates.filter flowFilter@{ name -> - val theData = this@filter.getData(name) ?: return@flowFilter false + val theData = this@filter.get(name) ?: return@flowFilter false predicate(name, theData) } } @@ -50,8 +50,8 @@ else object : ActiveDataSet { override fun flowData(): Flow> = this@withNamePrefix.flowData().map { it.data.named(prefix + it.name) } - override suspend fun getData(name: Name): Data? = - name.removeHeadOrNull(name)?.let { this@withNamePrefix.getData(it) } + override fun get(name: Name): Data? = + name.removeHeadOrNull(name)?.let { this@withNamePrefix.get(it) } override val updates: Flow get() = this@withNamePrefix.updates.map { prefix + it } } @@ -72,7 +72,7 @@ public fun DataSet.branch(branchName: Name): DataSet = if (branc } } - override suspend fun getData(name: Name): Data? = this@branch.getData(branchName + name) + override fun get(name: Name): Data? = this@branch.get(branchName + name) override val updates: Flow get() = this@branch.updates.mapNotNull { it.removeHeadOrNull(branchName) } } @@ -80,5 +80,5 @@ public fun DataSet.branch(branchName: Name): DataSet = if (branc public fun DataSet.branch(branchName: String): DataSet = this@branch.branch(Name.parse(branchName)) @DFExperimental -public suspend fun DataSet.rootData(): Data? = getData(Name.EMPTY) +public suspend fun DataSet.rootData(): Data? = get(Name.EMPTY) diff --git a/dataforge-data/src/jvmMain/kotlin/space/kscience/dataforge/data/dataSetBuilderInContext.kt b/dataforge-data/src/jvmMain/kotlin/space/kscience/dataforge/data/dataSetBuilderInContext.kt index b105910e..51bfa187 100644 --- a/dataforge-data/src/jvmMain/kotlin/space/kscience/dataforge/data/dataSetBuilderInContext.kt +++ b/dataforge-data/src/jvmMain/kotlin/space/kscience/dataforge/data/dataSetBuilderInContext.kt @@ -35,6 +35,6 @@ context(DataSetBuilder) public fun CoroutineScope.setAndWatch( ): Job = launch { node(name, dataSet) dataSet.updates.collect { nameInBranch -> - data(name + nameInBranch, dataSet.getData(nameInBranch)) + data(name + nameInBranch, dataSet.get(nameInBranch)) } } \ No newline at end of file diff --git a/dataforge-data/src/jvmMain/kotlin/space/kscience/dataforge/data/select.kt b/dataforge-data/src/jvmMain/kotlin/space/kscience/dataforge/data/select.kt index df8d3c45..6c53c147 100644 --- a/dataforge-data/src/jvmMain/kotlin/space/kscience/dataforge/data/select.kt +++ b/dataforge-data/src/jvmMain/kotlin/space/kscience/dataforge/data/select.kt @@ -52,12 +52,12 @@ public fun DataSet<*>.select( it as NamedData } - override suspend fun getData(name: Name): Data? = this@select.getData(name)?.let { datum -> + override fun get(name: Name): Data? = this@select.get(name)?.let { datum -> if (checkDatum(name, datum)) datum.castOrNull(type) else null } override val updates: Flow = this@select.updates.filter { - val datum = this@select.getData(it) ?: return@filter false + val datum = this@select.get(it) ?: return@filter false checkDatum(it, datum) } } @@ -73,11 +73,11 @@ public inline fun DataSet<*>.select( /** * Select a single datum if it is present and of given [type] */ -public suspend fun DataSet<*>.selectOne(type: KType, name: Name): NamedData? = - getData(name)?.castOrNull(type)?.named(name) +public fun DataSet<*>.selectOne(type: KType, name: Name): NamedData? = + get(name)?.castOrNull(type)?.named(name) -public suspend inline fun DataSet<*>.selectOne(name: Name): NamedData? = +public inline fun DataSet<*>.selectOne(name: Name): NamedData? = selectOne(typeOf(), name) -public suspend inline fun DataSet<*>.selectOne(name: String): NamedData? = +public inline fun DataSet<*>.selectOne(name: String): NamedData? = selectOne(typeOf(), Name.parse(name)) \ No newline at end of file diff --git a/dataforge-io/src/jvmMain/kotlin/space/kscience/dataforge/io/fileIO.kt b/dataforge-io/src/jvmMain/kotlin/space/kscience/dataforge/io/fileIO.kt index 1b600200..f2c664d4 100644 --- a/dataforge-io/src/jvmMain/kotlin/space/kscience/dataforge/io/fileIO.kt +++ b/dataforge-io/src/jvmMain/kotlin/space/kscience/dataforge/io/fileIO.kt @@ -88,9 +88,8 @@ public fun EnvelopeFormat.readFile(path: Path): Envelope { */ @Suppress("UNCHECKED_CAST") @DFExperimental -public inline fun IOPlugin.resolveIOFormat(): IOFormat? { - return ioFormatFactories.find { it.type.isSupertypeOf(typeOf()) } as IOFormat? -} +public inline fun IOPlugin.resolveIOFormat(): IOFormat? = + ioFormatFactories.find { it.type.isSupertypeOf(typeOf()) } as IOFormat? /** * Read file containing meta using given [formatOverride] or file extension to infer meta type. diff --git a/dataforge-workspace/src/commonMain/kotlin/space/kscience/dataforge/workspace/TaskResult.kt b/dataforge-workspace/src/commonMain/kotlin/space/kscience/dataforge/workspace/TaskResult.kt index 6a12e3bc..fc8e1e06 100644 --- a/dataforge-workspace/src/commonMain/kotlin/space/kscience/dataforge/workspace/TaskResult.kt +++ b/dataforge-workspace/src/commonMain/kotlin/space/kscience/dataforge/workspace/TaskResult.kt @@ -26,7 +26,7 @@ public interface TaskResult : DataSet { public val taskMeta: Meta override fun flowData(): Flow> - override suspend fun getData(name: Name): TaskData? + override fun get(name: Name): TaskData? } private class TaskResultImpl( @@ -40,7 +40,7 @@ private class TaskResultImpl( workspace.wrapData(it, it.name, taskName, taskMeta) } - override suspend fun getData(name: Name): TaskData? = dataSet.getData(name)?.let { + override fun get(name: Name): TaskData? = dataSet.get(name)?.let { workspace.wrapData(it, name, taskName, taskMeta) } } diff --git a/dataforge-workspace/src/commonMain/kotlin/space/kscience/dataforge/workspace/Workspace.kt b/dataforge-workspace/src/commonMain/kotlin/space/kscience/dataforge/workspace/Workspace.kt index bc2eb7dd..3aa2c0f4 100644 --- a/dataforge-workspace/src/commonMain/kotlin/space/kscience/dataforge/workspace/Workspace.kt +++ b/dataforge-workspace/src/commonMain/kotlin/space/kscience/dataforge/workspace/Workspace.kt @@ -46,7 +46,7 @@ public interface Workspace : ContextAware, Provider { } public suspend fun produceData(taskName: Name, taskMeta: Meta, name: Name): TaskData<*>? = - produce(taskName, taskMeta).getData(name) + produce(taskName, taskMeta).get(name) public companion object { public const val TYPE: String = "workspace" diff --git a/dataforge-workspace/src/jvmTest/kotlin/space/kscience/dataforge/workspace/SimpleWorkspaceTest.kt b/dataforge-workspace/src/jvmTest/kotlin/space/kscience/dataforge/workspace/SimpleWorkspaceTest.kt index d0db9823..e1a5a466 100644 --- a/dataforge-workspace/src/jvmTest/kotlin/space/kscience/dataforge/workspace/SimpleWorkspaceTest.kt +++ b/dataforge-workspace/src/jvmTest/kotlin/space/kscience/dataforge/workspace/SimpleWorkspaceTest.kt @@ -103,7 +103,7 @@ class SimpleWorkspaceTest { val squareData = from(square) val linearData = from(linear) squareData.forEach { data -> - val newData: Data = data.combine(linearData.getData(data.name)!!) { l, r -> + val newData: Data = data.combine(linearData.get(data.name)!!) { l, r -> l + r } data(data.name, newData)