Fix MetaItem equality
This commit is contained in:
parent
fc0454d339
commit
086fcd441b
@ -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")
|
||||
|
@ -16,8 +16,12 @@ import kotlinx.serialization.*
|
||||
@Serializable
|
||||
sealed class MetaItem<out M : Meta> {
|
||||
|
||||
abstract override fun equals(other: Any?): Boolean
|
||||
|
||||
abstract override fun hashCode(): Int
|
||||
|
||||
@Serializable
|
||||
data class ValueItem(val value: Value) : MetaItem<Nothing>() {
|
||||
class ValueItem(val value: Value) : MetaItem<Nothing>() {
|
||||
override fun toString(): String = value.toString()
|
||||
|
||||
@Serializer(ValueItem::class)
|
||||
@ -30,10 +34,18 @@ sealed class MetaItem<out M : Meta> {
|
||||
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<M : Meta>(@Serializable(MetaSerializer::class) val node: M) : MetaItem<M>() {
|
||||
class NodeItem<M : Meta>(@Serializable(MetaSerializer::class) val node: M) : MetaItem<M>() {
|
||||
//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<out M : Meta> {
|
||||
MetaSerializer.serialize(encoder, value.node)
|
||||
}
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean = node == (other as? NodeItem<*>)?.node
|
||||
|
||||
override fun hashCode(): Int = node.hashCode()
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
@ -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"
|
||||
|
@ -9,13 +9,18 @@ import hep.dataforge.values.*
|
||||
/**
|
||||
* A converter of generic object to and from [MetaItem]
|
||||
*/
|
||||
interface MetaCaster<T : Any> {
|
||||
interface MetaConverter<T : Any> {
|
||||
fun itemToObject(item: MetaItem<*>): T
|
||||
fun objectToMetaItem(obj: T): MetaItem<*>
|
||||
|
||||
companion object {
|
||||
|
||||
val meta = object : MetaCaster<Meta> {
|
||||
val item = object :MetaConverter<MetaItem<*>>{
|
||||
override fun itemToObject(item: MetaItem<*>): MetaItem<*> = item
|
||||
override fun objectToMetaItem(obj: MetaItem<*>): MetaItem<*> = obj
|
||||
}
|
||||
|
||||
val meta = object : MetaConverter<Meta> {
|
||||
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<T : Any> {
|
||||
override fun objectToMetaItem(obj: Meta): MetaItem<*> = MetaItem.NodeItem(obj)
|
||||
}
|
||||
|
||||
val value = object : MetaCaster<Value> {
|
||||
val value = object : MetaConverter<Value> {
|
||||
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<T : Any> {
|
||||
override fun objectToMetaItem(obj: Value): MetaItem<*> = MetaItem.ValueItem(obj)
|
||||
}
|
||||
|
||||
val string = object : MetaCaster<String> {
|
||||
val string = object : MetaConverter<String> {
|
||||
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<T : Any> {
|
||||
override fun objectToMetaItem(obj: String): MetaItem<*> = MetaItem.ValueItem(obj.asValue())
|
||||
}
|
||||
|
||||
val boolean = object : MetaCaster<Boolean> {
|
||||
val boolean = object : MetaConverter<Boolean> {
|
||||
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<T : Any> {
|
||||
override fun objectToMetaItem(obj: Boolean): MetaItem<*> = MetaItem.ValueItem(obj.asValue())
|
||||
}
|
||||
|
||||
val double = object : MetaCaster<Double> {
|
||||
val double = object : MetaConverter<Double> {
|
||||
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<T : Any> {
|
||||
override fun objectToMetaItem(obj: Double): MetaItem<*> = MetaItem.ValueItem(obj.asValue())
|
||||
}
|
||||
|
||||
val int = object : MetaCaster<Int> {
|
||||
val int = object : MetaConverter<Int> {
|
||||
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<T : Any> {
|
||||
}
|
||||
}
|
||||
|
||||
fun <T : Any> MetaCaster<T>.metaToObject(meta: Meta): T = itemToObject(MetaItem.NodeItem(meta))
|
||||
fun <T : Any> MetaCaster<T>.valueToObject(value: Value): T = itemToObject(MetaItem.ValueItem(value))
|
||||
fun <T : Any> MetaConverter<T>.metaToObject(meta: Meta): T = itemToObject(MetaItem.NodeItem(meta))
|
||||
fun <T : Any> MetaConverter<T>.valueToObject(value: Value): T = itemToObject(MetaItem.ValueItem(value))
|
Loading…
Reference in New Issue
Block a user