From 8f5a20bb2285da73ea78697214925c94fd39619a Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sun, 31 Mar 2019 21:39:41 +0300 Subject: [PATCH] General IO builder --- .../commonMain/kotlin/hep/dataforge/io/Binary.kt | 6 ------ .../kotlin/hep/dataforge/io/BinaryMetaFormat.kt | 4 ++-- .../commonMain/kotlin/hep/dataforge/io/Envelope.kt | 13 +++++++++++-- .../commonMain/kotlin/hep/dataforge/io/IOFormat.kt | 10 ++++++++++ .../kotlin/hep/dataforge/io/JsonMetaFormat.kt | 4 ++-- .../kotlin/hep/dataforge/io/MetaFormat.kt | 9 ++++----- 6 files changed, 29 insertions(+), 17 deletions(-) delete mode 100644 dataforge-io/src/commonMain/kotlin/hep/dataforge/io/Binary.kt create mode 100644 dataforge-io/src/commonMain/kotlin/hep/dataforge/io/IOFormat.kt diff --git a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/Binary.kt b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/Binary.kt deleted file mode 100644 index 2b502f12..00000000 --- a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/Binary.kt +++ /dev/null @@ -1,6 +0,0 @@ -package hep.dataforge.io - -import kotlinx.io.core.Input - -//TODO replace by abstraction -typealias Binary = Input \ No newline at end of file diff --git a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/BinaryMetaFormat.kt b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/BinaryMetaFormat.kt index fc983072..0e73934a 100644 --- a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/BinaryMetaFormat.kt +++ b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/BinaryMetaFormat.kt @@ -8,8 +8,8 @@ import kotlinx.io.core.readText import kotlinx.io.core.writeText object BinaryMetaFormat : MetaFormat { - override fun write(meta: Meta, out: Output) { - out.writeMeta(meta) + override fun write(obj: Meta, out: Output) { + out.writeMeta(obj) } override fun read(input: Input): Meta { diff --git a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/Envelope.kt b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/Envelope.kt index 56a7e9be..1a9e58d7 100644 --- a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/Envelope.kt +++ b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/Envelope.kt @@ -3,10 +3,11 @@ package hep.dataforge.io import hep.dataforge.meta.Meta import hep.dataforge.meta.get import hep.dataforge.meta.string +import kotlinx.io.core.Input interface Envelope { val meta: Meta - val data: Binary? + val data: Input? companion object { @@ -18,9 +19,16 @@ interface Envelope { const val ENVELOPE_DATA_TYPE_KEY = "$ENVELOPE_NODE.dataType" const val ENVELOPE_DESCRIPTION_KEY = "$ENVELOPE_NODE.description" //const val ENVELOPE_TIME_KEY = "@envelope.time" + } } +class SimpleEnvelope(override val meta: Meta, val dataProvider: () -> Input?) : Envelope{ + override val data: Input? + get() = dataProvider() + +} + /** * The purpose of the envelope * @@ -40,4 +48,5 @@ val Envelope.dataType: String? get() = meta[Envelope.ENVELOPE_DATA_TYPE_KEY].str * * @return */ -val Envelope.description: String? get() = meta[Envelope.ENVELOPE_DESCRIPTION_KEY].string \ No newline at end of file +val Envelope.description: String? get() = meta[Envelope.ENVELOPE_DESCRIPTION_KEY].string + diff --git a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/IOFormat.kt b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/IOFormat.kt new file mode 100644 index 00000000..ce58c05d --- /dev/null +++ b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/IOFormat.kt @@ -0,0 +1,10 @@ +package hep.dataforge.io + +import kotlinx.io.core.Input +import kotlinx.io.core.Output + + +interface IOFormat { + fun write(obj: T, out: Output) + fun read(input: Input): T +} \ No newline at end of file diff --git a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/JsonMetaFormat.kt b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/JsonMetaFormat.kt index 6b53a2fa..bd6fa81a 100644 --- a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/JsonMetaFormat.kt +++ b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/JsonMetaFormat.kt @@ -14,8 +14,8 @@ import kotlinx.serialization.json.* object JsonMetaFormat : MetaFormat { - override fun write(meta: Meta, out: Output) { - val str = meta.toJson().toString() + override fun write(obj: Meta, out: Output) { + val str = obj.toJson().toString() out.writeText(str) } diff --git a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/MetaFormat.kt b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/MetaFormat.kt index 50e91248..61969536 100644 --- a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/MetaFormat.kt +++ b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/MetaFormat.kt @@ -1,15 +1,14 @@ package hep.dataforge.io import hep.dataforge.meta.Meta -import kotlinx.io.core.* +import kotlinx.io.core.BytePacketBuilder +import kotlinx.io.core.ByteReadPacket +import kotlinx.io.core.toByteArray /** * A format for meta serialization */ -interface MetaFormat { - fun write(meta: Meta, out: Output) - fun read(input: Input): Meta -} +interface MetaFormat: IOFormat /** * ServiceLoader compatible factory