From 459e5704371667c8e5ddd64ee8bb0c9e9a42e270 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Tue, 30 Apr 2019 21:39:21 +0300 Subject: [PATCH] Config list delegates --- .../hep/dataforge/descriptors/NodeDescriptor.kt | 2 +- .../kotlin/hep/dataforge/meta/ConfigDelegates.kt | 16 +++++++++++++--- .../kotlin/hep/dataforge/meta/Delegates.kt | 2 +- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/descriptors/NodeDescriptor.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/descriptors/NodeDescriptor.kt index bbc9fdcc..902c81a3 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/descriptors/NodeDescriptor.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/descriptors/NodeDescriptor.kt @@ -75,7 +75,7 @@ class NodeDescriptor(override val config: Config) : Specific { * * @return */ - var tags: List by value().map { value -> + var tags: List by value{ value -> value?.list?.map { it.string } ?: emptyList() } 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 9a5a72c5..9baa802a 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/ConfigDelegates.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/ConfigDelegates.kt @@ -1,6 +1,5 @@ package hep.dataforge.meta -import hep.dataforge.values.Null import hep.dataforge.values.Value import kotlin.jvm.JvmName @@ -10,9 +9,18 @@ import kotlin.jvm.JvmName /** * A property delegate that uses custom key */ -fun Configurable.value(default: Any = Null, key: String? = null) = +fun Configurable.value(default: T, key: String? = null) = MutableValueDelegate(config, key, Value.of(default)) +fun Configurable.value(default: T? = null, key: String? = null, transform: (Value?) -> T) = + MutableValueDelegate(config, key, Value.of(default)).transform(reader = transform) + +fun Configurable.stringList(key: String? = null) = + value { it?.list?.map { value -> value.string } ?: emptyList() } + +fun Configurable.numberList(key: String? = null) = + value { it?.list?.map { value -> value.number } ?: emptyList() } + fun Configurable.string(default: String? = null, key: String? = null) = MutableStringDelegate(config, key, default) @@ -93,10 +101,12 @@ fun Configurable.float(default: Float, key: String? = null) = inline fun > Configurable.enum(default: E, key: String? = null) = MutableSafeEnumvDelegate(config, key, default) { enumValueOf(it) } +/* Node delegates */ + fun Configurable.node(key: String? = null) = MutableNodeDelegate(config, key) fun Configurable.spec(spec: Specification, key: String? = null) = MutableMorphDelegate(config, key) { spec.wrap(it) } fun Configurable.spec(builder: (Config) -> T, key: String? = null) = - MutableMorphDelegate(config, key) { specification(builder).wrap(it) } \ No newline at end of file + MutableMorphDelegate(config, key) { specification(builder).wrap(it) } diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Delegates.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Delegates.kt index ba3bd883..377ec994 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Delegates.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Delegates.kt @@ -184,7 +184,7 @@ class MutableValueDelegate>( } } - fun map(writer: (T) -> Value? = { Value.of(it) }, reader: (Value?) -> T) = + fun transform(writer: (T) -> Value? = { Value.of(it) }, reader: (Value?) -> T) = ReadWriteDelegateWrapper(this, reader, writer) }