From 29fa30fb514cc2f8c596cb4590718f6e0e879041 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Mon, 20 Mar 2023 18:34:03 +0300 Subject: [PATCH] add type checks for inMemoryCache --- .../kscience/dataforge/workspace/FileWorkspaceCache.kt | 7 +++++-- .../dataforge/workspace/InMemoryWorkspaceCache.kt | 8 +++++--- .../dataforge/workspace/FileWorkspaceCacheTest.kt | 3 ++- 3 files changed, 12 insertions(+), 6 deletions(-) rename dataforge-workspace/src/{commonMain => jvmMain}/kotlin/space/kscience/dataforge/workspace/InMemoryWorkspaceCache.kt (83%) diff --git a/dataforge-workspace/src/jvmMain/kotlin/space/kscience/dataforge/workspace/FileWorkspaceCache.kt b/dataforge-workspace/src/jvmMain/kotlin/space/kscience/dataforge/workspace/FileWorkspaceCache.kt index 5d17baf3..fdcb49a6 100644 --- a/dataforge-workspace/src/jvmMain/kotlin/space/kscience/dataforge/workspace/FileWorkspaceCache.kt +++ b/dataforge-workspace/src/jvmMain/kotlin/space/kscience/dataforge/workspace/FileWorkspaceCache.kt @@ -4,6 +4,7 @@ import io.ktor.utils.io.core.Input import io.ktor.utils.io.core.Output import io.ktor.utils.io.core.readBytes import io.ktor.utils.io.core.writeFully +import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.KSerializer import kotlinx.serialization.json.Json import kotlinx.serialization.protobuf.ProtoBuf @@ -26,6 +27,7 @@ import kotlin.reflect.KType public class JsonIOFormat(override val type: KType) : IOFormat { + @Suppress("UNCHECKED_CAST") private val serializer: KSerializer = serializer(type) as KSerializer override fun readObject(input: Input): T = Json.decodeFromString(serializer, input.readUtf8String()) @@ -35,8 +37,10 @@ public class JsonIOFormat(override val type: KType) : IOFormat { } } +@OptIn(ExperimentalSerializationApi::class) public class ProtobufIOFormat(override val type: KType) : IOFormat { + @Suppress("UNCHECKED_CAST") private val serializer: KSerializer = serializer(type) as KSerializer override fun readObject(input: Input): T = ProtoBuf.decodeFromByteArray(serializer, input.readBytes()) @@ -49,8 +53,7 @@ public class ProtobufIOFormat(override val type: KType) : IOFormat { public class FileWorkspaceCache(public val cacheDirectory: Path) : WorkspaceCache { -// private fun TaskData<*>.checkType(taskType: KType): TaskData = this as TaskData - + // private fun TaskData<*>.checkType(taskType: KType): TaskData = this as TaskData @OptIn(DFExperimental::class, DFInternal::class) override suspend fun evaluate(result: TaskResult): TaskResult { diff --git a/dataforge-workspace/src/commonMain/kotlin/space/kscience/dataforge/workspace/InMemoryWorkspaceCache.kt b/dataforge-workspace/src/jvmMain/kotlin/space/kscience/dataforge/workspace/InMemoryWorkspaceCache.kt similarity index 83% rename from dataforge-workspace/src/commonMain/kotlin/space/kscience/dataforge/workspace/InMemoryWorkspaceCache.kt rename to dataforge-workspace/src/jvmMain/kotlin/space/kscience/dataforge/workspace/InMemoryWorkspaceCache.kt index 53968c21..bb8ea9a4 100644 --- a/dataforge-workspace/src/commonMain/kotlin/space/kscience/dataforge/workspace/InMemoryWorkspaceCache.kt +++ b/dataforge-workspace/src/jvmMain/kotlin/space/kscience/dataforge/workspace/InMemoryWorkspaceCache.kt @@ -3,6 +3,7 @@ package space.kscience.dataforge.workspace import space.kscience.dataforge.meta.Meta import space.kscience.dataforge.names.Name import kotlin.reflect.KType +import kotlin.reflect.full.isSubtypeOf private typealias TaskResultId = Pair @@ -12,9 +13,10 @@ public class InMemoryWorkspaceCache : WorkspaceCache { // never do that at home! private val cache = HashMap>>() - //TODO do actual check - @Suppress("UNUSED_PARAMETER") - private fun TaskData<*>.checkType(taskType: KType): TaskData = this as TaskData + @Suppress("UNCHECKED_CAST") + private fun TaskData<*>.checkType(taskType: KType): TaskData = + if (type.isSubtypeOf(taskType)) this as TaskData + else error("Cached data type mismatch: expected $taskType but got $type") override suspend fun evaluate(result: TaskResult): TaskResult { for (d: TaskData in result) { diff --git a/dataforge-workspace/src/jvmTest/kotlin/space/kscience/dataforge/workspace/FileWorkspaceCacheTest.kt b/dataforge-workspace/src/jvmTest/kotlin/space/kscience/dataforge/workspace/FileWorkspaceCacheTest.kt index 9d427a76..00ca67cb 100644 --- a/dataforge-workspace/src/jvmTest/kotlin/space/kscience/dataforge/workspace/FileWorkspaceCacheTest.kt +++ b/dataforge-workspace/src/jvmTest/kotlin/space/kscience/dataforge/workspace/FileWorkspaceCacheTest.kt @@ -22,7 +22,8 @@ class FileWorkspaceCacheTest { } fileCache(Files.createTempDirectory("dataforge-temporary-cache")) - task { + @Suppress("UNUSED_VARIABLE") + val echo by task { pipeFrom(dataByType()) { arg, _, _ -> arg } } }