From 086fcd441b58a44797c8d97ab262bb3bbe39ede1 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Fri, 5 Jun 2020 21:08:41 +0300 Subject: [PATCH] Fix MetaItem equality --- build.gradle.kts | 2 +- .../kotlin/hep/dataforge/meta/Meta.kt | 20 ++++++++++++++-- .../dataforge/meta/descriptors/Described.kt | 2 +- .../{MetaCaster.kt => MetaConverter.kt} | 23 +++++++++++-------- 4 files changed, 34 insertions(+), 13 deletions(-) rename dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/transformations/{MetaCaster.kt => MetaConverter.kt} (76%) diff --git a/build.gradle.kts b/build.gradle.kts index 9a48fb23..dd159c0b 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -7,7 +7,7 @@ plugins { id("org.jetbrains.dokka") version "0.10.1" } -val dataforgeVersion by extra("0.1.8-dev-2") +val dataforgeVersion by extra("0.1.8-dev-4") val bintrayRepo by extra("dataforge") val githubProject by extra("dataforge-core") 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 768ddbfa..f0e3725f 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Meta.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Meta.kt @@ -16,8 +16,12 @@ import kotlinx.serialization.* @Serializable sealed class MetaItem { + abstract override fun equals(other: Any?): Boolean + + abstract override fun hashCode(): Int + @Serializable - data class ValueItem(val value: Value) : MetaItem() { + class ValueItem(val value: Value) : MetaItem() { override fun toString(): String = value.toString() @Serializer(ValueItem::class) @@ -30,10 +34,18 @@ sealed class MetaItem { ValueSerializer.serialize(encoder, value.value) } } + + override fun equals(other: Any?): Boolean { + return this.value == (other as? ValueItem)?.value + } + + override fun hashCode(): Int { + return value.hashCode() + } } @Serializable - data class NodeItem(@Serializable(MetaSerializer::class) val node: M) : MetaItem() { + class NodeItem(@Serializable(MetaSerializer::class) val node: M) : MetaItem() { //Fixing serializer for node could cause class cast problems, but it should not since Meta descendants are not serializeable override fun toString(): String = node.toString() @@ -47,6 +59,10 @@ sealed class MetaItem { MetaSerializer.serialize(encoder, value.node) } } + + override fun equals(other: Any?): Boolean = node == (other as? NodeItem<*>)?.node + + override fun hashCode(): Int = node.hashCode() } companion object { diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/descriptors/Described.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/descriptors/Described.kt index 0a09a0fb..ac8ba74a 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/descriptors/Described.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/descriptors/Described.kt @@ -4,7 +4,7 @@ package hep.dataforge.meta.descriptors * An object which provides its descriptor */ interface Described { - val descriptor: NodeDescriptor? + val descriptor: ItemDescriptor? companion object { const val DESCRIPTOR_NODE = "@descriptor" diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/transformations/MetaCaster.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/transformations/MetaConverter.kt similarity index 76% rename from dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/transformations/MetaCaster.kt rename to dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/transformations/MetaConverter.kt index 21785980..bd967a85 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/transformations/MetaCaster.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/transformations/MetaConverter.kt @@ -9,13 +9,18 @@ import hep.dataforge.values.* /** * A converter of generic object to and from [MetaItem] */ -interface MetaCaster { +interface MetaConverter { fun itemToObject(item: MetaItem<*>): T fun objectToMetaItem(obj: T): MetaItem<*> companion object { - val meta = object : MetaCaster { + val item = object :MetaConverter>{ + override fun itemToObject(item: MetaItem<*>): MetaItem<*> = item + override fun objectToMetaItem(obj: MetaItem<*>): MetaItem<*> = obj + } + + val meta = object : MetaConverter { override fun itemToObject(item: MetaItem<*>): Meta = when (item) { is MetaItem.NodeItem -> item.node is MetaItem.ValueItem -> item.value.toMeta() @@ -24,7 +29,7 @@ interface MetaCaster { override fun objectToMetaItem(obj: Meta): MetaItem<*> = MetaItem.NodeItem(obj) } - val value = object : MetaCaster { + val value = object : MetaConverter { override fun itemToObject(item: MetaItem<*>): Value = when (item) { is MetaItem.NodeItem -> item.node[Meta.VALUE_KEY].value ?: error("Can't convert node to a value") is MetaItem.ValueItem -> item.value @@ -33,7 +38,7 @@ interface MetaCaster { override fun objectToMetaItem(obj: Value): MetaItem<*> = MetaItem.ValueItem(obj) } - val string = object : MetaCaster { + val string = object : MetaConverter { override fun itemToObject(item: MetaItem<*>): String = when (item) { is MetaItem.NodeItem -> item.node[Meta.VALUE_KEY].value ?: error("Can't convert node to a value") is MetaItem.ValueItem -> item.value @@ -42,7 +47,7 @@ interface MetaCaster { override fun objectToMetaItem(obj: String): MetaItem<*> = MetaItem.ValueItem(obj.asValue()) } - val boolean = object : MetaCaster { + val boolean = object : MetaConverter { override fun itemToObject(item: MetaItem<*>): Boolean = when (item) { is MetaItem.NodeItem -> item.node[Meta.VALUE_KEY].value ?: error("Can't convert node to a value") is MetaItem.ValueItem -> item.value @@ -51,7 +56,7 @@ interface MetaCaster { override fun objectToMetaItem(obj: Boolean): MetaItem<*> = MetaItem.ValueItem(obj.asValue()) } - val double = object : MetaCaster { + val double = object : MetaConverter { override fun itemToObject(item: MetaItem<*>): Double = when (item) { is MetaItem.NodeItem -> item.node[Meta.VALUE_KEY].value ?: error("Can't convert node to a value") is MetaItem.ValueItem -> item.value @@ -60,7 +65,7 @@ interface MetaCaster { override fun objectToMetaItem(obj: Double): MetaItem<*> = MetaItem.ValueItem(obj.asValue()) } - val int = object : MetaCaster { + val int = object : MetaConverter { override fun itemToObject(item: MetaItem<*>): Int = when (item) { is MetaItem.NodeItem -> item.node[Meta.VALUE_KEY].value ?: error("Can't convert node to a value") is MetaItem.ValueItem -> item.value @@ -71,5 +76,5 @@ interface MetaCaster { } } -fun MetaCaster.metaToObject(meta: Meta): T = itemToObject(MetaItem.NodeItem(meta)) -fun MetaCaster.valueToObject(value: Value): T = itemToObject(MetaItem.ValueItem(value)) +fun MetaConverter.metaToObject(meta: Meta): T = itemToObject(MetaItem.NodeItem(meta)) +fun MetaConverter.valueToObject(value: Value): T = itemToObject(MetaItem.ValueItem(value))