From 00c9e70c09f9cf4d2eb756119b0d13b09780a74e Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Tue, 19 Mar 2019 20:13:57 +0300 Subject: [PATCH] minor refactoring --- .../kotlin/hep/dataforge/data/DataNode.kt | 13 +++++++------ .../commonMain/kotlin/hep/dataforge/meta/Config.kt | 4 ++-- .../commonMain/kotlin/hep/dataforge/meta/Meta.kt | 5 +++-- .../kotlin/hep/dataforge/meta/MetaBuilder.kt | 4 ++-- .../kotlin/hep/dataforge/meta/MutableMeta.kt | 13 +++++++------ .../commonMain/kotlin/hep/dataforge/names/Name.kt | 2 +- .../src/jsMain/kotlin/hep/dataforge/meta/jsMeta.kt | 7 +++++++ .../kotlin/hep/dataforge/output/html/HtmlOutput.kt | 3 ++- 8 files changed, 31 insertions(+), 20 deletions(-) diff --git a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/DataNode.kt b/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/DataNode.kt index bf8f6f81..53063520 100644 --- a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/DataNode.kt +++ b/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/DataNode.kt @@ -4,6 +4,7 @@ import hep.dataforge.names.Name import hep.dataforge.names.NameToken import hep.dataforge.names.plus import hep.dataforge.names.toName +import hep.dataforge.names.asName /** * A tree-like data structure grouped into the node. All data inside the node must inherit its type @@ -50,23 +51,23 @@ class DataTree internal constructor(private val items: Map? = when (name.length) { 0 -> error("Empty name") 1 -> (items[name.first()] as? DataTreeItem.Value)?.value - else -> getNode(name.first()!!.toName())?.get(name.cutFirst()) + else -> getNode(name.first()!!.asName())?.get(name.cutFirst()) } override fun getNode(name: Name): DataTree? = when (name.length) { 0 -> this 1 -> (items[name.first()] as? DataTreeItem.Node)?.tree - else -> getNode(name.first()!!.toName())?.getNode(name.cutFirst()) + else -> getNode(name.first()!!.asName())?.getNode(name.cutFirst()) } override fun dataSequence(): Sequence>> { return sequence { items.forEach { (head, tree) -> when (tree) { - is DataTreeItem.Value -> yield(head.toName() to tree.value) + is DataTreeItem.Value -> yield(head.asName() to tree.value) is DataTreeItem.Node -> { val subSequence = - tree.tree.dataSequence().map { (name, data) -> (head.toName() + name) to data } + tree.tree.dataSequence().map { (name, data) -> (head.asName() + name) to data } yieldAll(subSequence) } } @@ -78,9 +79,9 @@ class DataTree internal constructor(private val items: Map if (tree is DataTreeItem.Node) { - yield(head.toName() to tree.tree) + yield(head.asName() to tree.tree) val subSequence = - tree.tree.nodeSequence().map { (name, node) -> (head.toName() + name) to node } + tree.tree.nodeSequence().map { (name, node) -> (head.asName() + name) to node } yieldAll(subSequence) } } diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Config.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Config.kt index 79407337..1f9a3522 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Config.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Config.kt @@ -2,7 +2,7 @@ package hep.dataforge.meta import hep.dataforge.names.Name import hep.dataforge.names.NameToken -import hep.dataforge.names.toName +import hep.dataforge.names.asName //TODO add validator to configuration @@ -28,7 +28,7 @@ operator fun Config.get(token: NameToken): MetaItem? = items[token] fun Meta.toConfig(): Config = this as? Config ?: Config().also { builder -> this.items.mapValues { entry -> val item = entry.value - builder[entry.key.toName()] = when (item) { + builder[entry.key.asName()] = when (item) { is MetaItem.ValueItem -> MetaItem.ValueItem(item.value) is MetaItem.NodeItem -> MetaItem.NodeItem(item.node.toConfig()) } 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 a2a158c1..e6133dc5 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Meta.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Meta.kt @@ -7,6 +7,7 @@ import hep.dataforge.names.Name import hep.dataforge.names.NameToken import hep.dataforge.names.plus import hep.dataforge.names.toName +import hep.dataforge.names.asName import hep.dataforge.values.EnumValue import hep.dataforge.values.Value import hep.dataforge.values.boolean @@ -93,8 +94,8 @@ fun Meta.asValueSequence(): Sequence> { return items.asSequence().flatMap { entry -> val item = entry.value when (item) { - is ValueItem -> sequenceOf(entry.key.toName() to item.value) - is NodeItem -> item.node.asValueSequence().map { pair -> (entry.key.toName() + pair.first) to pair.second } + is ValueItem -> sequenceOf(entry.key.asName() to item.value) + is NodeItem -> item.node.asValueSequence().map { pair -> (entry.key.asName() + pair.first) to pair.second } } } } diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/MetaBuilder.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/MetaBuilder.kt index 61310d67..fe6829d4 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/MetaBuilder.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/MetaBuilder.kt @@ -1,7 +1,7 @@ package hep.dataforge.meta import hep.dataforge.names.Name -import hep.dataforge.names.toName +import hep.dataforge.names.asName import hep.dataforge.values.Value /** @@ -38,7 +38,7 @@ fun Meta.builder(): MetaBuilder { return MetaBuilder().also { builder -> items.mapValues { entry -> val item = entry.value - builder[entry.key.toName()] = when (item) { + builder[entry.key.asName()] = when (item) { is MetaItem.ValueItem -> MetaItem.ValueItem(item.value) is MetaItem.NodeItem -> MetaItem.NodeItem(item.node.builder()) } diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/MutableMeta.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/MutableMeta.kt index 1d26b2b7..9600d9b1 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/MutableMeta.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/MutableMeta.kt @@ -4,6 +4,7 @@ import hep.dataforge.names.Name import hep.dataforge.names.NameToken import hep.dataforge.names.plus import hep.dataforge.names.toName +import hep.dataforge.names.asName import hep.dataforge.values.Value internal data class MetaListener( @@ -62,7 +63,7 @@ abstract class MutableMetaNode> : AbstractMetaNode(), } } } - itemChanged(key.toName(), oldItem, newItem) + itemChanged(key.asName(), oldItem, newItem) } /** @@ -103,7 +104,7 @@ fun > MutableMeta.setItem(name: String, item: MetaItem) fun > MutableMeta.setValue(name: String, value: Value) = set(name.toName(), MetaItem.ValueItem(value)) -fun > MutableMeta.setItem(token: NameToken, item: MetaItem?) = set(token.toName(), item) +fun > MutableMeta.setItem(token: NameToken, item: MetaItem?) = set(token.asName(), item) fun > MutableMetaNode.setNode(name: Name, node: Meta) = set(name, MetaItem.NodeItem(wrap(name, node))) @@ -125,7 +126,7 @@ operator fun > M.set(name: Name, value: Any?) { } } -operator fun > M.set(name: NameToken, value: Any?) = set(Name(key), value) +operator fun > M.set(name: NameToken, value: Any?) = set(name.asName(), value) operator fun > M.set(key: String, value: Any?) = set(key.toName(), value) @@ -139,9 +140,9 @@ fun > M.update(meta: Meta) { meta.items.forEach { entry -> val value = entry.value when (value) { - is MetaItem.ValueItem -> setValue(entry.key.toName(), value.value) - is MetaItem.NodeItem -> (this[entry.key.toName()] as? MetaItem.NodeItem)?.node?.update(value.node) - ?: run { setNode(entry.key.toName(), value.node) } + is MetaItem.ValueItem -> setValue(entry.key.asName(), value.value) + is MetaItem.NodeItem -> (this[entry.key.asName()] as? MetaItem.NodeItem)?.node?.update(value.node) + ?: run { setNode(entry.key.asName(), value.node) } } } } diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/names/Name.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/names/Name.kt index d9121af5..759eb425 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/names/Name.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/names/Name.kt @@ -101,7 +101,7 @@ operator fun Name.plus(other: Name): Name = Name(this.tokens + other.tokens) operator fun Name.plus(other: String): Name = this + other.toName() -fun NameToken.toName() = Name(listOf(this)) +fun NameToken.asName() = Name(listOf(this)) val EmptyName = Name(emptyList()) diff --git a/dataforge-meta/src/jsMain/kotlin/hep/dataforge/meta/jsMeta.kt b/dataforge-meta/src/jsMain/kotlin/hep/dataforge/meta/jsMeta.kt index 0858f67a..849900a7 100644 --- a/dataforge-meta/src/jsMain/kotlin/hep/dataforge/meta/jsMeta.kt +++ b/dataforge-meta/src/jsMain/kotlin/hep/dataforge/meta/jsMeta.kt @@ -1,7 +1,14 @@ package hep.dataforge.meta +//TODO add Meta wrapper for dynamic + +/** + * Represent or copy this [Meta] to dynamic object to be passed to JS libraries + */ fun Meta.toDynamic(): dynamic { + //if(this is DynamicMeta) return this.obj + fun MetaItem<*>.toDynamic(): dynamic = when (this) { is MetaItem.ValueItem -> this.value.value.asDynamic() is MetaItem.NodeItem -> this.node.toDynamic() diff --git a/dataforge-output/dataforge-output-html/src/jvmMain/kotlin/hep/dataforge/output/html/HtmlOutput.kt b/dataforge-output/dataforge-output-html/src/jvmMain/kotlin/hep/dataforge/output/html/HtmlOutput.kt index 4eb7ccff..fbb87196 100644 --- a/dataforge-output/dataforge-output-html/src/jvmMain/kotlin/hep/dataforge/output/html/HtmlOutput.kt +++ b/dataforge-output/dataforge-output-html/src/jvmMain/kotlin/hep/dataforge/output/html/HtmlOutput.kt @@ -8,6 +8,7 @@ import hep.dataforge.output.TextRenderer.Companion.TEXT_RENDERER_TYPE import hep.dataforge.output.html.HtmlBuilder.Companion.HTML_CONVERTER_TYPE import hep.dataforge.provider.Type import hep.dataforge.provider.provideAll +import hep.dataforge.provider.provideAllByType import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.html.TagConsumer @@ -28,7 +29,7 @@ class HtmlOutput(override val context: Context, private val consumer: T } else { val value = cache[obj::class] if (value == null) { - val answer = context.provideAll(HTML_CONVERTER_TYPE).filterIsInstance>() + val answer = context.provideAllByType>() .filter { it.type.isInstance(obj) }.firstOrNull() if (answer != null) { cache[obj::class] = answer