General IO builder

This commit is contained in:
Alexander Nozik 2019-03-31 21:39:41 +03:00
parent 5480d90a36
commit 8f5a20bb22
6 changed files with 29 additions and 17 deletions

View File

@ -1,6 +0,0 @@
package hep.dataforge.io
import kotlinx.io.core.Input
//TODO replace by abstraction
typealias Binary = Input

View File

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

View File

@ -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
val Envelope.description: String? get() = meta[Envelope.ENVELOPE_DESCRIPTION_KEY].string

View File

@ -0,0 +1,10 @@
package hep.dataforge.io
import kotlinx.io.core.Input
import kotlinx.io.core.Output
interface IOFormat<T : Any> {
fun write(obj: T, out: Output)
fun read(input: Input): T
}

View File

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

View File

@ -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<Meta>
/**
* ServiceLoader compatible factory