From 66355793ee0a5143be296a353bb45f4d39aaf11e Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Thu, 28 Jan 2021 10:34:28 +0300 Subject: [PATCH] Remove restriction on empty ListValue since it have a separate type now. --- .../kotlin/hep/dataforge/io/MetaFormatTest.kt | 24 +++++++++++++++++++ .../kotlin/hep/dataforge/values/Value.kt | 8 +++---- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/dataforge-io/src/commonTest/kotlin/hep/dataforge/io/MetaFormatTest.kt b/dataforge-io/src/commonTest/kotlin/hep/dataforge/io/MetaFormatTest.kt index af33cfde..9e71a907 100644 --- a/dataforge-io/src/commonTest/kotlin/hep/dataforge/io/MetaFormatTest.kt +++ b/dataforge-io/src/commonTest/kotlin/hep/dataforge/io/MetaFormatTest.kt @@ -2,6 +2,7 @@ package hep.dataforge.io import hep.dataforge.meta.* import hep.dataforge.meta.JsonMeta.Companion.JSON_ARRAY_KEY +import hep.dataforge.values.ListValue import hep.dataforge.values.number import kotlinx.io.asBinary import kotlinx.serialization.json.* @@ -79,4 +80,27 @@ class MetaFormatTest { assertEquals(listOf(1.0, 2.0, 3.0), meta["$JSON_ARRAY_KEY[2"].value?.list?.map { it.number.toDouble() }) } + @Test + fun testJsonStringToMeta(){ + val jsonString = """ + { + "comments": [ + ], + "end_time": "2018-04-13T22:01:46", + "format_description": "https://docs.google.com/document/d/12qmnZRO55y6zr08Wf-BQYAmklqgf5y3j_gD_VkNscXc/edit?usp=sharing", + "iteration_info": { + "iteration": 4, + "reverse": false + }, + "operator": "Vasiliy", + "programm_revision": "1.1.1-79-g7c0cad6", + "start_time": "2018-04-13T21:42:04", + "type": "info_file" + } + """.trimIndent() + val json = Json.parseToJsonElement(jsonString) + val meta = json.toMetaItem().node!! + assertEquals(ListValue.EMPTY, meta["comments"].value) + } + } \ No newline at end of file diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/Value.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/Value.kt index 676f7134..73b1318c 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/Value.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/Value.kt @@ -179,10 +179,6 @@ public class EnumValue>(override val value: E) : Value { } public class ListValue(override val list: List) : Value, Iterable { - init { - require(list.isNotEmpty()) { "Can't create list value from empty list" } - } - override val value: List get() = list override val type: ValueType get() = ValueType.LIST @@ -202,6 +198,10 @@ public class ListValue(override val list: List) : Value, Iterable override fun hashCode(): Int { return list.hashCode() } + + public companion object{ + public val EMPTY: ListValue = ListValue(emptyList()) + } } public fun Number.asValue(): Value = NumberValue(this)