This commit is contained in:
Alexander Nozik 2019-10-29 20:11:57 +03:00
parent 264ed14cf4
commit 10de05240c
7 changed files with 53 additions and 20 deletions

View File

@ -2,13 +2,11 @@ package hep.dataforge.io.yaml
import hep.dataforge.context.Context import hep.dataforge.context.Context
import hep.dataforge.io.* import hep.dataforge.io.*
import hep.dataforge.meta.EmptyMeta import hep.dataforge.meta.*
import hep.dataforge.meta.Meta
import hep.dataforge.meta.get
import hep.dataforge.meta.string
import kotlinx.io.core.* import kotlinx.io.core.*
import kotlinx.serialization.toUtf8Bytes import kotlinx.serialization.toUtf8Bytes
@DFExperimental
class FrontMatterEnvelopeFormat( class FrontMatterEnvelopeFormat(
val io: IOPlugin, val io: IOPlugin,
val metaType: String = YamlMetaFormat.name.toString(), val metaType: String = YamlMetaFormat.name.toString(),

View File

@ -4,6 +4,7 @@ import hep.dataforge.context.Context
import hep.dataforge.descriptors.NodeDescriptor import hep.dataforge.descriptors.NodeDescriptor
import hep.dataforge.io.MetaFormat import hep.dataforge.io.MetaFormat
import hep.dataforge.io.MetaFormatFactory import hep.dataforge.io.MetaFormatFactory
import hep.dataforge.meta.DFExperimental
import hep.dataforge.meta.Meta import hep.dataforge.meta.Meta
import hep.dataforge.meta.toMap import hep.dataforge.meta.toMap
import hep.dataforge.meta.toMeta import hep.dataforge.meta.toMeta
@ -29,6 +30,7 @@ private class InputAsStream(val input: Input) : InputStream() {
private fun Input.asStream() = InputAsStream(this) private fun Input.asStream() = InputAsStream(this)
@DFExperimental
class YamlMetaFormat(val meta: Meta) : MetaFormat { class YamlMetaFormat(val meta: Meta) : MetaFormat {
private val yaml = Yaml() private val yaml = Yaml()

View File

@ -117,11 +117,11 @@ fun <T : Specific> Configurable.spec(builder: (Config) -> T, key: Name? = null)
* Extra delegates for special cases * Extra delegates for special cases
*/ */
fun Configurable.stringList(key: Name? = null): ReadWriteDelegateWrapper<Value?, List<String>> = fun Configurable.stringList(vararg strings: String, key: Name? = null): ReadWriteDelegateWrapper<Value?, List<String>> =
value(emptyList(), key) { it?.list?.map { value -> value.string } ?: emptyList() } value(strings.asList(), key) { it?.list?.map { value -> value.string } ?: emptyList() }
fun Configurable.numberList(key: Name? = null): ReadWriteDelegateWrapper<Value?, List<Number>> = fun Configurable.numberList(vararg numbers: Number, key: Name? = null): ReadWriteDelegateWrapper<Value?, List<Number>> =
value(emptyList(), key) { it?.list?.map { value -> value.number } ?: emptyList() } value(numbers.asList(), key) { it?.list?.map { value -> value.number } ?: emptyList() }
/** /**
* A special delegate for double arrays * A special delegate for double arrays

View File

@ -14,6 +14,7 @@ import hep.dataforge.values.Value
/** /**
* Convert meta to map of maps * Convert meta to map of maps
*/ */
@DFExperimental
fun Meta.toMap(descriptor: NodeDescriptor? = null): Map<String, Any?> { fun Meta.toMap(descriptor: NodeDescriptor? = null): Map<String, Any?> {
return items.entries.associate { (token, item) -> return items.entries.associate { (token, item) ->
token.toString() to when (item) { token.toString() to when (item) {
@ -26,6 +27,7 @@ fun Meta.toMap(descriptor: NodeDescriptor? = null): Map<String, Any?> {
/** /**
* Convert map of maps to meta * Convert map of maps to meta
*/ */
@DFExperimental
fun Map<String, Any?>.toMeta(descriptor: NodeDescriptor? = null): Meta = buildMeta { fun Map<String, Any?>.toMeta(descriptor: NodeDescriptor? = null): Meta = buildMeta {
entries.forEach { (key, value) -> entries.forEach { (key, value) ->
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")

View File

@ -41,7 +41,7 @@ interface Value {
* get this value represented as List * get this value represented as List
*/ */
val list: List<Value> val list: List<Value>
get() = listOf(this) get() = if(this == Null) emptyList() else listOf(this)
override fun equals(other: Any?): Boolean override fun equals(other: Any?): Boolean
@ -60,7 +60,14 @@ interface Value {
true -> True true -> True
false -> False false -> False
is Number -> value.asValue() 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 DoubleArray -> value.asValue()
is IntArray -> value.asValue() is IntArray -> value.asValue()
is FloatArray -> value.asValue() is FloatArray -> value.asValue()
@ -174,9 +181,7 @@ class EnumValue<E : Enum<*>>(override val value: E) : Value {
class ListValue(override val list: List<Value>) : Value { class ListValue(override val list: List<Value>) : Value {
init { init {
if (list.isEmpty()) { require(list.isNotEmpty()) { "Can't create list value from empty list" }
throw IllegalArgumentException("Can't create list value from empty list")
}
} }
override val value: List<Value> get() = list override val value: List<Value> get() = list
@ -208,17 +213,20 @@ fun Boolean.asValue(): Value = if (this) True else False
fun String.asValue(): Value = StringValue(this) fun String.asValue(): Value = StringValue(this)
fun Iterable<Value>.asValue(): Value = ListValue(this.toList()) fun Iterable<Value>.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) })
/** /**

View File

@ -46,4 +46,4 @@ class DoubleArrayValue(override val value: DoubleArray) : Value {
override fun toString(): String = list.joinToString (prefix = "[", postfix = "]") override fun toString(): String = list.joinToString (prefix = "[", postfix = "]")
} }
fun DoubleArray.asValue(): DoubleArrayValue = DoubleArrayValue(this) fun DoubleArray.asValue(): Value = if(isEmpty()) Null else DoubleArrayValue(this)

View File

@ -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<TestSpecific> {
override fun wrap(config: Config): TestSpecific = TestSpecific(config)
}
}
@Test
fun testSpecific(){
val testObject = TestSpecific.build {
list = emptyList()
}
assertEquals(emptyList(), testObject.list)
}
}