From ade4eec96ea19f07ef833602412716e43a6f1cdc Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Wed, 30 Oct 2019 17:13:31 +0300 Subject: [PATCH 1/9] Make putted values optional in MetaBuilder --- .../kotlin/hep/dataforge/meta/MetaBuilder.kt | 60 +++++++++---------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/MetaBuilder.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/MetaBuilder.kt index 671d1f13..c5ad3831 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/MetaBuilder.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/MetaBuilder.kt @@ -15,55 +15,55 @@ class MetaBuilder : AbstractMutableMeta() { override fun wrapNode(meta: Meta): MetaBuilder = if (meta is MetaBuilder) meta else meta.builder() override fun empty(): MetaBuilder = MetaBuilder() - infix fun String.put(value: Value){ - set(this,value) + infix fun String.put(value: Value?) { + set(this, value) } - infix fun String.put(string: String){ - set(this,string.asValue()) + infix fun String.put(string: String?) { + set(this, string?.asValue()) } - infix fun String.put(number: Number){ - set(this,number.asValue()) + infix fun String.put(number: Number?) { + set(this, number?.asValue()) } - infix fun String.put(boolean: Boolean){ - set(this, boolean.asValue()) + infix fun String.put(boolean: Boolean?) { + set(this, boolean?.asValue()) } - infix fun String.put(enum: Enum<*>){ + infix fun String.put(enum: Enum<*>) { set(this, EnumValue(enum)) } @JvmName("putValues") - infix fun String.put(iterable: Iterable){ + infix fun String.put(iterable: Iterable) { set(this, iterable.asValue()) } @JvmName("putNumbers") - infix fun String.put(iterable: Iterable){ + infix fun String.put(iterable: Iterable) { set(this, iterable.map { it.asValue() }.asValue()) } @JvmName("putStrings") - infix fun String.put(iterable: Iterable){ + infix fun String.put(iterable: Iterable) { set(this, iterable.map { it.asValue() }.asValue()) } - infix fun String.put(array: DoubleArray){ + infix fun String.put(array: DoubleArray) { set(this, array.asValue()) } - infix fun String.putValue(any: Any?){ + infix fun String.putValue(any: Any?) { set(this, Value.of(any)) } - infix fun String.put(meta: Meta) { + infix fun String.put(meta: Meta?) { this@MetaBuilder[this] = meta } - infix fun String.put(repr: MetaRepr){ - set(this,repr.toMeta()) + infix fun String.put(repr: MetaRepr?) { + set(this, repr?.toMeta()) } @JvmName("putMetas") @@ -75,37 +75,37 @@ class MetaBuilder : AbstractMutableMeta() { this@MetaBuilder[this] = MetaBuilder().apply(metaBuilder) } - infix fun Name.put(value: Value){ - set(this,value) + infix fun Name.put(value: Value?) { + set(this, value) } - infix fun Name.put(string: String){ - set(this,string.asValue()) + infix fun Name.put(string: String?) { + set(this, string?.asValue()) } - infix fun Name.put(number: Number){ - set(this,number.asValue()) + infix fun Name.put(number: Number?) { + set(this, number?.asValue()) } - infix fun Name.put(boolean: Boolean){ - set(this, boolean.asValue()) + infix fun Name.put(boolean: Boolean?) { + set(this, boolean?.asValue()) } - infix fun Name.put(enum: Enum<*>){ + infix fun Name.put(enum: Enum<*>) { set(this, EnumValue(enum)) } @JvmName("putValues") - infix fun Name.put(iterable: Iterable){ + infix fun Name.put(iterable: Iterable) { set(this, iterable.asValue()) } - infix fun Name.put(meta: Meta) { + infix fun Name.put(meta: Meta?) { this@MetaBuilder[this] = meta } - infix fun Name.put(repr: MetaRepr){ - set(this,repr.toMeta()) + infix fun Name.put(repr: MetaRepr?) { + set(this, repr?.toMeta()) } @JvmName("putMetas") From 33c8e7088e3e0b22e56e1ac7cbca5ab1b4ac401d Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Wed, 30 Oct 2019 19:43:26 +0300 Subject: [PATCH 2/9] Fixed leaked byte packets in io --- .../kotlin/hep/dataforge/io/Binary.kt | 11 ++-- .../kotlin/hep/dataforge/io/MetaFormat.kt | 2 +- .../hep/dataforge/io/TaggedEnvelopeFormat.kt | 11 ++-- .../hep/dataforge/io/TaglessEnvelopeFormat.kt | 10 +++- .../kotlin/hep/dataforge/io/FileBinary.kt | 4 +- .../kotlin/hep/dataforge/io/FileEnvelope.kt | 2 +- .../kotlin/hep/dataforge/io/FileBinaryTest.kt | 56 +++++++++++++++++++ 7 files changed, 79 insertions(+), 17 deletions(-) create mode 100644 dataforge-io/src/jvmTest/kotlin/hep/dataforge/io/FileBinaryTest.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 index 252de7bc..ca05de4d 100644 --- a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/Binary.kt +++ b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/Binary.kt @@ -1,9 +1,6 @@ package hep.dataforge.io -import kotlinx.io.core.ByteReadPacket -import kotlinx.io.core.Input -import kotlinx.io.core.buildPacket -import kotlinx.io.core.readBytes +import kotlinx.io.core.* import kotlin.math.min /** @@ -45,7 +42,7 @@ fun Binary.toBytes(): ByteArray = read { @ExperimentalUnsignedTypes fun RandomAccessBinary.readPacket(from: UInt, size: UInt): ByteReadPacket = read(from, size) { - ByteReadPacket(this.readBytes()) + buildPacket { copyTo(this) } } @ExperimentalUnsignedTypes @@ -65,7 +62,9 @@ inline class ArrayBinary(val array: ByteArray) : RandomAccessBinary { override fun read(from: UInt, size: UInt, block: Input.() -> R): R { val theSize = min(size, array.size.toUInt() - from) - return ByteReadPacket(array, from.toInt(), theSize.toInt()).block() + return buildPacket { + writeFully(array, from.toInt(), theSize.toInt()) + }.block() } } 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 313d04ae..ca9a53a2 100644 --- a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/MetaFormat.kt +++ b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/MetaFormat.kt @@ -52,7 +52,7 @@ fun Meta.toBytes(format: MetaFormat = JsonMetaFormat.default): ByteReadPacket = } fun MetaFormat.parse(str: String): Meta { - return ByteReadPacket(str.toByteArray()).readObject() + return buildPacket { writeText(str) }.readObject() } fun MetaFormatFactory.parse(str: String): Meta = invoke().parse(str) diff --git a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/TaggedEnvelopeFormat.kt b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/TaggedEnvelopeFormat.kt index b69a5f19..c51cb36d 100644 --- a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/TaggedEnvelopeFormat.kt +++ b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/TaggedEnvelopeFormat.kt @@ -7,6 +7,7 @@ import hep.dataforge.meta.string import hep.dataforge.names.Name import hep.dataforge.names.plus import hep.dataforge.names.toName +import kotlinx.io.charsets.Charsets import kotlinx.io.core.* @ExperimentalUnsignedTypes @@ -30,10 +31,12 @@ class TaggedEnvelopeFormat( override fun Output.writeObject(obj: Envelope) { val metaBytes = metaFormat.writeBytes(obj.meta) - val tag = Tag(metaFormatKey, metaBytes.size.toUInt(), obj.data?.size ?: 0.toULong()) + val tag = Tag(metaFormatKey, metaBytes.size.toUInt() + 2u, obj.data?.size ?: 0.toULong()) writePacket(tag.toBytes()) writeFully(metaBytes) + writeText("\r\n") obj.data?.read { copyTo(this@writeObject) } + flush() } /** @@ -92,14 +95,14 @@ class TaggedEnvelopeFormat( } private fun Input.readTag(): Tag { - val start = readTextExactBytes(2) + val start = readTextExactBytes(2, charset = Charsets.ISO_8859_1) if (start != START_SEQUENCE) error("The input is not an envelope") - val version = readTextExactBytes(4) + val version = readTextExactBytes(4, charset = Charsets.ISO_8859_1) if (version != VERSION) error("Wrong version of DataForge: expected $VERSION but found $version") val metaFormatKey = readShort() val metaLength = readUInt() val dataLength = readULong() - val end = readTextExactBytes(4) + val end = readTextExactBytes(4, charset = Charsets.ISO_8859_1) if (end != END_SEQUENCE) error("The input is not an envelope") return Tag(metaFormatKey, metaLength, dataLength) } diff --git a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/TaglessEnvelopeFormat.kt b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/TaglessEnvelopeFormat.kt index 83fbe46b..8e7c9e2b 100644 --- a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/TaglessEnvelopeFormat.kt +++ b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/TaglessEnvelopeFormat.kt @@ -72,7 +72,9 @@ class TaglessEnvelopeFormat( val metaFormat = properties[META_TYPE_PROPERTY]?.let { io.metaFormat(it) } ?: JsonMetaFormat.default val metaSize = properties.get(META_LENGTH_PROPERTY)?.toInt() meta = if (metaSize != null) { - val metaPacket = ByteReadPacket(readBytes(metaSize)) + val metaPacket = buildPacket { + writeFully(readBytes(metaSize)) + } metaFormat.run { metaPacket.readObject() } } else { metaFormat.run { @@ -126,11 +128,13 @@ class TaglessEnvelopeFormat( val metaSize = properties.get(META_LENGTH_PROPERTY)?.toInt() meta = if (metaSize != null) { - val metaPacket = ByteReadPacket(readBytes(metaSize)) + val metaPacket = buildPacket { + writeFully(readBytes(metaSize)) + } offset += metaSize.toUInt() metaFormat.run { metaPacket.readObject() } } else { - error("Can't partially read an envelope with undefined meta size") + error("Can't partially read an envelope with undefined meta size") } } diff --git a/dataforge-io/src/jvmMain/kotlin/hep/dataforge/io/FileBinary.kt b/dataforge-io/src/jvmMain/kotlin/hep/dataforge/io/FileBinary.kt index 2c357a47..b935a103 100644 --- a/dataforge-io/src/jvmMain/kotlin/hep/dataforge/io/FileBinary.kt +++ b/dataforge-io/src/jvmMain/kotlin/hep/dataforge/io/FileBinary.kt @@ -1,7 +1,7 @@ package hep.dataforge.io -import kotlinx.io.core.ByteReadPacket import kotlinx.io.core.Input +import kotlinx.io.core.buildPacket import java.nio.channels.FileChannel import java.nio.file.Files import java.nio.file.Path @@ -17,7 +17,7 @@ class FileBinary(val path: Path, private val offset: UInt = 0u, size: ULong? = n FileChannel.open(path, StandardOpenOption.READ).use { val theSize: UInt = min(size, Files.size(path).toUInt() - offset) val buffer = it.map(FileChannel.MapMode.READ_ONLY, (from + offset).toLong(), theSize.toLong()) - return ByteReadPacket(buffer).block() + return buildPacket { writeFully(buffer) }.block() } } } diff --git a/dataforge-io/src/jvmMain/kotlin/hep/dataforge/io/FileEnvelope.kt b/dataforge-io/src/jvmMain/kotlin/hep/dataforge/io/FileEnvelope.kt index dcc8685d..3187cd54 100644 --- a/dataforge-io/src/jvmMain/kotlin/hep/dataforge/io/FileEnvelope.kt +++ b/dataforge-io/src/jvmMain/kotlin/hep/dataforge/io/FileEnvelope.kt @@ -15,7 +15,7 @@ class FileEnvelope internal constructor(val path: Path, val format: EnvelopeForm init { val input = Files.newByteChannel(path, StandardOpenOption.READ).asInput() - partialEnvelope = format.run { input.readPartial() } + partialEnvelope = format.run { input.use { it.readPartial()} } } override val meta: Meta get() = partialEnvelope.meta diff --git a/dataforge-io/src/jvmTest/kotlin/hep/dataforge/io/FileBinaryTest.kt b/dataforge-io/src/jvmTest/kotlin/hep/dataforge/io/FileBinaryTest.kt new file mode 100644 index 00000000..94403dcd --- /dev/null +++ b/dataforge-io/src/jvmTest/kotlin/hep/dataforge/io/FileBinaryTest.kt @@ -0,0 +1,56 @@ +package hep.dataforge.io + +import hep.dataforge.context.Global +import java.nio.file.Files +import kotlin.test.Test +import kotlin.test.assertEquals + +class FileBinaryTest { + val envelope = Envelope { + meta { + "a" put "AAA" + "b" put 22.2 + } + dataType = "hep.dataforge.test" + dataID = "myData" // добавил только что + data { + writeDouble(16.7) + } + } + + @Test + fun testSize() { + val binary = envelope.data + assertEquals(binary?.size?.toInt(), binary?.toBytes()?.size) + } + + @Test + fun testFileData(){ + val dataFile = Files.createTempFile("dataforge_test_bin", ".bin") + dataFile.toFile().writeText("This is my binary") + val envelopeFromFile = Envelope { + meta { + "a" put "AAA" + "b" put 22.2 + } + dataType = "hep.dataforge.satellite" + dataID = "cellDepositTest" // добавил только что + data = dataFile.asBinary() + } + val binary = envelopeFromFile.data!! + println(binary.toBytes().size) + assertEquals(binary.size.toInt(), binary.toBytes().size) + + } + + @Test + fun testFileDataSizeRewriting() { + println(System.getProperty("user.dir")) + val tmpPath = Files.createTempFile("dataforge_test", ".df") + Global.io.writeEnvelopeFile(tmpPath, envelope) + + val binary = Global.io.readEnvelopeFile(tmpPath).data!! + assertEquals(binary.size.toInt(), binary.toBytes().size) + } + +} \ No newline at end of file From 0ef0a430773c3a3279f05484daf4196491cd21a8 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Wed, 30 Oct 2019 19:57:03 +0300 Subject: [PATCH 3/9] Added check for file size in FileBinary --- .../src/jvmMain/kotlin/hep/dataforge/io/FileBinary.kt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/dataforge-io/src/jvmMain/kotlin/hep/dataforge/io/FileBinary.kt b/dataforge-io/src/jvmMain/kotlin/hep/dataforge/io/FileBinary.kt index b935a103..aa90a638 100644 --- a/dataforge-io/src/jvmMain/kotlin/hep/dataforge/io/FileBinary.kt +++ b/dataforge-io/src/jvmMain/kotlin/hep/dataforge/io/FileBinary.kt @@ -13,6 +13,12 @@ class FileBinary(val path: Path, private val offset: UInt = 0u, size: ULong? = n override val size: ULong = size ?: (Files.size(path).toULong() - offset).toULong() + init { + if( size != null && Files.size(path) < offset.toLong() + size.toLong()){ + error("Can't read binary from file. File is to short.") + } + } + override fun read(from: UInt, size: UInt, block: Input.() -> R): R { FileChannel.open(path, StandardOpenOption.READ).use { val theSize: UInt = min(size, Files.size(path).toUInt() - offset) From 6e98bc7408e7acc14df9b67736c6cb4306e2b5c4 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Thu, 31 Oct 2019 10:21:52 +0300 Subject: [PATCH 4/9] Added descriptors to custom serializers --- .../io/serialization/MetaSerializer.kt | 13 ++-- .../io/serialization/serializationUtils.kt | 59 ++++++++++++++++--- .../hep/dataforge/io/MetaSerializerTest.kt | 5 ++ 3 files changed, 64 insertions(+), 13 deletions(-) diff --git a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/serialization/MetaSerializer.kt b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/serialization/MetaSerializer.kt index 2f6a00de..271f95e2 100644 --- a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/serialization/MetaSerializer.kt +++ b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/serialization/MetaSerializer.kt @@ -14,12 +14,12 @@ import kotlinx.serialization.json.JsonOutput @Serializer(Value::class) object ValueSerializer : KSerializer { private val valueTypeSerializer = EnumSerializer(ValueType::class) - private val listSerializer by lazy { ArrayListSerializer(ValueSerializer)} + private val listSerializer by lazy { ArrayListSerializer(ValueSerializer) } override val descriptor: SerialDescriptor = descriptor("Value") { - element("isList") - element("valueType") - element("value") + boolean("isList") + enum("valueType") + element("value", PolymorphicClassDescriptor) } private fun Decoder.decodeValue(): Value { @@ -68,10 +68,11 @@ object ValueSerializer : KSerializer { @Serializer(MetaItem::class) object MetaItemSerializer : KSerializer> { override val descriptor: SerialDescriptor = descriptor("MetaItem") { - element("isNode") - element("value") + boolean("isNode") + element("value", PolymorphicClassDescriptor) } + override fun deserialize(decoder: Decoder): MetaItem<*> { val isNode = decoder.decodeBoolean() return if (isNode) { diff --git a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/serialization/serializationUtils.kt b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/serialization/serializationUtils.kt index f83b2197..4d33fad9 100644 --- a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/serialization/serializationUtils.kt +++ b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/serialization/serializationUtils.kt @@ -4,19 +4,64 @@ import kotlinx.serialization.CompositeDecoder import kotlinx.serialization.Decoder import kotlinx.serialization.KSerializer import kotlinx.serialization.SerialDescriptor -import kotlinx.serialization.internal.SerialClassDescImpl +import kotlinx.serialization.internal.* +/** + * A convenience builder for serial descriptors + */ inline class SerialDescriptorBuilder(private val impl: SerialClassDescImpl) { - fun element(name: String, isOptional: Boolean = false) = impl.addElement(name, isOptional) + fun element( + name: String, + descriptor: SerialDescriptor, + isOptional: Boolean = false, + vararg annotations: Annotation + ) { + impl.addElement(name, isOptional) + impl.pushDescriptor(descriptor) + annotations.forEach { + impl.pushAnnotation(it) + } + } - fun annotation(a: Annotation) = impl.pushAnnotation(a) + fun element( + name: String, + isOptional: Boolean = false, + vararg annotations: Annotation, + block: SerialDescriptorBuilder.() -> Unit + ) { + impl.addElement(name, isOptional) + impl.pushDescriptor(SerialDescriptorBuilder(SerialClassDescImpl(name)).apply(block).build()) + annotations.forEach { + impl.pushAnnotation(it) + } + } + + fun boolean(name: String, isOptional: Boolean = false, vararg annotations: Annotation) = + element(name, BooleanDescriptor, isOptional, *annotations) + + fun string(name: String, isOptional: Boolean = false, vararg annotations: Annotation) = + element(name, StringDescriptor, isOptional, *annotations) + + fun int(name: String, isOptional: Boolean = false, vararg annotations: Annotation) = + element(name, IntDescriptor, isOptional, *annotations) + + fun double(name: String, isOptional: Boolean = false, vararg annotations: Annotation) = + element(name, DoubleDescriptor, isOptional, *annotations) + + fun float(name: String, isOptional: Boolean = false, vararg annotations: Annotation) = + element(name, FloatDescriptor, isOptional, *annotations) + + fun long(name: String, isOptional: Boolean = false, vararg annotations: Annotation) = + element(name, LongDescriptor, isOptional, *annotations) + + fun doubleArray(name: String, isOptional: Boolean = false, vararg annotations: Annotation) = + element(name, DoubleArraySerializer.descriptor, isOptional, *annotations) + + inline fun > enum(name: String, isOptional: Boolean = false, vararg annotations: Annotation) = + element(name, EnumSerializer(E::class).descriptor, isOptional, *annotations) fun classAnnotation(a: Annotation) = impl.pushClassAnnotation(a) - fun descriptor(name: String, block: SerialDescriptorBuilder.() -> Unit) = impl.pushDescriptor( - SerialDescriptorBuilder(SerialClassDescImpl(name)).apply(block).build() - ) - fun build(): SerialDescriptor = impl } diff --git a/dataforge-io/src/commonTest/kotlin/hep/dataforge/io/MetaSerializerTest.kt b/dataforge-io/src/commonTest/kotlin/hep/dataforge/io/MetaSerializerTest.kt index ba0ed7c7..f33ad894 100644 --- a/dataforge-io/src/commonTest/kotlin/hep/dataforge/io/MetaSerializerTest.kt +++ b/dataforge-io/src/commonTest/kotlin/hep/dataforge/io/MetaSerializerTest.kt @@ -51,4 +51,9 @@ class MetaSerializerTest { val restored = Json.plain.parse(NameSerializer, string) assertEquals(restored, name) } + + @Test + fun testMetaItemDescriptor(){ + val descriptor = MetaItemSerializer.descriptor.getElementDescriptor(0) + } } \ No newline at end of file From 592f911db7a8793d4e7e1f38c35071287e4edec6 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Fri, 1 Nov 2019 14:41:16 +0300 Subject: [PATCH 5/9] Changed map and reduce action API to include both meta from data and meta from task/action --- .../kotlin/hep/dataforge/data/MapAction.kt | 27 +++++++++++++------ .../kotlin/hep/dataforge/data/ReduceAction.kt | 15 ++++++----- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/MapAction.kt b/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/MapAction.kt index b184a2a8..ea24ca77 100644 --- a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/MapAction.kt +++ b/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/MapAction.kt @@ -1,16 +1,19 @@ package hep.dataforge.data -import hep.dataforge.meta.* +import hep.dataforge.meta.Meta +import hep.dataforge.meta.MetaBuilder +import hep.dataforge.meta.builder +import hep.dataforge.meta.seal import hep.dataforge.names.Name import kotlin.reflect.KClass -class ActionEnv(val name: Name, val meta: Meta) +data class ActionEnv(val name: Name, val actionMeta: Meta, val meta: Meta) /** * Action environment */ -class MapActionBuilder(var name: Name, var meta: MetaBuilder) { +class MapActionBuilder(var name: Name, var meta: MetaBuilder, val actionMeta: Meta) { lateinit var result: suspend ActionEnv.(T) -> R /** @@ -33,16 +36,24 @@ class MapAction( return DataNode.invoke(outputType) { node.dataSequence().forEach { (name, data) -> - //merging data meta with action meta (data meta is primary) - val oldMeta = meta.builder().apply { update(data.meta) } - // creating environment from old meta and name - val env = ActionEnv(name, oldMeta) + /* + * Creating a new environment for action using **old** name, old meta and task meta + */ + val env = ActionEnv(name, meta, data.meta) + //applying transformation from builder - val builder = MapActionBuilder(name, oldMeta).apply(block) + val builder = MapActionBuilder( + name, + data.meta.builder(), // using data meta + meta + ).apply(block) + //getting new name val newName = builder.name + //getting new meta val newMeta = builder.meta.seal() + val newData = data.map(outputType, meta = newMeta) { builder.result(env, it) } //setting the data node this[newName] = newData diff --git a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/ReduceAction.kt b/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/ReduceAction.kt index 48738452..1ce6d3c2 100644 --- a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/ReduceAction.kt +++ b/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/ReduceAction.kt @@ -1,9 +1,7 @@ package hep.dataforge.data -import hep.dataforge.meta.Laminate import hep.dataforge.meta.Meta import hep.dataforge.meta.MetaBuilder -import hep.dataforge.meta.builder import hep.dataforge.names.Name import hep.dataforge.names.toName import kotlin.reflect.KClass @@ -84,15 +82,20 @@ class ReduceAction( return DataNode.invoke(outputType) { ReduceGroupBuilder(meta).apply(action).buildGroups(node).forEach { group -> - val laminate = Laminate(group.meta, meta) + //val laminate = Laminate(group.meta, meta) val dataMap = group.node.dataSequence().associate { it } - val groupName: String = group.name; + val groupName: String = group.name - val env = ActionEnv(groupName.toName(), laminate.builder()) + val groupMeta = group.meta - val res: DynamicData = dataMap.reduce(outputType, meta = laminate) { group.result.invoke(env, it) } + val env = ActionEnv(groupName.toName(), meta, groupMeta) + + val res: DynamicData = dataMap.reduce( + outputType, + meta = groupMeta + ) { group.result.invoke(env, it) } set(env.name, res) } From 257601d945a55ef225fce2aafdc9d5a28aece1c1 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Fri, 1 Nov 2019 14:53:58 +0300 Subject: [PATCH 6/9] Changed parameter order in ActionEnv --- .../commonMain/kotlin/hep/dataforge/data/MapAction.kt | 11 +++++++++-- .../kotlin/hep/dataforge/data/ReduceAction.kt | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/MapAction.kt b/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/MapAction.kt index ea24ca77..8c543927 100644 --- a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/MapAction.kt +++ b/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/MapAction.kt @@ -7,7 +7,14 @@ import hep.dataforge.meta.seal import hep.dataforge.names.Name import kotlin.reflect.KClass -data class ActionEnv(val name: Name, val actionMeta: Meta, val meta: Meta) +/** + * Action environment includes data name, data meta and action configuration meta + */ +data class ActionEnv( + val name: Name, + val meta: Meta, + val actionMeta: Meta +) /** @@ -39,7 +46,7 @@ class MapAction( /* * Creating a new environment for action using **old** name, old meta and task meta */ - val env = ActionEnv(name, meta, data.meta) + val env = ActionEnv(name, data.meta, meta) //applying transformation from builder val builder = MapActionBuilder( diff --git a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/ReduceAction.kt b/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/ReduceAction.kt index 1ce6d3c2..9bb49151 100644 --- a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/ReduceAction.kt +++ b/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/ReduceAction.kt @@ -90,7 +90,7 @@ class ReduceAction( val groupMeta = group.meta - val env = ActionEnv(groupName.toName(), meta, groupMeta) + val env = ActionEnv(groupName.toName(), groupMeta, meta) val res: DynamicData = dataMap.reduce( outputType, From 6f341f705adc134c9484252671388da7696ee5dc Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Fri, 1 Nov 2019 19:47:03 +0300 Subject: [PATCH 7/9] Added DF02 format to TaggedEnvelopeFormat. --- .../hep/dataforge/io/TaggedEnvelopeFormat.kt | 50 +++++++++++++------ 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/TaggedEnvelopeFormat.kt b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/TaggedEnvelopeFormat.kt index c51cb36d..97a23fb9 100644 --- a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/TaggedEnvelopeFormat.kt +++ b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/TaggedEnvelopeFormat.kt @@ -13,7 +13,8 @@ import kotlinx.io.core.* @ExperimentalUnsignedTypes class TaggedEnvelopeFormat( val io: IOPlugin, - private val metaFormatKey: Short + private val metaFormatKey: Short, + val version: VERSION = TaggedEnvelopeFormat.VERSION.DF02 ) : EnvelopeFormat { private val metaFormat = io.metaFormat(metaFormatKey) @@ -22,10 +23,17 @@ class TaggedEnvelopeFormat( private fun Tag.toBytes(): ByteReadPacket = buildPacket(24) { writeText(START_SEQUENCE) - writeText(VERSION) + writeText(version.name) writeShort(metaFormatKey) writeUInt(metaSize) - writeULong(dataSize) + when (version) { + TaggedEnvelopeFormat.VERSION.DF02 -> { + writeUInt(dataSize.toUInt()) + } + TaggedEnvelopeFormat.VERSION.DF03 -> { + writeULong(dataSize) + } + } writeText(END_SEQUENCE) } @@ -46,7 +54,7 @@ class TaggedEnvelopeFormat( * @param formats a collection of meta formats to resolve */ override fun Input.readObject(): Envelope { - val tag = readTag() + val tag = readTag(version) val metaFormat = io.metaFormat(tag.metaFormatKey) ?: error("Meta format with key ${tag.metaFormatKey} not found") @@ -59,7 +67,7 @@ class TaggedEnvelopeFormat( } override fun Input.readPartial(): PartialEnvelope { - val tag = readTag() + val tag = readTag(version) val metaFormat = io.metaFormat(tag.metaFormatKey) ?: error("Meta format with key ${tag.metaFormatKey} not found") @@ -67,7 +75,7 @@ class TaggedEnvelopeFormat( val metaPacket = ByteReadPacket(readBytes(tag.metaSize.toInt())) val meta = metaFormat.run { metaPacket.readObject() } - return PartialEnvelope(meta, TAG_SIZE + tag.metaSize, tag.dataSize) + return PartialEnvelope(meta, version.tagSize + tag.metaSize, tag.dataSize) } private data class Tag( @@ -76,13 +84,16 @@ class TaggedEnvelopeFormat( val dataSize: ULong ) + enum class VERSION(val tagSize: UInt) { + DF02(20u), + DF03(24u) + } + companion object : EnvelopeFormatFactory { - const val VERSION = "DF03" private const val START_SEQUENCE = "#~" private const val END_SEQUENCE = "~#\r\n" - private const val TAG_SIZE = 24u - override val name: Name = super.name + VERSION + override val name: Name = super.name + "tagged" override fun invoke(meta: Meta, context: Context): EnvelopeFormat { val io = context.io @@ -94,14 +105,17 @@ class TaggedEnvelopeFormat( return TaggedEnvelopeFormat(io, metaFormatFactory.key) } - private fun Input.readTag(): Tag { + private fun Input.readTag(version: VERSION): Tag { val start = readTextExactBytes(2, charset = Charsets.ISO_8859_1) if (start != START_SEQUENCE) error("The input is not an envelope") - val version = readTextExactBytes(4, charset = Charsets.ISO_8859_1) - if (version != VERSION) error("Wrong version of DataForge: expected $VERSION but found $version") + val versionString = readTextExactBytes(4, charset = Charsets.ISO_8859_1) + if (version.name != versionString) error("Wrong version of DataForge: expected $version but found $versionString") val metaFormatKey = readShort() val metaLength = readUInt() - val dataLength = readULong() + val dataLength: ULong = when (version) { + VERSION.DF02 -> readUInt().toULong() + VERSION.DF03 -> readULong() + } val end = readTextExactBytes(4, charset = Charsets.ISO_8859_1) if (end != END_SEQUENCE) error("The input is not an envelope") return Tag(metaFormatKey, metaLength, dataLength) @@ -109,14 +123,18 @@ class TaggedEnvelopeFormat( override fun peekFormat(io: IOPlugin, input: Input): EnvelopeFormat? { return try { - val tag = input.readTag() - TaggedEnvelopeFormat(io, tag.metaFormatKey) + val header = input.readTextExactBytes(6) + when(header.substring(2..5)){ + VERSION.DF02.name-> TaggedEnvelopeFormat(io, JsonMetaFormat.key,VERSION.DF02) + VERSION.DF03.name-> TaggedEnvelopeFormat(io, JsonMetaFormat.key,VERSION.DF03) + else -> null + } } catch (ex: Exception) { null } } - val default by lazy { invoke()} + val default by lazy { invoke() } } } \ No newline at end of file From 33b1de286596dbb482ecdd951de3a73061a1e005 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Fri, 1 Nov 2019 20:04:11 +0300 Subject: [PATCH 8/9] Optimized EnvelopeFormat generation API --- .../io/yaml/FrontMatterEnvelopeFormat.kt | 18 +++++++-------- .../kotlin/hep/dataforge/io/EnvelopeFormat.kt | 7 +++++- .../hep/dataforge/io/TaggedEnvelopeFormat.kt | 22 +++++++++---------- .../hep/dataforge/io/TaglessEnvelopeFormat.kt | 20 +++++++---------- 4 files changed, 33 insertions(+), 34 deletions(-) diff --git a/dataforge-io/dataforge-io-yaml/src/main/kotlin/hep/dataforge/io/yaml/FrontMatterEnvelopeFormat.kt b/dataforge-io/dataforge-io-yaml/src/main/kotlin/hep/dataforge/io/yaml/FrontMatterEnvelopeFormat.kt index 9306b336..db701625 100644 --- a/dataforge-io/dataforge-io-yaml/src/main/kotlin/hep/dataforge/io/yaml/FrontMatterEnvelopeFormat.kt +++ b/dataforge-io/dataforge-io-yaml/src/main/kotlin/hep/dataforge/io/yaml/FrontMatterEnvelopeFormat.kt @@ -2,20 +2,18 @@ package hep.dataforge.io.yaml import hep.dataforge.context.Context import hep.dataforge.io.* -import hep.dataforge.meta.* +import hep.dataforge.meta.DFExperimental +import hep.dataforge.meta.EmptyMeta +import hep.dataforge.meta.Meta import kotlinx.io.core.* import kotlinx.serialization.toUtf8Bytes @DFExperimental class FrontMatterEnvelopeFormat( val io: IOPlugin, - val metaType: String = YamlMetaFormat.name.toString(), meta: Meta = EmptyMeta ) : EnvelopeFormat { - val metaFormat = io.metaFormat(metaType, meta) - ?: error("Meta format with type $metaType could not be resolved in $io") - override fun Input.readPartial(): PartialEnvelope { var line: String = "" var offset = 0u @@ -60,11 +58,12 @@ class FrontMatterEnvelopeFormat( return SimpleEnvelope(meta, data) } - override fun Output.writeObject(obj: Envelope) { + override fun Output.writeEnvelope(envelope: Envelope, metaFormatFactory: MetaFormatFactory, formatMeta: Meta) { + val metaFormat = metaFormatFactory(formatMeta, io.context) writeText("$SEPARATOR\r\n") - metaFormat.run { writeObject(obj.meta) } + metaFormat.run { writeObject(envelope.meta) } writeText("$SEPARATOR\r\n") - obj.data?.read { copyTo(this@writeObject) } + envelope.data?.read { copyTo(this@writeEnvelope) } } companion object : EnvelopeFormatFactory { @@ -73,8 +72,7 @@ class FrontMatterEnvelopeFormat( private val metaTypeRegex = "---(\\w*)\\s*".toRegex() override fun invoke(meta: Meta, context: Context): EnvelopeFormat { - val metaFormatName: String = meta["name"].string ?: YamlMetaFormat.name.toString() - return FrontMatterEnvelopeFormat(context.io, metaFormatName, meta) + return FrontMatterEnvelopeFormat(context.io, meta) } override fun peekFormat(io: IOPlugin, input: Input): EnvelopeFormat? { diff --git a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/EnvelopeFormat.kt b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/EnvelopeFormat.kt index 4c5c5839..c52b9e1d 100644 --- a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/EnvelopeFormat.kt +++ b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/EnvelopeFormat.kt @@ -2,6 +2,7 @@ package hep.dataforge.io import hep.dataforge.context.Context import hep.dataforge.io.EnvelopeFormatFactory.Companion.ENVELOPE_FORMAT_TYPE +import hep.dataforge.meta.EmptyMeta import hep.dataforge.meta.Meta import hep.dataforge.names.Name import hep.dataforge.names.asName @@ -18,11 +19,15 @@ data class PartialEnvelope(val meta: Meta, val dataOffset: UInt, val dataSize: U interface EnvelopeFormat : IOFormat { + val defaultMetaFormat: MetaFormatFactory get() = JsonMetaFormat + fun Input.readPartial(): PartialEnvelope + fun Output.writeEnvelope(envelope: Envelope, metaFormatFactory: MetaFormatFactory, formatMeta: Meta = EmptyMeta) + override fun Input.readObject(): Envelope - override fun Output.writeObject(obj: Envelope) + override fun Output.writeObject(obj: Envelope): Unit = writeEnvelope(obj, defaultMetaFormat) } @Type(ENVELOPE_FORMAT_TYPE) diff --git a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/TaggedEnvelopeFormat.kt b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/TaggedEnvelopeFormat.kt index 97a23fb9..cce3eade 100644 --- a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/TaggedEnvelopeFormat.kt +++ b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/TaggedEnvelopeFormat.kt @@ -13,12 +13,11 @@ import kotlinx.io.core.* @ExperimentalUnsignedTypes class TaggedEnvelopeFormat( val io: IOPlugin, - private val metaFormatKey: Short, val version: VERSION = TaggedEnvelopeFormat.VERSION.DF02 ) : EnvelopeFormat { - private val metaFormat = io.metaFormat(metaFormatKey) - ?: error("Meta format with key $metaFormatKey could not be resolved in $io") +// private val metaFormat = io.metaFormat(metaFormatKey) +// ?: error("Meta format with key $metaFormatKey could not be resolved in $io") private fun Tag.toBytes(): ByteReadPacket = buildPacket(24) { @@ -37,13 +36,14 @@ class TaggedEnvelopeFormat( writeText(END_SEQUENCE) } - override fun Output.writeObject(obj: Envelope) { - val metaBytes = metaFormat.writeBytes(obj.meta) - val tag = Tag(metaFormatKey, metaBytes.size.toUInt() + 2u, obj.data?.size ?: 0.toULong()) + override fun Output.writeEnvelope(envelope: Envelope, metaFormatFactory: MetaFormatFactory, formatMeta: Meta) { + val metaFormat = metaFormatFactory.invoke(formatMeta, io.context) + val metaBytes = metaFormat.writeBytes(envelope.meta) + val tag = Tag(metaFormatFactory.key, metaBytes.size.toUInt() + 2u, envelope.data?.size ?: 0.toULong()) writePacket(tag.toBytes()) writeFully(metaBytes) writeText("\r\n") - obj.data?.read { copyTo(this@writeObject) } + envelope.data?.read { copyTo(this@writeEnvelope) } flush() } @@ -102,7 +102,7 @@ class TaggedEnvelopeFormat( val metaFormatFactory = io.metaFormatFactories.find { it.name == metaFormatName } ?: error("Meta format could not be resolved") - return TaggedEnvelopeFormat(io, metaFormatFactory.key) + return TaggedEnvelopeFormat(io) } private fun Input.readTag(version: VERSION): Tag { @@ -124,9 +124,9 @@ class TaggedEnvelopeFormat( override fun peekFormat(io: IOPlugin, input: Input): EnvelopeFormat? { return try { val header = input.readTextExactBytes(6) - when(header.substring(2..5)){ - VERSION.DF02.name-> TaggedEnvelopeFormat(io, JsonMetaFormat.key,VERSION.DF02) - VERSION.DF03.name-> TaggedEnvelopeFormat(io, JsonMetaFormat.key,VERSION.DF03) + when (header.substring(2..5)) { + VERSION.DF02.name -> TaggedEnvelopeFormat(io, VERSION.DF02) + VERSION.DF03.name -> TaggedEnvelopeFormat(io, VERSION.DF03) else -> null } } catch (ex: Exception) { diff --git a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/TaglessEnvelopeFormat.kt b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/TaglessEnvelopeFormat.kt index 8e7c9e2b..14d871db 100644 --- a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/TaglessEnvelopeFormat.kt +++ b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/TaglessEnvelopeFormat.kt @@ -8,33 +8,30 @@ import kotlinx.serialization.toUtf8Bytes class TaglessEnvelopeFormat( val io: IOPlugin, - val metaType: String = JsonMetaFormat.name.toString(), meta: Meta = EmptyMeta ) : EnvelopeFormat { private val metaStart = meta[META_START_PROPERTY].string ?: DEFAULT_META_START private val dataStart = meta[DATA_START_PROPERTY].string ?: DEFAULT_DATA_START - val metaFormat = io.metaFormat(metaType, meta) - ?: error("Meta format with type $metaType could not be resolved in $io") - private fun Output.writeProperty(key: String, value: Any) { writeText("#? $key: $value;\r\n") } - override fun Output.writeObject(obj: Envelope) { + override fun Output.writeEnvelope(envelope: Envelope, metaFormatFactory: MetaFormatFactory, formatMeta: Meta) { + val metaFormat = metaFormatFactory(formatMeta, io.context) //printing header writeText(TAGLESS_ENVELOPE_HEADER + "\r\n") //printing all properties - writeProperty(META_TYPE_PROPERTY, metaType) + writeProperty(META_TYPE_PROPERTY, metaFormatFactory.type) //TODO add optional metaFormat properties - writeProperty(DATA_LENGTH_PROPERTY, obj.data?.size ?: 0) + writeProperty(DATA_LENGTH_PROPERTY, envelope.data?.size ?: 0) //Printing meta - if (!obj.meta.isEmpty()) { - val metaBytes = metaFormat.writeBytes(obj.meta) + if (!envelope.meta.isEmpty()) { + val metaBytes = metaFormat.writeBytes(envelope.meta) writeProperty(META_LENGTH_PROPERTY, metaBytes.size) writeText(metaStart + "\r\n") writeFully(metaBytes) @@ -42,7 +39,7 @@ class TaglessEnvelopeFormat( } //Printing data - obj.data?.let { data -> + envelope.data?.let { data -> writeText(dataStart + "\r\n") writeFully(data.toBytes()) } @@ -170,8 +167,7 @@ class TaglessEnvelopeFormat( override val name = TAGLESS_ENVELOPE_TYPE.asName() override fun invoke(meta: Meta, context: Context): EnvelopeFormat { - val metaFormatName: String = meta["name"].string ?: JsonMetaFormat.name.toString() - return TaglessEnvelopeFormat(context.io, metaFormatName, meta) + return TaglessEnvelopeFormat(context.io, meta) } val default by lazy { invoke() } From aefee64581ec56badc4e4c81ec10ea50f36b6188 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Fri, 1 Nov 2019 21:15:55 +0300 Subject: [PATCH 9/9] optional descriptor for descriptor builder --- .../io/serialization/MetaSerializer.kt | 25 +++++++++++-------- .../io/serialization/serializationUtils.kt | 11 +++++--- .../hep/dataforge/io/MetaSerializerTest.kt | 2 ++ 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/serialization/MetaSerializer.kt b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/serialization/MetaSerializer.kt index 271f95e2..b22fed4a 100644 --- a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/serialization/MetaSerializer.kt +++ b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/serialization/MetaSerializer.kt @@ -1,6 +1,7 @@ -package hep.dataforge.io +package hep.dataforge.io.serialization -import hep.dataforge.io.serialization.descriptor +import hep.dataforge.io.toJson +import hep.dataforge.io.toMeta import hep.dataforge.meta.* import hep.dataforge.names.NameToken import hep.dataforge.values.* @@ -16,10 +17,10 @@ object ValueSerializer : KSerializer { private val valueTypeSerializer = EnumSerializer(ValueType::class) private val listSerializer by lazy { ArrayListSerializer(ValueSerializer) } - override val descriptor: SerialDescriptor = descriptor("Value") { + override val descriptor: SerialDescriptor = descriptor("hep.dataforge.values.Value") { boolean("isList") enum("valueType") - element("value", PolymorphicClassDescriptor) + element("value", null) } private fun Decoder.decodeValue(): Value { @@ -69,7 +70,7 @@ object ValueSerializer : KSerializer { object MetaItemSerializer : KSerializer> { override val descriptor: SerialDescriptor = descriptor("MetaItem") { boolean("isNode") - element("value", PolymorphicClassDescriptor) + element("value", null) } @@ -93,17 +94,21 @@ object MetaItemSerializer : KSerializer> { private class DeserializedMeta(override val items: Map>) : MetaBase() - /** * Serialized for meta */ @Serializer(Meta::class) object MetaSerializer : KSerializer { - private val mapSerializer = - HashMapSerializer(StringSerializer, MetaItemSerializer) + private val mapSerializer = HashMapSerializer( + StringSerializer, + MetaItemSerializer + ) - override val descriptor: SerialDescriptor = - NamedMapClassDescriptor("Meta", StringSerializer.descriptor, MetaItemSerializer.descriptor) + override val descriptor: SerialDescriptor = NamedMapClassDescriptor( + "hep.dataforge.meta.Meta", + StringSerializer.descriptor, + MetaItemSerializer.descriptor + ) override fun deserialize(decoder: Decoder): Meta { return if (decoder is JsonInput) { diff --git a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/serialization/serializationUtils.kt b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/serialization/serializationUtils.kt index 4d33fad9..09d17054 100644 --- a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/serialization/serializationUtils.kt +++ b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/serialization/serializationUtils.kt @@ -12,12 +12,12 @@ import kotlinx.serialization.internal.* inline class SerialDescriptorBuilder(private val impl: SerialClassDescImpl) { fun element( name: String, - descriptor: SerialDescriptor, + descriptor: SerialDescriptor?, isOptional: Boolean = false, vararg annotations: Annotation ) { impl.addElement(name, isOptional) - impl.pushDescriptor(descriptor) + descriptor?.let { impl.pushDescriptor(descriptor) } annotations.forEach { impl.pushAnnotation(it) } @@ -57,7 +57,7 @@ inline class SerialDescriptorBuilder(private val impl: SerialClassDescImpl) { fun doubleArray(name: String, isOptional: Boolean = false, vararg annotations: Annotation) = element(name, DoubleArraySerializer.descriptor, isOptional, *annotations) - inline fun > enum(name: String, isOptional: Boolean = false, vararg annotations: Annotation) = + inline fun > enum(name: String, isOptional: Boolean = false, vararg annotations: Annotation) = element(name, EnumSerializer(E::class).descriptor, isOptional, *annotations) fun classAnnotation(a: Annotation) = impl.pushClassAnnotation(a) @@ -65,7 +65,10 @@ inline class SerialDescriptorBuilder(private val impl: SerialClassDescImpl) { fun build(): SerialDescriptor = impl } -inline fun KSerializer<*>.descriptor(name: String, block: SerialDescriptorBuilder.() -> Unit): SerialDescriptor = +inline fun KSerializer.descriptor( + name: String, + block: SerialDescriptorBuilder.() -> Unit +): SerialDescriptor = SerialDescriptorBuilder(SerialClassDescImpl(name)).apply(block).build() fun Decoder.decodeStructure( diff --git a/dataforge-io/src/commonTest/kotlin/hep/dataforge/io/MetaSerializerTest.kt b/dataforge-io/src/commonTest/kotlin/hep/dataforge/io/MetaSerializerTest.kt index f33ad894..7a8447c0 100644 --- a/dataforge-io/src/commonTest/kotlin/hep/dataforge/io/MetaSerializerTest.kt +++ b/dataforge-io/src/commonTest/kotlin/hep/dataforge/io/MetaSerializerTest.kt @@ -1,5 +1,7 @@ package hep.dataforge.io +import hep.dataforge.io.serialization.MetaItemSerializer +import hep.dataforge.io.serialization.MetaSerializer import hep.dataforge.io.serialization.NameSerializer import hep.dataforge.meta.buildMeta import hep.dataforge.names.toName