Workaround for enum delegate

This commit is contained in:
Alexander Nozik 2020-03-15 13:33:20 +03:00
parent 4ab71a79db
commit 2c52e9aaa3
7 changed files with 27 additions and 16 deletions

View File

@ -1,6 +1,6 @@
plugins { plugins {
val toolsVersion = "0.4.0" val toolsVersion = "0.4.0-dev"
id("scientifik.mpp") version toolsVersion apply false id("scientifik.mpp") version toolsVersion apply false
id("scientifik.jvm") version toolsVersion apply false id("scientifik.jvm") version toolsVersion apply false
id("scientifik.publish") version toolsVersion apply false id("scientifik.publish") version toolsVersion apply false

View File

@ -4,6 +4,7 @@ import hep.dataforge.meta.*
import hep.dataforge.meta.descriptors.* import hep.dataforge.meta.descriptors.*
import hep.dataforge.names.Name import hep.dataforge.names.Name
import hep.dataforge.names.toName import hep.dataforge.names.toName
import hep.dataforge.values.Value
/** /**
* A container that holds a [Config] and a default item provider. * A container that holds a [Config] and a default item provider.
@ -52,10 +53,13 @@ fun Configurable.setProperty(name: Name, item: MetaItem<*>?) {
} }
} }
fun Configurable.setProperty(name: Name, value: Value) = setProperty(name, MetaItem.ValueItem(value))
fun Configurable.setProperty(name: Name, meta: Meta) = setProperty(name, MetaItem.NodeItem(meta))
fun Configurable.setProperty(key: String, item: MetaItem<*>?) { fun Configurable.setProperty(key: String, item: MetaItem<*>?) {
setProperty(key.toName(), item) setProperty(key.toName(), item)
} }
fun <T : Configurable> T.configure(meta: Meta): T = this.apply { config.update(meta) } fun <T : Configurable> T.configure(meta: Meta): T = this.apply { config.update(meta) }
fun <T : Configurable> T.configure(action: Config.() -> Unit): T = apply { config.apply(action) } inline fun <T : Configurable> T.configure(action: Config.() -> Unit): T = apply { config.apply(action) }

View File

@ -168,14 +168,13 @@ fun Configurable.float(default: Float, key: Name? = null): ReadWriteProperty<Any
/** /**
* Enum delegate * Enum delegate
*/ */
inline fun <reified E : Enum<E>> Configurable.enum(default: E, key: Name? = null): ReadWriteProperty<Any?, E> { fun <E : Enum<E>> Configurable.enum(
return item(default, key).transform { it.enum<E>() ?: default } default: E, key: Name? = null, resolve: MetaItem<*>.() -> E?
} ): ReadWriteProperty<Any?, E> = item(default, key).transform {it?.resolve() ?: default }
/* /*
* Extra delegates for special cases * Extra delegates for special cases
*/ */
fun Configurable.stringList(vararg strings: String, key: Name? = null): ReadWriteProperty<Any?, List<String>> = fun Configurable.stringList(vararg strings: String, key: Name? = null): ReadWriteProperty<Any?, List<String>> =
item(listOf(*strings), key) { item(listOf(*strings), key) {
it?.value?.stringList ?: emptyList() it?.value?.stringList ?: emptyList()

View File

@ -57,8 +57,11 @@ fun <C : Configurable, S : Specification<C>> Configurable.update(spec: S, action
fun <C : Configurable, S : Specification<C>> S.createStyle(action: C.() -> Unit): Meta = fun <C : Configurable, S : Specification<C>> S.createStyle(action: C.() -> Unit): Meta =
Config().also { update(it, action) } Config().also { update(it, action) }
fun <T : Configurable> MetaItem<*>.spec(spec: Specification<T>): T? = node?.let { spec.wrap( fun <T : Configurable> MetaItem<*>.spec(spec: Specification<T>): T? = node?.let {
Config(), it) } spec.wrap(
Config(), it
)
}
@JvmName("configSpec") @JvmName("configSpec")
fun <T : Configurable> MetaItem<Config>.spec(spec: Specification<T>): T? = node?.let { spec.wrap(it) } fun <T : Configurable> MetaItem<Config>.spec(spec: Specification<T>): T? = node?.let { spec.wrap(it) }

View File

@ -231,6 +231,8 @@ fun FloatArray.asValue(): Value = if (isEmpty()) Null else ListValue(map { Numbe
fun ByteArray.asValue(): Value = if (isEmpty()) Null else ListValue(map { NumberValue(it) }) fun ByteArray.asValue(): Value = if (isEmpty()) Null else ListValue(map { NumberValue(it) })
fun <E : Enum<E>> E.asValue(): Value = EnumValue(this)
/** /**
* Create Value from String using closest match conversion * Create Value from String using closest match conversion

View File

@ -13,14 +13,16 @@ class MetaDelegateTest {
class InnerSpec : Scheme() { class InnerSpec : Scheme() {
var innerValue by string() var innerValue by string()
companion object : SchemeSpec<InnerSpec>(::InnerSpec) companion object : SchemeSpec<InnerSpec>(::InnerSpec)
} }
class TestScheme : Scheme() { class TestScheme : Scheme() {
var myValue by string() var myValue by string()
var safeValue by double(2.2) var safeValue by double(2.2)
var enumValue by enum(TestEnum.YES) var enumValue by enum(TestEnum.YES) { enum<TestEnum>() }
var inner by spec(InnerSpec) var inner by spec(InnerSpec)
companion object : SchemeSpec<TestScheme>(::TestScheme) companion object : SchemeSpec<TestScheme>(::TestScheme)
} }

View File

@ -1,5 +1,6 @@
package hep.dataforge.tables package hep.dataforge.tables
import hep.dataforge.meta.enum
import hep.dataforge.meta.scheme.Scheme import hep.dataforge.meta.scheme.Scheme
import hep.dataforge.meta.scheme.SchemeSpec import hep.dataforge.meta.scheme.SchemeSpec
import hep.dataforge.meta.scheme.enum import hep.dataforge.meta.scheme.enum
@ -13,5 +14,5 @@ open class ColumnScheme : Scheme() {
} }
class ValueColumnScheme : ColumnScheme() { class ValueColumnScheme : ColumnScheme() {
var valueType by enum(ValueType.STRING) var valueType by enum(ValueType.STRING){enum<ValueType>()}
} }