diff --git a/build.gradle.kts b/build.gradle.kts index d73b0e60..43ea9882 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -6,7 +6,7 @@ plugins { id("scientifik.publish") version toolsVersion apply false } -val dataforgeVersion by extra("0.1.7") +val dataforgeVersion by extra("0.1.8-dev-1") val bintrayRepo by extra("dataforge") val githubProject by extra("dataforge-core") diff --git a/dataforge-io/src/jvmMain/kotlin/hep/dataforge/io/tcp/EnvelopeServer.kt b/dataforge-io/src/jvmMain/kotlin/hep/dataforge/io/tcp/EnvelopeServer.kt index fc61ee49..b9569279 100644 --- a/dataforge-io/src/jvmMain/kotlin/hep/dataforge/io/tcp/EnvelopeServer.kt +++ b/dataforge-io/src/jvmMain/kotlin/hep/dataforge/io/tcp/EnvelopeServer.kt @@ -28,7 +28,7 @@ class EnvelopeServer( fun start() { if (job == null) { logger.info { "Starting envelope server on port $port" } - val job = scope.launch(Dispatchers.IO) { + job = scope.launch(Dispatchers.IO) { val serverSocket = ServerSocket(port) //TODO add handshake and format negotiation while (isActive && !serverSocket.isClosed) { diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/JsonMeta.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/JsonMeta.kt index 89ab1b3e..48ef6c4e 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/JsonMeta.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/JsonMeta.kt @@ -28,7 +28,7 @@ fun Value.toJson(descriptor: ValueDescriptor? = null): JsonElement { } //Use these methods to customize JSON key mapping -private fun String.toJsonKey(descriptor: ItemDescriptor?) = descriptor?.getProperty("jsonName").string ?: toString() +private fun String.toJsonKey(descriptor: ItemDescriptor?) = descriptor?.attributes["jsonName"].string ?: toString() //private fun NodeDescriptor?.getDescriptor(key: String) = this?.items?.get(key) @@ -146,7 +146,7 @@ class JsonMeta(val json: JsonObject, val descriptor: NodeDescriptor? = null) : M @Suppress("UNCHECKED_CAST") private operator fun MutableMap>.set(key: String, value: JsonElement): Unit { val itemDescriptor = descriptor?.items?.get(key) - return when (value) { + when (value) { is JsonPrimitive -> { this[key] = MetaItem.ValueItem(value.toValue(itemDescriptor as? ValueDescriptor)) as MetaItem diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Meta.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Meta.kt index 9650b04e..3aa5b848 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Meta.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Meta.kt @@ -170,8 +170,12 @@ interface MetaNode> : Meta { /** * The same as [Meta.get], but with specific node type */ -@Suppress("UNCHECKED_CAST") -operator fun > M?.get(name: Name): MetaItem? = (this as Meta)[name] as MetaItem? +operator fun > M?.get(name: Name): MetaItem? = if( this == null) { + null +} else { + @Suppress("UNCHECKED_CAST") + (this as Meta).get(name) as MetaItem? +} operator fun > M?.get(key: String): MetaItem? = this[key.toName()] diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/MutableMetaDelegate.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/MutableMetaDelegate.kt index 0cffb3a3..39a022f4 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/MutableMetaDelegate.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/MutableMetaDelegate.kt @@ -99,6 +99,10 @@ fun > M.boolean(default: Boolean, key: Name? = null) = fun > M.number(default: Number, key: Name? = null) = item(default, key).transform { it.number!! } +@JvmName("lazyValue") +fun > M.string(key: Name? = null, default: () -> Value) = + lazyItem(key, default).transform { it.value!! } + @JvmName("lazyString") fun > M.string(key: Name? = null, default: () -> String) = lazyItem(key, default).transform { it.string!! } diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/descriptors/ItemDescriptor.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/descriptors/ItemDescriptor.kt index e025cbb1..02af4374 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/descriptors/ItemDescriptor.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/descriptors/ItemDescriptor.kt @@ -8,28 +8,28 @@ import hep.dataforge.values.Value import hep.dataforge.values.ValueType @DFBuilder -sealed class ItemDescriptor : Scheme() { +sealed class ItemDescriptor(val config: Config) { /** * True if same name siblings with this name are allowed * * @return */ - var multiple: Boolean by boolean(false) + var multiple: Boolean by config.boolean(false) /** * The item description * * @return */ - var info: String? by string() + var info: String? by config.string() /** * Additional attributes of an item. For example validation and widget parameters * * @return */ - var attributes by config() + var attributes by config.node() /** * True if the item is required @@ -43,7 +43,7 @@ sealed class ItemDescriptor : Scheme() { * Configure attributes of the descriptor */ fun ItemDescriptor.attributes(block: Config.() -> Unit) { - (attributes ?: Config().also { this.config = it }).apply(block) + (attributes ?: Config().also { this.attributes = it }).apply(block) } /** @@ -65,28 +65,32 @@ fun ItemDescriptor.validateItem(item: MetaItem<*>?): Boolean { * @author Alexander Nozik */ @DFBuilder -class NodeDescriptor private constructor() : ItemDescriptor() { +class NodeDescriptor(config: Config = Config()) : ItemDescriptor(config) { + init { + config[IS_NODE_KEY] = true + } + /** * True if the node is required * * @return */ - override var required: Boolean by boolean { default == null } + override var required: Boolean by config.boolean { default == null } /** * The default for this node. Null if there is no default. * * @return */ - var default by node() + var default by config.node() val items: Map get() = config.getIndexed(ITEM_KEY).mapValues { (_, item) -> val node = item.node ?: error("Node descriptor must be a node") if (node[IS_NODE_KEY].boolean == true) { - NodeDescriptor.wrap(node) + NodeDescriptor(node) } else { - ValueDescriptor.wrap(node) + ValueDescriptor(node) } } @@ -99,7 +103,7 @@ class NodeDescriptor private constructor() : ItemDescriptor() { it.value.node[IS_NODE_KEY].boolean == true }.associate { (name, item) -> val node = item.node ?: error("Node descriptor must be a node") - name to NodeDescriptor.wrap(node) + name to NodeDescriptor(node) } /** @@ -110,7 +114,7 @@ class NodeDescriptor private constructor() : ItemDescriptor() { it.value.node[IS_NODE_KEY].boolean != true }.associate { (name, item) -> val node = item.node ?: error("Node descriptor must be a node") - name to ValueDescriptor.wrap(node) + name to ValueDescriptor(node) } private fun buildNode(name: Name): NodeDescriptor { @@ -122,7 +126,7 @@ class NodeDescriptor private constructor() : ItemDescriptor() { it[IS_NODE_KEY] = true config[token] = it } - wrap(config) + NodeDescriptor(config) } else -> buildNode(name.first()?.asName()!!).buildNode(name.cutFirst()) } @@ -137,41 +141,37 @@ class NodeDescriptor private constructor() : ItemDescriptor() { config[token] = descriptor.config } - fun defineItem(name: Name, descriptor: ItemDescriptor) { + fun item(name: Name, descriptor: ItemDescriptor) { buildNode(name.cutLast()).newItem(name.last().toString(), descriptor) } - fun defineItem(name: String, descriptor: ItemDescriptor) { - defineItem(name.toName(), descriptor) + fun item(name: String, descriptor: ItemDescriptor) { + item(name.toName(), descriptor) } - fun defineNode(name: Name, block: NodeDescriptor.() -> Unit) { - defineItem(name, NodeDescriptor(block)) + fun node(name: Name, block: NodeDescriptor.() -> Unit) { + item(name, NodeDescriptor().apply(block)) } - fun defineNode(name: String, block: NodeDescriptor.() -> Unit) { - defineNode(name.toName(), block) + fun node(name: String, block: NodeDescriptor.() -> Unit) { + node(name.toName(), block) } - fun defineValue(name: Name, block: ValueDescriptor.() -> Unit) { + fun value(name: Name, block: ValueDescriptor.() -> Unit) { require(name.length >= 1) { "Name length for value descriptor must be non-empty" } - defineItem(name, ValueDescriptor(block)) + item(name, ValueDescriptor().apply(block)) } - fun defineValue(name: String, block: ValueDescriptor.() -> Unit) { - defineValue(name.toName(), block) + fun value(name: String, block: ValueDescriptor.() -> Unit) { + value(name.toName(), block) } - companion object : SchemeSpec(::NodeDescriptor) { + companion object{ val ITEM_KEY = "item".asName() val IS_NODE_KEY = "@isNode".asName() - override fun empty(): NodeDescriptor { - return super.empty().apply { - config[IS_NODE_KEY] = true - } - } + inline operator fun invoke(block: NodeDescriptor.()->Unit) = NodeDescriptor().apply(block) //TODO infer descriptor from spec } @@ -188,6 +188,8 @@ operator fun ItemDescriptor.get(name: Name): ItemDescriptor? { } } +operator fun ItemDescriptor.get(name: String): ItemDescriptor? = get(name.toName()) + /** * A descriptor for meta value * @@ -196,21 +198,21 @@ operator fun ItemDescriptor.get(name: Name): ItemDescriptor? { * @author Alexander Nozik */ @DFBuilder -class ValueDescriptor : ItemDescriptor() { +class ValueDescriptor(config: Config = Config()) : ItemDescriptor(config) { /** * True if the value is required * * @return */ - override var required: Boolean by boolean { default == null } + override var required: Boolean by config.boolean { default == null } /** * The default for this value. Null if there is no default. * * @return */ - var default: Value? by value() + var default: Value? by config.value() fun default(v: Any) { this.default = Value.of(v) @@ -221,7 +223,7 @@ class ValueDescriptor : ItemDescriptor() { * * @return */ - var type: List by item { + var type: List by config.item().transform { it?.value?.list?.map { v -> ValueType.valueOf(v.string) } ?: emptyList() } @@ -248,11 +250,11 @@ class ValueDescriptor : ItemDescriptor() { * * @return */ - var allowedValues: List by value { - it?.list ?: if (type.size == 1 && type[0] === ValueType.BOOLEAN) { - listOf(True, False) - } else { - emptyList() + var allowedValues: List by config.value().transform { + when{ + it?.list != null -> it.list + type.size == 1 && type[0] === ValueType.BOOLEAN -> listOf(True, False) + else -> emptyList() } } @@ -262,6 +264,4 @@ class ValueDescriptor : ItemDescriptor() { fun allow(vararg v: Any) { this.allowedValues = v.map { Value.of(it) } } - - companion object : SchemeSpec(::ValueDescriptor) } \ No newline at end of file diff --git a/dataforge-meta/src/commonTest/kotlin/hep/dataforge/meta/descriptors/DescriptorTest.kt b/dataforge-meta/src/commonTest/kotlin/hep/dataforge/meta/descriptors/DescriptorTest.kt index 77e670c8..9c022637 100644 --- a/dataforge-meta/src/commonTest/kotlin/hep/dataforge/meta/descriptors/DescriptorTest.kt +++ b/dataforge-meta/src/commonTest/kotlin/hep/dataforge/meta/descriptors/DescriptorTest.kt @@ -3,18 +3,19 @@ package hep.dataforge.meta.descriptors import hep.dataforge.values.ValueType import kotlin.test.Test import kotlin.test.assertEquals +import kotlin.test.assertNotNull class DescriptorTest { val descriptor = NodeDescriptor { - defineNode("aNode") { + node("aNode") { info = "A root demo node" - defineValue("b") { + value("b") { info = "b number value" type(ValueType.NUMBER) } - defineNode("otherNode") { - defineValue("otherValue") { + node("otherNode") { + value("otherValue") { type(ValueType.BOOLEAN) default(false) info = "default value" @@ -25,6 +26,8 @@ class DescriptorTest { @Test fun testAllowedValues() { + val child = descriptor["aNode.b"] + assertNotNull(child) val allowed = descriptor.nodes["aNode"]?.values?.get("b")?.allowedValues assertEquals(emptyList(), allowed) } diff --git a/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/TaskBuilder.kt b/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/TaskBuilder.kt index 7f359914..9ce9cf27 100644 --- a/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/TaskBuilder.kt +++ b/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/TaskBuilder.kt @@ -2,9 +2,9 @@ package hep.dataforge.workspace import hep.dataforge.context.Context import hep.dataforge.data.* -import hep.dataforge.meta.descriptors.NodeDescriptor import hep.dataforge.meta.DFBuilder import hep.dataforge.meta.Meta +import hep.dataforge.meta.descriptors.NodeDescriptor import hep.dataforge.meta.get import hep.dataforge.meta.string import hep.dataforge.names.Name @@ -200,7 +200,7 @@ class TaskBuilder(val name: Name, val type: KClass) { * Use DSL to create a descriptor for this task */ fun description(transform: NodeDescriptor.() -> Unit) { - this.descriptor = NodeDescriptor(transform) + this.descriptor = NodeDescriptor().apply(transform) } internal fun build(): GenericTask { @@ -212,7 +212,7 @@ class TaskBuilder(val name: Name, val type: KClass) { return GenericTask( name, type, - descriptor ?: NodeDescriptor.empty(), + descriptor ?: NodeDescriptor(), modelTransform ) { val workspace = this