Remove restriction on empty ListValue since it have a separate type now.

This commit is contained in:
Alexander Nozik 2021-01-28 10:34:28 +03:00
parent e88178ffe7
commit 66355793ee
2 changed files with 28 additions and 4 deletions

View File

@ -2,6 +2,7 @@ package hep.dataforge.io
import hep.dataforge.meta.* import hep.dataforge.meta.*
import hep.dataforge.meta.JsonMeta.Companion.JSON_ARRAY_KEY import hep.dataforge.meta.JsonMeta.Companion.JSON_ARRAY_KEY
import hep.dataforge.values.ListValue
import hep.dataforge.values.number import hep.dataforge.values.number
import kotlinx.io.asBinary import kotlinx.io.asBinary
import kotlinx.serialization.json.* 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() }) 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)
}
} }

View File

@ -179,10 +179,6 @@ public class EnumValue<E : Enum<*>>(override val value: E) : Value {
} }
public class ListValue(override val list: List<Value>) : Value, Iterable<Value> { public class ListValue(override val list: List<Value>) : Value, Iterable<Value> {
init {
require(list.isNotEmpty()) { "Can't create list value from empty list" }
}
override val value: List<Value> get() = list override val value: List<Value> get() = list
override val type: ValueType get() = ValueType.LIST override val type: ValueType get() = ValueType.LIST
@ -202,6 +198,10 @@ public class ListValue(override val list: List<Value>) : Value, Iterable<Value>
override fun hashCode(): Int { override fun hashCode(): Int {
return list.hashCode() return list.hashCode()
} }
public companion object{
public val EMPTY: ListValue = ListValue(emptyList())
}
} }
public fun Number.asValue(): Value = NumberValue(this) public fun Number.asValue(): Value = NumberValue(this)