From 7fa6617e7e85d341d7740cc252e46753cfd4c573 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Mon, 10 Jun 2024 21:22:00 +0300 Subject: [PATCH] First implementation for protobuf converter --- CHANGELOG.md | 2 ++ .../space/kscience/dataforge/meta/JsonMeta.kt | 14 +++++++++----- .../kscience/dataforge/meta/MetaConverter.kt | 8 +++++++- .../kscience/dataforge/meta/ConvertersTest.kt | 17 +++++++++++++++++ 4 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 dataforge-meta/src/commonTest/kotlin/space/kscience/dataforge/meta/ConvertersTest.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 43e6bf4e..f7a2e363 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ ### Removed ### Fixed +- Fixed NameToken parsing. +- Top level string list meta conversion. ### Security diff --git a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/JsonMeta.kt b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/JsonMeta.kt index 049c1733..39a98927 100644 --- a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/JsonMeta.kt +++ b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/JsonMeta.kt @@ -163,11 +163,15 @@ public fun JsonObject.toMeta(descriptor: MetaDescriptor? = null): SealedMeta { public fun JsonElement.toMeta(descriptor: MetaDescriptor? = null): SealedMeta = when (this) { is JsonPrimitive -> Meta(toValue(descriptor)) is JsonObject -> toMeta(descriptor) - is JsonArray -> SealedMeta(null, - linkedMapOf().apply { - addJsonElement(Meta.JSON_ARRAY_KEY, this@toMeta, null) - } - ) + is JsonArray -> if (any { it is JsonObject }) { + SealedMeta(null, + linkedMapOf().apply { + addJsonElement(Meta.JSON_ARRAY_KEY, this@toMeta, null) + } + ) + } else{ + Meta(map { it.toValueOrNull(descriptor) ?: kotlin.error("Unreachable: should not contain objects") }.asValue()) + } } // diff --git a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MetaConverter.kt b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MetaConverter.kt index 9baf0087..0f0e8efe 100644 --- a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MetaConverter.kt +++ b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MetaConverter.kt @@ -11,7 +11,7 @@ import space.kscience.dataforge.misc.DFExperimental /** * A converter of generic object to and from [Meta] */ -public interface MetaConverter: MetaReader { +public interface MetaConverter : MetaReader { /** * A descriptor for resulting meta @@ -116,6 +116,12 @@ public interface MetaConverter: MetaReader { override fun convert(obj: E): Meta = Meta(obj.asValue()) } + public val stringList: MetaConverter> = object : MetaConverter> { + override fun convert(obj: List): Meta = Meta(obj.map { it.asValue() }.asValue()) + + override fun readOrNull(source: Meta): List? = source.stringList + } + public fun valueList( writer: (T) -> Value = { Value.of(it) }, reader: (Value) -> T, diff --git a/dataforge-meta/src/commonTest/kotlin/space/kscience/dataforge/meta/ConvertersTest.kt b/dataforge-meta/src/commonTest/kotlin/space/kscience/dataforge/meta/ConvertersTest.kt new file mode 100644 index 00000000..fda978e4 --- /dev/null +++ b/dataforge-meta/src/commonTest/kotlin/space/kscience/dataforge/meta/ConvertersTest.kt @@ -0,0 +1,17 @@ +package space.kscience.dataforge.meta + +import kotlin.test.Test +import kotlin.test.assertEquals + +class ConvertersTest { + + @Test + fun stringListConversion() { + val list = listOf("A", "B", "C") + val meta = MetaConverter.stringList.convert(list) + val json = meta.toJson() + val reconstructedMeta = json.toMeta() + val reconstructed = MetaConverter.stringList.read(reconstructedMeta) + assertEquals(list,reconstructed) + } +} \ No newline at end of file