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.
@ -45,17 +46,20 @@ fun Configurable.getProperty(key: String) = getProperty(key.toName())
* Set a configurable property * Set a configurable property
*/ */
fun Configurable.setProperty(name: Name, item: MetaItem<*>?) { fun Configurable.setProperty(name: Name, item: MetaItem<*>?) {
if(validateItem(name,item)) { if (validateItem(name, item)) {
config[name] = item config[name] = item
} else { } else {
error("Validation failed for property $name with value $item") error("Validation failed for property $name with value $item")
} }
} }
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

@ -29,14 +29,14 @@ interface Specification<T : Configurable> {
/** /**
* Wrap a configuration using static meta as default * Wrap a configuration using static meta as default
*/ */
fun wrap(config: Config = Config(), default: Meta): T = wrap(config){default[it]} fun wrap(config: Config = Config(), default: Meta): T = wrap(config) { default[it] }
/** /**
* Wrap a configuration using static meta as default * Wrap a configuration using static meta as default
*/ */
fun wrap(default: Meta): T = wrap( fun wrap(default: Meta): T = wrap(
Config() Config()
){default[it]} ) { default[it] }
} }
/** /**
@ -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

@ -44,7 +44,7 @@ interface Value {
* get this value represented as List * get this value represented as List
*/ */
val list: List<Value> val list: List<Value>
get() = if(this == Null) emptyList() else listOf(this) get() = if (this == Null) emptyList() else listOf(this)
override fun equals(other: Any?): Boolean override fun equals(other: Any?): Boolean
@ -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,15 +13,17 @@ 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)
} }
@Test @Test

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>()}
} }