From 56e7d5545066649eeec7bd3f90eaeb394dcdc3e8 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sat, 28 Mar 2020 21:23:44 +0300 Subject: [PATCH] Add a Config generator to MetaTransformation --- build.gradle.kts | 2 +- .../dataforge/io/yaml/YamlMetaFormatTest.kt | 2 +- .../kotlin/hep/dataforge/meta/mapMeta.kt | 29 ++++++++++++++----- .../transformations/MetaTransformation.kt | 18 ++++++++++-- .../kotlin/hep/dataforge/meta/MetaTest.kt | 1 + 5 files changed, 41 insertions(+), 11 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index e3972ef4..b5772512 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.6-dev-1") +val dataforgeVersion by extra("0.1.6-dev-2") val bintrayRepo by extra("dataforge") val githubProject by extra("dataforge-core") diff --git a/dataforge-io/dataforge-io-yaml/src/test/kotlin/hep/dataforge/io/yaml/YamlMetaFormatTest.kt b/dataforge-io/dataforge-io-yaml/src/test/kotlin/hep/dataforge/io/yaml/YamlMetaFormatTest.kt index 24fb6593..0bb1c5d0 100644 --- a/dataforge-io/dataforge-io-yaml/src/test/kotlin/hep/dataforge/io/yaml/YamlMetaFormatTest.kt +++ b/dataforge-io/dataforge-io-yaml/src/test/kotlin/hep/dataforge/io/yaml/YamlMetaFormatTest.kt @@ -34,7 +34,7 @@ class YamlMetaFormatTest { assertEquals(meta, meta.seal()) meta.items.keys.forEach { - if (meta[it] != result[it]) error("${meta[it]} != ${result[it]}") + assertEquals(meta[it],result[it],"${meta[it]} != ${result[it]}") } assertEquals(meta, result) diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/mapMeta.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/mapMeta.kt index 0bd792af..ed77f7f0 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/mapMeta.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/mapMeta.kt @@ -1,8 +1,8 @@ -@file:Suppress("UNUSED_PARAMETER") - package hep.dataforge.meta import hep.dataforge.meta.descriptors.NodeDescriptor +import hep.dataforge.names.toName +import hep.dataforge.values.ListValue import hep.dataforge.values.Value /** @@ -18,14 +18,29 @@ fun Meta.toMap(descriptor: NodeDescriptor? = null): Map { } /** - * Convert map of maps to meta + * Convert map of maps to meta. This method will recognize [MetaItem], [Map] and [List] of all mentioned above as value. + * All other values will be converted to values. */ +@DFExperimental fun Map.toMeta(descriptor: NodeDescriptor? = null): Meta = Meta { + @Suppress("UNCHECKED_CAST") + fun toItem(value: Any?): MetaItem<*> = when (value) { + is MetaItem<*> -> value + is Meta -> MetaItem.NodeItem(value) + is Map<*, *> -> MetaItem.NodeItem((value as Map).toMeta()) + else -> MetaItem.ValueItem(Value.of(value)) + } + entries.forEach { (key, value) -> - @Suppress("UNCHECKED_CAST") - when (value) { - is Map<*, *> -> setNode(key, (value as Map).toMeta()) - else -> setValue(key, Value.of(value)) + if (value is List<*>) { + val items = value.map { toItem(it) } + if (items.all { it is MetaItem.ValueItem }) { + setValue(key, ListValue(items.map { it.value!! })) + } else { + setIndexedItems(key.toName(), value.map { toItem(it) }) + } + } else { + setItem(key, toItem(value)) } } } \ No newline at end of file diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/transformations/MetaTransformation.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/transformations/MetaTransformation.kt index d6f3bedf..f02545fe 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/transformations/MetaTransformation.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/transformations/MetaTransformation.kt @@ -22,7 +22,7 @@ interface TransformationRule { meta.sequence().filter { matches(it.first, it.second) }.map { it.first } /** - * Apply transformation for a single item (Node or Value) and return resulting tree with absolute path + * Apply transformation for a single item (Node or Value) to the target */ fun > transformItem(name: Name, item: MetaItem<*>?, target: M): Unit } @@ -89,7 +89,7 @@ inline class MetaTransformation(val transformations: Collection rule.selectItems(source).forEach { name -> @@ -98,6 +98,20 @@ inline class MetaTransformation(val transformations: Collection + rule.selectItems(source).forEach { name -> + rule.transformItem(name, source[name], this) + } + } + + bind(source, this) + } + /** * Transform a meta, replacing all elements found in rules with transformed entries */ diff --git a/dataforge-meta/src/commonTest/kotlin/hep/dataforge/meta/MetaTest.kt b/dataforge-meta/src/commonTest/kotlin/hep/dataforge/meta/MetaTest.kt index 55cc461d..12569bc3 100644 --- a/dataforge-meta/src/commonTest/kotlin/hep/dataforge/meta/MetaTest.kt +++ b/dataforge-meta/src/commonTest/kotlin/hep/dataforge/meta/MetaTest.kt @@ -31,6 +31,7 @@ class MetaTest { assertEquals(meta1, meta2) } + @OptIn(DFExperimental::class) @Test fun metaToMap() { val meta = Meta {