From 58c5355e252ad088ceda1162f5256357fff89625 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Mon, 20 Feb 2023 18:31:56 +0300 Subject: [PATCH] Fix bug in plugin `isAttached` --- build.gradle.kts | 2 +- .../dataforge/context/AbstractPlugin.kt | 2 +- .../dataforge-io-yaml/build.gradle.kts | 2 +- .../dataforge/io/yaml/YamlMetaFormat.kt | 4 +-- .../space/kscience/dataforge/io/IOFormat.kt | 18 ++++++++--- .../kscience/dataforge/workspace/fileData.kt | 31 ++++++++++++++----- 6 files changed, 42 insertions(+), 17 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index c1d6b7b8..97b67691 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -9,7 +9,7 @@ plugins { allprojects { group = "space.kscience" - version = "0.6.1-dev-2" + version = "0.6.1-dev-3" } subprojects { diff --git a/dataforge-context/src/commonMain/kotlin/space/kscience/dataforge/context/AbstractPlugin.kt b/dataforge-context/src/commonMain/kotlin/space/kscience/dataforge/context/AbstractPlugin.kt index bfba218a..3357a0f1 100644 --- a/dataforge-context/src/commonMain/kotlin/space/kscience/dataforge/context/AbstractPlugin.kt +++ b/dataforge-context/src/commonMain/kotlin/space/kscience/dataforge/context/AbstractPlugin.kt @@ -11,7 +11,7 @@ public abstract class AbstractPlugin(override val meta: Meta = Meta.EMPTY) : Plu private var _context: Context? = null private val dependencies = HashMap, Meta>() - override val isAttached: Boolean = _context != null + override val isAttached: Boolean get() = _context != null override val context: Context get() = _context ?: error("Plugin $tag is not attached") diff --git a/dataforge-io/dataforge-io-yaml/build.gradle.kts b/dataforge-io/dataforge-io-yaml/build.gradle.kts index 854925f6..b33e1b9a 100644 --- a/dataforge-io/dataforge-io-yaml/build.gradle.kts +++ b/dataforge-io/dataforge-io-yaml/build.gradle.kts @@ -15,7 +15,7 @@ kscience { dependencies { api(projects.dataforgeIo) } - useSerialization("1.4.1"){ + useSerialization{ yamlKt() } } diff --git a/dataforge-io/dataforge-io-yaml/src/commonMain/kotlin/space/kscience/dataforge/io/yaml/YamlMetaFormat.kt b/dataforge-io/dataforge-io-yaml/src/commonMain/kotlin/space/kscience/dataforge/io/yaml/YamlMetaFormat.kt index f043508c..51c143a6 100644 --- a/dataforge-io/dataforge-io-yaml/src/commonMain/kotlin/space/kscience/dataforge/io/yaml/YamlMetaFormat.kt +++ b/dataforge-io/dataforge-io-yaml/src/commonMain/kotlin/space/kscience/dataforge/io/yaml/YamlMetaFormat.kt @@ -90,8 +90,8 @@ public fun YamlMap.toMeta(): Meta = YamlMeta(this) public class YamlMetaFormat(private val meta: Meta) : MetaFormat { override fun writeMeta(output: Output, meta: Meta, descriptor: MetaDescriptor?) { - val yaml = meta.toYaml() - val string = Yaml.encodeToString(yaml) + val yaml: YamlMap = meta.toYaml() + val string = Yaml.encodeToString(YamlMap.serializer(), yaml) output.writeUtf8String(string) } diff --git a/dataforge-io/src/commonMain/kotlin/space/kscience/dataforge/io/IOFormat.kt b/dataforge-io/src/commonMain/kotlin/space/kscience/dataforge/io/IOFormat.kt index 10f33e19..12ea587b 100644 --- a/dataforge-io/src/commonMain/kotlin/space/kscience/dataforge/io/IOFormat.kt +++ b/dataforge-io/src/commonMain/kotlin/space/kscience/dataforge/io/IOFormat.kt @@ -1,9 +1,6 @@ package space.kscience.dataforge.io -import io.ktor.utils.io.core.Input -import io.ktor.utils.io.core.Output -import io.ktor.utils.io.core.readDouble -import io.ktor.utils.io.core.writeDouble +import io.ktor.utils.io.core.* import space.kscience.dataforge.context.Context import space.kscience.dataforge.context.Factory import space.kscience.dataforge.io.IOFormatFactory.Companion.IO_FORMAT_TYPE @@ -27,6 +24,19 @@ public interface IOReader { public fun readObject(input: Input): T public fun readObject(binary: Binary): T = binary.read { readObject(this) } + + public companion object { + /** + * no-op reader for binaries. + */ + public val binary: IOReader = object : IOReader { + override val type: KType = typeOf() + + override fun readObject(input: Input): Binary = input.readBytes().asBinary() + + override fun readObject(binary: Binary): Binary = binary + } + } } public inline fun IOReader(crossinline read: Input.() -> T): IOReader = object : IOReader { diff --git a/dataforge-workspace/src/jvmMain/kotlin/space/kscience/dataforge/workspace/fileData.kt b/dataforge-workspace/src/jvmMain/kotlin/space/kscience/dataforge/workspace/fileData.kt index cd34d8bb..c352d635 100644 --- a/dataforge-workspace/src/jvmMain/kotlin/space/kscience/dataforge/workspace/fileData.kt +++ b/dataforge-workspace/src/jvmMain/kotlin/space/kscience/dataforge/workspace/fileData.kt @@ -36,11 +36,14 @@ import kotlin.reflect.typeOf public typealias FileFormatResolver = (path: Path, meta: Meta) -> IOReader -public class FileData internal constructor(private val data: Data) : Data by data { - - public val path: String? get() = meta[META_FILE_PATH_KEY].string - public val extension: String? get() = meta[META_FILE_EXTENSION_KEY].string +/** + * A data based on a filesystem [Path] + */ +public class FileData internal constructor(private val data: Data, public val path: Path) : Data by data { + // public val path: String? get() = meta[META_FILE_PATH_KEY].string +// public val extension: String? get() = meta[META_FILE_EXTENSION_KEY].string +// public val createdTime: Instant? get() = meta[META_FILE_CREATE_TIME_KEY].string?.let { Instant.parse(it) } public val updatedTime: Instant? get() = meta[META_FILE_UPDATE_TIME_KEY].string?.let { Instant.parse(it) } @@ -74,9 +77,12 @@ public fun IOPlugin.readDataFile( FileData.META_FILE_UPDATE_TIME_KEY put attributes.lastModifiedTime().toInstant().toString() FileData.META_FILE_CREATE_TIME_KEY put attributes.creationTime().toInstant().toString() } - return FileData(Data(format.type, updatedMeta) { - envelope.data?.readWith(format) ?: error("Can't convert envelope without content to Data") - }) + return FileData( + Data(format.type, updatedMeta) { + envelope.data?.readWith(format) ?: error("Can't convert envelope without content to Data") + }, + path + ) } @@ -122,7 +128,15 @@ public fun IOPlugin.readDataDirectory( public inline fun IOPlugin.readDataDirectory( path: Path, noinline formatResolver: FileFormatResolver, -): DataTree = readDataDirectory(typeOf(), path, formatResolver) +): DataTree = readDataDirectory(typeOf(), path, formatResolver) + +/** + * Read raw binary data tree from the directory. All files are read as-is (save for meta files). + */ +@DFExperimental +public fun IOPlugin.readRawDirectory( + path: Path, +): DataTree = readDataDirectory(path) { _, _ -> IOReader.binary } @OptIn(DFExperimental::class) @@ -205,6 +219,7 @@ public suspend fun IOPlugin.writeDataDirectory( is DataTreeItem.Node -> { writeDataDirectory(childPath, item.tree, format, envelopeFormat) } + is DataTreeItem.Leaf -> { val envelope = item.data.toEnvelope(format) if (envelopeFormat != null) {