From 10de05240c160e28a0927a1908837078ea8ec8cc Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Tue, 29 Oct 2019 20:11:57 +0300 Subject: [PATCH] Fix #12 --- .../io/yaml/FrontMatterEnvelopeFormat.kt | 6 ++-- .../hep/dataforge/io/yaml/YamlMetaFormat.kt | 2 ++ .../hep/dataforge/meta/configDelegates.kt | 8 ++--- .../kotlin/hep/dataforge/meta/mapMeta.kt | 2 ++ .../kotlin/hep/dataforge/values/Value.kt | 30 ++++++++++++------- .../hep/dataforge/values/exoticValues.kt | 2 +- .../hep/dataforge/meta/SpecificationTest.kt | 23 ++++++++++++++ 7 files changed, 53 insertions(+), 20 deletions(-) create mode 100644 dataforge-meta/src/commonTest/kotlin/hep/dataforge/meta/SpecificationTest.kt 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 d12e0a26..9306b336 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,13 +2,11 @@ package hep.dataforge.io.yaml import hep.dataforge.context.Context import hep.dataforge.io.* -import hep.dataforge.meta.EmptyMeta -import hep.dataforge.meta.Meta -import hep.dataforge.meta.get -import hep.dataforge.meta.string +import hep.dataforge.meta.* import kotlinx.io.core.* import kotlinx.serialization.toUtf8Bytes +@DFExperimental class FrontMatterEnvelopeFormat( val io: IOPlugin, val metaType: String = YamlMetaFormat.name.toString(), diff --git a/dataforge-io/dataforge-io-yaml/src/main/kotlin/hep/dataforge/io/yaml/YamlMetaFormat.kt b/dataforge-io/dataforge-io-yaml/src/main/kotlin/hep/dataforge/io/yaml/YamlMetaFormat.kt index 39fa68df..24ea44ec 100644 --- a/dataforge-io/dataforge-io-yaml/src/main/kotlin/hep/dataforge/io/yaml/YamlMetaFormat.kt +++ b/dataforge-io/dataforge-io-yaml/src/main/kotlin/hep/dataforge/io/yaml/YamlMetaFormat.kt @@ -4,6 +4,7 @@ import hep.dataforge.context.Context import hep.dataforge.descriptors.NodeDescriptor import hep.dataforge.io.MetaFormat import hep.dataforge.io.MetaFormatFactory +import hep.dataforge.meta.DFExperimental import hep.dataforge.meta.Meta import hep.dataforge.meta.toMap import hep.dataforge.meta.toMeta @@ -29,6 +30,7 @@ private class InputAsStream(val input: Input) : InputStream() { private fun Input.asStream() = InputAsStream(this) +@DFExperimental class YamlMetaFormat(val meta: Meta) : MetaFormat { private val yaml = Yaml() diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/configDelegates.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/configDelegates.kt index 1dabd05b..34fa0e71 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/configDelegates.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/configDelegates.kt @@ -117,11 +117,11 @@ fun Configurable.spec(builder: (Config) -> T, key: Name? = null) * Extra delegates for special cases */ -fun Configurable.stringList(key: Name? = null): ReadWriteDelegateWrapper> = - value(emptyList(), key) { it?.list?.map { value -> value.string } ?: emptyList() } +fun Configurable.stringList(vararg strings: String, key: Name? = null): ReadWriteDelegateWrapper> = + value(strings.asList(), key) { it?.list?.map { value -> value.string } ?: emptyList() } -fun Configurable.numberList(key: Name? = null): ReadWriteDelegateWrapper> = - value(emptyList(), key) { it?.list?.map { value -> value.number } ?: emptyList() } +fun Configurable.numberList(vararg numbers: Number, key: Name? = null): ReadWriteDelegateWrapper> = + value(numbers.asList(), key) { it?.list?.map { value -> value.number } ?: emptyList() } /** * A special delegate for double arrays diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/mapMeta.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/mapMeta.kt index d146ea1f..b0d8162c 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/mapMeta.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/mapMeta.kt @@ -14,6 +14,7 @@ import hep.dataforge.values.Value /** * Convert meta to map of maps */ +@DFExperimental fun Meta.toMap(descriptor: NodeDescriptor? = null): Map { return items.entries.associate { (token, item) -> token.toString() to when (item) { @@ -26,6 +27,7 @@ fun Meta.toMap(descriptor: NodeDescriptor? = null): Map { /** * Convert map of maps to meta */ +@DFExperimental fun Map.toMeta(descriptor: NodeDescriptor? = null): Meta = buildMeta { entries.forEach { (key, value) -> @Suppress("UNCHECKED_CAST") 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 2fce145f..f044b018 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/Value.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/Value.kt @@ -41,7 +41,7 @@ interface Value { * get this value represented as List */ val list: List - get() = listOf(this) + get() = if(this == Null) emptyList() else listOf(this) override fun equals(other: Any?): Boolean @@ -60,7 +60,14 @@ interface Value { true -> True false -> False is Number -> value.asValue() - is Iterable<*> -> ListValue(value.map { of(it) }) + is Iterable<*> -> { + val list = value.map { of(it) } + if (list.isEmpty()) { + Null + } else { + ListValue(list) + } + } is DoubleArray -> value.asValue() is IntArray -> value.asValue() is FloatArray -> value.asValue() @@ -174,9 +181,7 @@ class EnumValue>(override val value: E) : Value { class ListValue(override val list: List) : Value { init { - if (list.isEmpty()) { - throw IllegalArgumentException("Can't create list value from empty list") - } + require(list.isNotEmpty()) { "Can't create list value from empty list" } } override val value: List get() = list @@ -208,17 +213,20 @@ fun Boolean.asValue(): Value = if (this) True else False fun String.asValue(): Value = StringValue(this) -fun Iterable.asValue(): Value = ListValue(this.toList()) +fun Iterable.asValue(): Value { + val list = toList() + return if (list.isEmpty()) Null else ListValue(this.toList()) +} -fun IntArray.asValue(): Value = ListValue(map { NumberValue(it) }) +fun IntArray.asValue(): Value = if (isEmpty()) Null else ListValue(map { NumberValue(it) }) -fun LongArray.asValue(): Value = ListValue(map { NumberValue(it) }) +fun LongArray.asValue(): Value = if (isEmpty()) Null else ListValue(map { NumberValue(it) }) -fun ShortArray.asValue(): Value = ListValue(map { NumberValue(it) }) +fun ShortArray.asValue(): Value = if (isEmpty()) Null else ListValue(map { NumberValue(it) }) -fun FloatArray.asValue(): Value = ListValue(map { NumberValue(it) }) +fun FloatArray.asValue(): Value = if (isEmpty()) Null else ListValue(map { NumberValue(it) }) -fun ByteArray.asValue(): Value = ListValue(map { NumberValue(it) }) +fun ByteArray.asValue(): Value = if (isEmpty()) Null else ListValue(map { NumberValue(it) }) /** diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/exoticValues.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/exoticValues.kt index fa69e8da..d0fb86e8 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/exoticValues.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/exoticValues.kt @@ -46,4 +46,4 @@ class DoubleArrayValue(override val value: DoubleArray) : Value { override fun toString(): String = list.joinToString (prefix = "[", postfix = "]") } -fun DoubleArray.asValue(): DoubleArrayValue = DoubleArrayValue(this) +fun DoubleArray.asValue(): Value = if(isEmpty()) Null else DoubleArrayValue(this) diff --git a/dataforge-meta/src/commonTest/kotlin/hep/dataforge/meta/SpecificationTest.kt b/dataforge-meta/src/commonTest/kotlin/hep/dataforge/meta/SpecificationTest.kt new file mode 100644 index 00000000..9098cf18 --- /dev/null +++ b/dataforge-meta/src/commonTest/kotlin/hep/dataforge/meta/SpecificationTest.kt @@ -0,0 +1,23 @@ +package hep.dataforge.meta + +import kotlin.test.Test +import kotlin.test.assertEquals + +class SpecificationTest { + class TestSpecific(override val config: Config) : Specific { + var list by numberList(1, 2, 3) + + companion object : Specification { + override fun wrap(config: Config): TestSpecific = TestSpecific(config) + } + } + + + @Test + fun testSpecific(){ + val testObject = TestSpecific.build { + list = emptyList() + } + assertEquals(emptyList(), testObject.list) + } +} \ No newline at end of file