From 2c52e9aaa34e160f69c8f9c57fbc3937900016d4 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sun, 15 Mar 2020 13:33:20 +0300 Subject: [PATCH] Workaround for enum delegate --- build.gradle.kts | 2 +- .../kotlin/hep/dataforge/meta/scheme/Configurable.kt | 8 ++++++-- .../hep/dataforge/meta/scheme/ConfigurableDelegate.kt | 7 +++---- .../kotlin/hep/dataforge/meta/scheme/Specification.kt | 11 +++++++---- .../commonMain/kotlin/hep/dataforge/values/Value.kt | 4 +++- .../kotlin/hep/dataforge/meta/MetaDelegateTest.kt | 8 +++++--- .../kotlin/hep/dataforge/tables/ColumnScheme.kt | 3 ++- 7 files changed, 27 insertions(+), 16 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 0d4cc1fb..fca616de 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,6 +1,6 @@ plugins { - val toolsVersion = "0.4.0" + val toolsVersion = "0.4.0-dev" id("scientifik.mpp") version toolsVersion apply false id("scientifik.jvm") version toolsVersion apply false id("scientifik.publish") version toolsVersion apply false diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/scheme/Configurable.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/scheme/Configurable.kt index 42aaeb07..df8c8e93 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/scheme/Configurable.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/scheme/Configurable.kt @@ -4,6 +4,7 @@ import hep.dataforge.meta.* import hep.dataforge.meta.descriptors.* import hep.dataforge.names.Name import hep.dataforge.names.toName +import hep.dataforge.values.Value /** * 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 */ fun Configurable.setProperty(name: Name, item: MetaItem<*>?) { - if(validateItem(name,item)) { + if (validateItem(name, item)) { config[name] = item } else { 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<*>?) { setProperty(key.toName(), item) } fun T.configure(meta: Meta): T = this.apply { config.update(meta) } -fun T.configure(action: Config.() -> Unit): T = apply { config.apply(action) } +inline fun T.configure(action: Config.() -> Unit): T = apply { config.apply(action) } diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/scheme/ConfigurableDelegate.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/scheme/ConfigurableDelegate.kt index da6d92a8..a83bc62c 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/scheme/ConfigurableDelegate.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/scheme/ConfigurableDelegate.kt @@ -168,14 +168,13 @@ fun Configurable.float(default: Float, key: Name? = null): ReadWriteProperty> Configurable.enum(default: E, key: Name? = null): ReadWriteProperty { - return item(default, key).transform { it.enum() ?: default } -} +fun > Configurable.enum( + default: E, key: Name? = null, resolve: MetaItem<*>.() -> E? +): ReadWriteProperty = item(default, key).transform {it?.resolve() ?: default } /* * Extra delegates for special cases */ - fun Configurable.stringList(vararg strings: String, key: Name? = null): ReadWriteProperty> = item(listOf(*strings), key) { it?.value?.stringList ?: emptyList() diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/scheme/Specification.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/scheme/Specification.kt index 4aa7bf02..1783e841 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/scheme/Specification.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/scheme/Specification.kt @@ -29,14 +29,14 @@ interface Specification { /** * 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 */ fun wrap(default: Meta): T = wrap( Config() - ){default[it]} + ) { default[it] } } /** @@ -57,8 +57,11 @@ fun > Configurable.update(spec: S, action fun > S.createStyle(action: C.() -> Unit): Meta = Config().also { update(it, action) } -fun MetaItem<*>.spec(spec: Specification): T? = node?.let { spec.wrap( - Config(), it) } +fun MetaItem<*>.spec(spec: Specification): T? = node?.let { + spec.wrap( + Config(), it + ) +} @JvmName("configSpec") fun MetaItem.spec(spec: Specification): T? = node?.let { spec.wrap(it) } diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/Value.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/Value.kt index ccc92d50..28a5838c 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/Value.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/Value.kt @@ -44,7 +44,7 @@ interface Value { * get this value represented as List */ val list: List - get() = if(this == Null) emptyList() else listOf(this) + get() = if (this == Null) emptyList() else listOf(this) 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 > E.asValue(): Value = EnumValue(this) + /** * Create Value from String using closest match conversion diff --git a/dataforge-meta/src/commonTest/kotlin/hep/dataforge/meta/MetaDelegateTest.kt b/dataforge-meta/src/commonTest/kotlin/hep/dataforge/meta/MetaDelegateTest.kt index 4cf2002f..277c2a6c 100644 --- a/dataforge-meta/src/commonTest/kotlin/hep/dataforge/meta/MetaDelegateTest.kt +++ b/dataforge-meta/src/commonTest/kotlin/hep/dataforge/meta/MetaDelegateTest.kt @@ -13,15 +13,17 @@ class MetaDelegateTest { class InnerSpec : Scheme() { var innerValue by string() - companion object: SchemeSpec(::InnerSpec) + + companion object : SchemeSpec(::InnerSpec) } class TestScheme : Scheme() { var myValue by string() var safeValue by double(2.2) - var enumValue by enum(TestEnum.YES) + var enumValue by enum(TestEnum.YES) { enum() } var inner by spec(InnerSpec) - companion object: SchemeSpec(::TestScheme) + + companion object : SchemeSpec(::TestScheme) } @Test diff --git a/dataforge-tables/src/commonMain/kotlin/hep/dataforge/tables/ColumnScheme.kt b/dataforge-tables/src/commonMain/kotlin/hep/dataforge/tables/ColumnScheme.kt index 2b65b234..7d364784 100644 --- a/dataforge-tables/src/commonMain/kotlin/hep/dataforge/tables/ColumnScheme.kt +++ b/dataforge-tables/src/commonMain/kotlin/hep/dataforge/tables/ColumnScheme.kt @@ -1,5 +1,6 @@ package hep.dataforge.tables +import hep.dataforge.meta.enum import hep.dataforge.meta.scheme.Scheme import hep.dataforge.meta.scheme.SchemeSpec import hep.dataforge.meta.scheme.enum @@ -13,5 +14,5 @@ open class ColumnScheme : Scheme() { } class ValueColumnScheme : ColumnScheme() { - var valueType by enum(ValueType.STRING) + var valueType by enum(ValueType.STRING){enum()} } \ No newline at end of file