Config list delegates

This commit is contained in:
Alexander Nozik 2019-04-30 21:39:21 +03:00
parent 3cf62bb6b2
commit 459e570437
3 changed files with 15 additions and 5 deletions

View File

@ -75,7 +75,7 @@ class NodeDescriptor(override val config: Config) : Specific {
* *
* @return * @return
*/ */
var tags: List<String> by value().map { value -> var tags: List<String> by value{ value ->
value?.list?.map { it.string } ?: emptyList() value?.list?.map { it.string } ?: emptyList()
} }

View File

@ -1,6 +1,5 @@
package hep.dataforge.meta package hep.dataforge.meta
import hep.dataforge.values.Null
import hep.dataforge.values.Value import hep.dataforge.values.Value
import kotlin.jvm.JvmName import kotlin.jvm.JvmName
@ -10,9 +9,18 @@ import kotlin.jvm.JvmName
/** /**
* A property delegate that uses custom key * A property delegate that uses custom key
*/ */
fun Configurable.value(default: Any = Null, key: String? = null) = fun <T> Configurable.value(default: T, key: String? = null) =
MutableValueDelegate(config, key, Value.of(default)) MutableValueDelegate(config, key, Value.of(default))
fun <T> 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) = fun Configurable.string(default: String? = null, key: String? = null) =
MutableStringDelegate(config, key, default) MutableStringDelegate(config, key, default)
@ -93,10 +101,12 @@ fun Configurable.float(default: Float, key: String? = null) =
inline fun <reified E : Enum<E>> Configurable.enum(default: E, key: String? = null) = inline fun <reified E : Enum<E>> Configurable.enum(default: E, key: String? = null) =
MutableSafeEnumvDelegate(config, key, default) { enumValueOf(it) } MutableSafeEnumvDelegate(config, key, default) { enumValueOf(it) }
/* Node delegates */
fun Configurable.node(key: String? = null) = MutableNodeDelegate(config, key) fun Configurable.node(key: String? = null) = MutableNodeDelegate(config, key)
fun <T : Specific> Configurable.spec(spec: Specification<T>, key: String? = null) = fun <T : Specific> Configurable.spec(spec: Specification<T>, key: String? = null) =
MutableMorphDelegate(config, key) { spec.wrap(it) } MutableMorphDelegate(config, key) { spec.wrap(it) }
fun <T : Specific> Configurable.spec(builder: (Config) -> T, key: String? = null) = fun <T : Specific> Configurable.spec(builder: (Config) -> T, key: String? = null) =
MutableMorphDelegate(config, key) { specification(builder).wrap(it) } MutableMorphDelegate(config, key) { specification(builder).wrap(it) }

View File

@ -184,7 +184,7 @@ class MutableValueDelegate<M : MutableMeta<M>>(
} }
} }
fun <T> map(writer: (T) -> Value? = { Value.of(it) }, reader: (Value?) -> T) = fun <T> transform(writer: (T) -> Value? = { Value.of(it) }, reader: (Value?) -> T) =
ReadWriteDelegateWrapper(this, reader, writer) ReadWriteDelegateWrapper(this, reader, writer)
} }