Moved ValueItem and NodeItem to the top level

This commit is contained in:
Alexander Nozik 2020-12-24 10:48:51 +03:00
parent 1c89543d73
commit 6389a25aaf
33 changed files with 431 additions and 439 deletions

View File

@ -15,6 +15,8 @@
- Context logging moved to the extension
- `number` and `string` methods on `Value` moved to extensions (breaking change)
- \[Major breaking change\] Schemes and configurables us `MutableItemProvider` instead of `Config`
- \[Major breaking change\] `MetaItem` renamed to `TypedMetaItem` and `MetaItem` is now an alias for `TypedMetaItem<*>`
- \[Major breaking change\] Moved `NodeItem` and `ValueItem` to a top level
### Deprecated

View File

@ -2,7 +2,7 @@ plugins {
id("ru.mipt.npm.project")
}
val dataforgeVersion by extra("0.2.1-dev-6")
val dataforgeVersion by extra("0.3.0-dev-SNAPSHOT")
val bintrayRepo by extra("dataforge")
val githubProject by extra("dataforge-core")

View File

@ -7,7 +7,7 @@ public final class hep/dataforge/io/BinaryMetaFormat : hep/dataforge/io/MetaForm
public fun invoke (Lhep/dataforge/meta/Meta;Lhep/dataforge/context/Context;)Lhep/dataforge/io/MetaFormat;
public synthetic fun invoke (Lhep/dataforge/meta/Meta;Lhep/dataforge/context/Context;)Ljava/lang/Object;
public fun readMeta (Lkotlinx/io/Input;Lhep/dataforge/meta/descriptors/NodeDescriptor;)Lhep/dataforge/meta/Meta;
public final fun readMetaItem (Lkotlinx/io/Input;)Lhep/dataforge/meta/MetaItem;
public final fun readMetaItem (Lkotlinx/io/Input;)Lhep/dataforge/meta/TypedMetaItem;
public fun readObject (Lkotlinx/io/Input;)Lhep/dataforge/meta/Meta;
public synthetic fun readObject (Lkotlinx/io/Input;)Ljava/lang/Object;
public fun toMeta ()Lhep/dataforge/meta/Meta;
@ -219,8 +219,8 @@ public final class hep/dataforge/io/IOPlugin : hep/dataforge/context/AbstractPlu
public final fun getIoFormatFactories ()Ljava/util/Collection;
public final fun getMetaFormatFactories ()Ljava/util/Collection;
public fun getTag ()Lhep/dataforge/context/PluginTag;
public final fun resolveEnvelopeFormat (Lhep/dataforge/meta/MetaItem;)Lhep/dataforge/io/EnvelopeFormat;
public final fun resolveIOFormat (Lhep/dataforge/meta/MetaItem;Lkotlin/reflect/KClass;)Lhep/dataforge/io/IOFormat;
public final fun resolveEnvelopeFormat (Lhep/dataforge/meta/TypedMetaItem;)Lhep/dataforge/io/EnvelopeFormat;
public final fun resolveIOFormat (Lhep/dataforge/meta/TypedMetaItem;Lkotlin/reflect/KClass;)Lhep/dataforge/io/IOFormat;
public final fun resolveMetaFormat (Ljava/lang/String;Lhep/dataforge/meta/Meta;)Lhep/dataforge/io/MetaFormat;
public final fun resolveMetaFormat (SLhep/dataforge/meta/Meta;)Lhep/dataforge/io/MetaFormat;
public static synthetic fun resolveMetaFormat$default (Lhep/dataforge/io/IOPlugin;Ljava/lang/String;Lhep/dataforge/meta/Meta;ILjava/lang/Object;)Lhep/dataforge/io/MetaFormat;

View File

@ -59,8 +59,8 @@ public final class hep/dataforge/io/yaml/YamlMetaFormat$Companion : hep/dataforg
public final class hep/dataforge/io/yaml/YamlMetaFormatKt {
public static final fun toMeta (Lnet/mamoe/yamlkt/YamlMap;)Lhep/dataforge/meta/Meta;
public static final fun toMetaItem (Lnet/mamoe/yamlkt/YamlElement;Lhep/dataforge/meta/descriptors/ItemDescriptor;)Lhep/dataforge/meta/MetaItem;
public static synthetic fun toMetaItem$default (Lnet/mamoe/yamlkt/YamlElement;Lhep/dataforge/meta/descriptors/ItemDescriptor;ILjava/lang/Object;)Lhep/dataforge/meta/MetaItem;
public static final fun toMetaItem (Lnet/mamoe/yamlkt/YamlElement;Lhep/dataforge/meta/descriptors/ItemDescriptor;)Lhep/dataforge/meta/TypedMetaItem;
public static synthetic fun toMetaItem$default (Lnet/mamoe/yamlkt/YamlElement;Lhep/dataforge/meta/descriptors/ItemDescriptor;ILjava/lang/Object;)Lhep/dataforge/meta/TypedMetaItem;
public static final fun toYaml (Lhep/dataforge/meta/Meta;)Lnet/mamoe/yamlkt/YamlMap;
}

View File

@ -22,10 +22,10 @@ import net.mamoe.yamlkt.*
public fun Meta.toYaml(): YamlMap {
val map: Map<String, Any?> = items.entries.associate { (key, item) ->
key.toString() to when (item) {
is MetaItem.ValueItem -> {
is ValueItem -> {
item.value.value
}
is MetaItem.NodeItem -> {
is NodeItem -> {
item.node.toYaml()
}
}
@ -35,8 +35,8 @@ public fun Meta.toYaml(): YamlMap {
private class YamlMeta(private val yamlMap: YamlMap, private val descriptor: NodeDescriptor? = null) : MetaBase() {
private fun buildItems(): Map<NameToken, MetaItem<*>> {
val map = LinkedHashMap<NameToken, MetaItem<*>>()
private fun buildItems(): Map<NameToken, MetaItem> {
val map = LinkedHashMap<NameToken, MetaItem>()
yamlMap.content.entries.forEach { (key, value) ->
val stringKey = key.toString()
@ -53,7 +53,7 @@ private class YamlMeta(private val yamlMap: YamlMap, private val descriptor: Nod
(it as YamlLiteral).content.parseValue()
}
)
map[token] = MetaItem.ValueItem(listValue)
map[token] = ValueItem(listValue)
} else value.forEachIndexed { index, yamlElement ->
val indexKey = (itemDescriptor as? NodeDescriptor)?.indexKey ?: ItemDescriptor.DEFAULT_INDEX_KEY
val indexValue: String = (yamlElement as? YamlMap)?.getStringOrNull(indexKey)
@ -67,10 +67,10 @@ private class YamlMeta(private val yamlMap: YamlMap, private val descriptor: Nod
return map
}
override val items: Map<NameToken, MetaItem<*>> get() = buildItems()
override val items: Map<NameToken, MetaItem> get() = buildItems()
}
public fun YamlElement.toMetaItem(descriptor: ItemDescriptor? = null): MetaItem<*> = when (this) {
public fun YamlElement.toMetaItem(descriptor: ItemDescriptor? = null): MetaItem = when (this) {
YamlNull -> Null.asMetaItem()
is YamlLiteral -> content.parseValue().asMetaItem()
is YamlMap -> toMeta().asMetaItem()

View File

@ -1,11 +1,8 @@
package hep.dataforge.io
import hep.dataforge.context.Context
import hep.dataforge.meta.Meta
import hep.dataforge.meta.MetaBuilder
import hep.dataforge.meta.MetaItem
import hep.dataforge.meta.*
import hep.dataforge.meta.descriptors.NodeDescriptor
import hep.dataforge.meta.set
import hep.dataforge.values.*
import kotlinx.io.*
import kotlinx.io.text.readUtf8String
@ -22,7 +19,7 @@ public object BinaryMetaFormat : MetaFormat, MetaFormatFactory {
override fun invoke(meta: Meta, context: Context): MetaFormat = this
override fun readMeta(input: Input, descriptor: NodeDescriptor?): Meta {
return (input.readMetaItem() as MetaItem.NodeItem).node
return (input.readMetaItem() as NodeItem).node
}
private fun Output.writeChar(char: Char) = writeByte(char.toByte())
@ -88,10 +85,10 @@ public object BinaryMetaFormat : MetaFormat, MetaFormatFactory {
meta.items.forEach { (key, item) ->
output.writeString(key.toString())
when (item) {
is MetaItem.ValueItem -> {
is ValueItem -> {
output.writeValue(item.value)
}
is MetaItem.NodeItem -> {
is NodeItem -> {
writeObject(output, item.node)
}
}
@ -104,21 +101,21 @@ public object BinaryMetaFormat : MetaFormat, MetaFormatFactory {
}
@Suppress("UNCHECKED_CAST")
public fun Input.readMetaItem(): MetaItem<MetaBuilder> {
public fun Input.readMetaItem(): TypedMetaItem<MetaBuilder> {
return when (val keyChar = readByte().toChar()) {
'S' -> MetaItem.ValueItem(StringValue(readString()))
'N' -> MetaItem.ValueItem(Null)
'+' -> MetaItem.ValueItem(True)
'-' -> MetaItem.ValueItem(True)
's' -> MetaItem.ValueItem(NumberValue(readShort()))
'i' -> MetaItem.ValueItem(NumberValue(readInt()))
'l' -> MetaItem.ValueItem(NumberValue(readInt()))
'f' -> MetaItem.ValueItem(NumberValue(readFloat()))
'd' -> MetaItem.ValueItem(NumberValue(readDouble()))
'S' -> ValueItem(StringValue(readString()))
'N' -> ValueItem(Null)
'+' -> ValueItem(True)
'-' -> ValueItem(True)
's' -> ValueItem(NumberValue(readShort()))
'i' -> ValueItem(NumberValue(readInt()))
'l' -> ValueItem(NumberValue(readInt()))
'f' -> ValueItem(NumberValue(readFloat()))
'd' -> ValueItem(NumberValue(readDouble()))
'L' -> {
val length = readInt()
val list = (1..length).map { (readMetaItem() as MetaItem.ValueItem).value }
MetaItem.ValueItem(Value.of(list))
val list = (1..length).map { (readMetaItem() as ValueItem).value }
ValueItem(Value.of(list))
}
'M' -> {
val length = readInt()
@ -129,7 +126,7 @@ public object BinaryMetaFormat : MetaFormat, MetaFormatFactory {
set(name, item)
}
}
MetaItem.NodeItem(meta)
NodeItem(meta)
}
else -> error("Unknown serialization key character: $keyChar")
}

View File

@ -6,8 +6,8 @@ import hep.dataforge.context.Named
import hep.dataforge.io.IOFormat.Companion.NAME_KEY
import hep.dataforge.io.IOFormatFactory.Companion.IO_FORMAT_TYPE
import hep.dataforge.meta.Meta
import hep.dataforge.meta.MetaItem
import hep.dataforge.meta.MetaRepr
import hep.dataforge.meta.ValueItem
import hep.dataforge.names.Name
import hep.dataforge.names.asName
import hep.dataforge.provider.Type
@ -121,7 +121,7 @@ public object ValueIOFormat : IOFormat<Value>, IOFormatFactory<Value> {
}
override fun readObject(input: Input): Value {
return (BinaryMetaFormat.run { input.readMetaItem() } as? MetaItem.ValueItem)?.value
return (BinaryMetaFormat.run { input.readMetaItem() } as? ValueItem)?.value
?: error("The item is not a value")
}
}

View File

@ -18,7 +18,7 @@ public class IOPlugin(meta: Meta) : AbstractPlugin(meta) {
context.gather<IOFormatFactory<*>>(IO_FORMAT_TYPE).values
}
public fun <T : Any> resolveIOFormat(item: MetaItem<*>, type: KClass<out T>): IOFormat<T>? {
public fun <T : Any> resolveIOFormat(item: MetaItem, type: KClass<out T>): IOFormat<T>? {
val key = item.string ?: item.node[NAME_KEY]?.string ?: error("Format name not defined")
val name = key.toName()
return ioFormatFactories.find { it.name == name }?.let {
@ -46,7 +46,7 @@ public class IOPlugin(meta: Meta) : AbstractPlugin(meta) {
private fun resolveEnvelopeFormat(name: Name, meta: Meta = Meta.EMPTY): EnvelopeFormat? =
envelopeFormatFactories.find { it.name == name }?.invoke(meta, context)
public fun resolveEnvelopeFormat(item: MetaItem<*>): EnvelopeFormat? {
public fun resolveEnvelopeFormat(item: MetaItem): EnvelopeFormat? {
val name = item.string ?: item.node[NAME_KEY]?.string ?: error("Envelope format name not defined")
val meta = item.node[META_KEY].node ?: Meta.EMPTY
return resolveEnvelopeFormat(name.toName(), meta)

View File

@ -1,8 +1,8 @@
package hep.dataforge.io
import hep.dataforge.meta.Meta
import hep.dataforge.meta.MetaItem
import hep.dataforge.meta.MetaSerializer
import hep.dataforge.meta.TypedMetaItem
import hep.dataforge.names.Name
import hep.dataforge.names.toName
import kotlinx.serialization.ExperimentalSerializationApi
@ -52,7 +52,7 @@ class MetaSerializerTest {
@OptIn(ExperimentalSerializationApi::class)
@Test
fun testMetaItemDescriptor() {
val descriptor = MetaItem.serializer(MetaSerializer).descriptor.getElementDescriptor(0)
val descriptor = TypedMetaItem.serializer(MetaSerializer).descriptor.getElementDescriptor(0)
println(descriptor)
}
}

View File

@ -2,15 +2,14 @@ public abstract class hep/dataforge/meta/AbstractMutableMeta : hep/dataforge/met
public fun <init> ()V
protected final fun getChildren ()Ljava/util/Map;
public fun getItems ()Ljava/util/Map;
protected fun replaceItem (Lhep/dataforge/names/NameToken;Lhep/dataforge/meta/MetaItem;Lhep/dataforge/meta/MetaItem;)V
public fun setItem (Lhep/dataforge/names/Name;Lhep/dataforge/meta/MetaItem;)V
protected final fun wrapItem (Lhep/dataforge/meta/MetaItem;)Lhep/dataforge/meta/MetaItem;
protected fun replaceItem (Lhep/dataforge/names/NameToken;Lhep/dataforge/meta/TypedMetaItem;Lhep/dataforge/meta/TypedMetaItem;)V
public fun setItem (Lhep/dataforge/names/Name;Lhep/dataforge/meta/TypedMetaItem;)V
protected abstract fun wrapNode (Lhep/dataforge/meta/Meta;)Lhep/dataforge/meta/MutableMeta;
}
public abstract class hep/dataforge/meta/AbstractTypedMeta : hep/dataforge/meta/MetaBase, hep/dataforge/meta/TypedMeta {
public fun <init> ()V
public fun getItem (Lhep/dataforge/names/Name;)Lhep/dataforge/meta/MetaItem;
public fun getItem (Lhep/dataforge/names/Name;)Lhep/dataforge/meta/TypedMetaItem;
public fun toMeta ()Lhep/dataforge/meta/Meta;
}
@ -35,7 +34,7 @@ public final class hep/dataforge/meta/Config$ConfigSerializer : kotlinx/serializ
public final class hep/dataforge/meta/ConfigKt {
public static final fun asConfig (Lhep/dataforge/meta/Meta;)Lhep/dataforge/meta/Config;
public static final fun get (Lhep/dataforge/meta/Config;Lhep/dataforge/names/NameToken;)Lhep/dataforge/meta/MetaItem;
public static final fun get (Lhep/dataforge/meta/Config;Lhep/dataforge/names/NameToken;)Lhep/dataforge/meta/TypedMetaItem;
public static final fun toConfig (Lhep/dataforge/meta/Meta;)Lhep/dataforge/meta/Config;
}
@ -118,7 +117,7 @@ public final class hep/dataforge/meta/ItemListener {
public abstract interface class hep/dataforge/meta/ItemProvider {
public static final field Companion Lhep/dataforge/meta/ItemProvider$Companion;
public abstract fun getItem (Lhep/dataforge/names/Name;)Lhep/dataforge/meta/MetaItem;
public abstract fun getItem (Lhep/dataforge/names/Name;)Lhep/dataforge/meta/TypedMetaItem;
}
public final class hep/dataforge/meta/ItemProvider$Companion {
@ -126,8 +125,8 @@ public final class hep/dataforge/meta/ItemProvider$Companion {
}
public final class hep/dataforge/meta/ItemProviderKt {
public static final fun get (Lhep/dataforge/meta/ItemProvider;Lhep/dataforge/names/Name;)Lhep/dataforge/meta/MetaItem;
public static final fun get (Lhep/dataforge/meta/ItemProvider;Ljava/lang/String;)Lhep/dataforge/meta/MetaItem;
public static final fun get (Lhep/dataforge/meta/ItemProvider;Lhep/dataforge/names/Name;)Lhep/dataforge/meta/TypedMetaItem;
public static final fun get (Lhep/dataforge/meta/ItemProvider;Ljava/lang/String;)Lhep/dataforge/meta/TypedMetaItem;
public static final fun getChild (Lhep/dataforge/meta/ItemProvider;Lhep/dataforge/names/Name;)Lhep/dataforge/meta/ItemProvider;
public static final fun getChild (Lhep/dataforge/meta/ItemProvider;Ljava/lang/String;)Lhep/dataforge/meta/ItemProvider;
public static final fun getIndexed (Lhep/dataforge/meta/ItemProvider;Lhep/dataforge/names/Name;)Ljava/util/Map;
@ -154,8 +153,8 @@ public final class hep/dataforge/meta/JsonMetaKt {
public static synthetic fun toJson$default (Lhep/dataforge/values/Value;Lhep/dataforge/meta/descriptors/ValueDescriptor;ILjava/lang/Object;)Lkotlinx/serialization/json/JsonElement;
public static final fun toMeta (Lkotlinx/serialization/json/JsonObject;Lhep/dataforge/meta/descriptors/NodeDescriptor;)Lhep/dataforge/meta/JsonMeta;
public static synthetic fun toMeta$default (Lkotlinx/serialization/json/JsonObject;Lhep/dataforge/meta/descriptors/NodeDescriptor;ILjava/lang/Object;)Lhep/dataforge/meta/JsonMeta;
public static final fun toMetaItem (Lkotlinx/serialization/json/JsonElement;Lhep/dataforge/meta/descriptors/ItemDescriptor;)Lhep/dataforge/meta/MetaItem;
public static synthetic fun toMetaItem$default (Lkotlinx/serialization/json/JsonElement;Lhep/dataforge/meta/descriptors/ItemDescriptor;ILjava/lang/Object;)Lhep/dataforge/meta/MetaItem;
public static final fun toMetaItem (Lkotlinx/serialization/json/JsonElement;Lhep/dataforge/meta/descriptors/ItemDescriptor;)Lhep/dataforge/meta/TypedMetaItem;
public static synthetic fun toMetaItem$default (Lkotlinx/serialization/json/JsonElement;Lhep/dataforge/meta/descriptors/ItemDescriptor;ILjava/lang/Object;)Lhep/dataforge/meta/TypedMetaItem;
public static final fun toValue (Lkotlinx/serialization/json/JsonPrimitive;Lhep/dataforge/meta/descriptors/ValueDescriptor;)Lhep/dataforge/values/Value;
}
@ -174,7 +173,7 @@ public final class hep/dataforge/meta/Laminate$Companion {
public final class hep/dataforge/meta/LaminateKt {
public static final fun Laminate ([Lhep/dataforge/meta/Meta;)Lhep/dataforge/meta/Laminate;
public static final fun getFirst (Lhep/dataforge/meta/Laminate;Lhep/dataforge/names/Name;)Lhep/dataforge/meta/MetaItem;
public static final fun getFirst (Lhep/dataforge/meta/Laminate;Lhep/dataforge/names/Name;)Lhep/dataforge/meta/TypedMetaItem;
public static final fun withBottom (Lhep/dataforge/meta/Laminate;Lhep/dataforge/meta/Meta;)Lhep/dataforge/meta/Laminate;
public static final fun withTop (Lhep/dataforge/meta/Laminate;Lhep/dataforge/meta/Meta;)Lhep/dataforge/meta/Laminate;
}
@ -191,7 +190,7 @@ public abstract interface class hep/dataforge/meta/Meta : hep/dataforge/meta/Ite
public static final field TYPE Ljava/lang/String;
public static final field VALUE_KEY Ljava/lang/String;
public abstract fun equals (Ljava/lang/Object;)Z
public abstract fun getItem (Lhep/dataforge/names/Name;)Lhep/dataforge/meta/MetaItem;
public abstract fun getItem (Lhep/dataforge/names/Name;)Lhep/dataforge/meta/TypedMetaItem;
public abstract fun getItems ()Ljava/util/Map;
public abstract fun hashCode ()I
public abstract fun toMeta ()Lhep/dataforge/meta/Meta;
@ -205,14 +204,14 @@ public final class hep/dataforge/meta/Meta$Companion {
}
public final class hep/dataforge/meta/Meta$DefaultImpls {
public static fun getItem (Lhep/dataforge/meta/Meta;Lhep/dataforge/names/Name;)Lhep/dataforge/meta/MetaItem;
public static fun getItem (Lhep/dataforge/meta/Meta;Lhep/dataforge/names/Name;)Lhep/dataforge/meta/TypedMetaItem;
public static fun toMeta (Lhep/dataforge/meta/Meta;)Lhep/dataforge/meta/Meta;
}
public abstract class hep/dataforge/meta/MetaBase : hep/dataforge/meta/Meta {
public fun <init> ()V
public fun equals (Ljava/lang/Object;)Z
public fun getItem (Lhep/dataforge/names/Name;)Lhep/dataforge/meta/MetaItem;
public fun getItem (Lhep/dataforge/names/Name;)Lhep/dataforge/meta/TypedMetaItem;
public fun hashCode ()I
public fun toMeta ()Lhep/dataforge/meta/Meta;
public fun toString ()Ljava/lang/String;
@ -230,8 +229,8 @@ public final class hep/dataforge/meta/MetaBuilder : hep/dataforge/meta/AbstractM
public final fun put (Lhep/dataforge/names/Name;Ljava/lang/String;)V
public final fun put (Lhep/dataforge/names/Name;Lkotlin/jvm/functions/Function1;)V
public final fun put (Ljava/lang/String;Lhep/dataforge/meta/Meta;)V
public final fun put (Ljava/lang/String;Lhep/dataforge/meta/MetaItem;)V
public final fun put (Ljava/lang/String;Lhep/dataforge/meta/MetaRepr;)V
public final fun put (Ljava/lang/String;Lhep/dataforge/meta/TypedMetaItem;)V
public final fun put (Ljava/lang/String;Lhep/dataforge/values/Value;)V
public final fun put (Ljava/lang/String;Ljava/lang/Boolean;)V
public final fun put (Ljava/lang/String;Ljava/lang/Enum;)V
@ -253,73 +252,20 @@ public final class hep/dataforge/meta/MetaBuilderKt {
public static final fun builder (Lhep/dataforge/meta/Meta;)Lhep/dataforge/meta/MetaBuilder;
}
public abstract class hep/dataforge/meta/MetaItem {
public static final field Companion Lhep/dataforge/meta/MetaItem$Companion;
public abstract fun equals (Ljava/lang/Object;)Z
public abstract fun hashCode ()I
}
public final class hep/dataforge/meta/MetaItem$Companion {
public final fun of (Ljava/lang/Object;)Lhep/dataforge/meta/MetaItem;
public final fun serializer (Lkotlinx/serialization/KSerializer;)Lkotlinx/serialization/KSerializer;
}
public final class hep/dataforge/meta/MetaItem$NodeItem : hep/dataforge/meta/MetaItem {
public static final field Companion Lhep/dataforge/meta/MetaItem$NodeItem$Companion;
public fun <init> (Lhep/dataforge/meta/Meta;)V
public fun equals (Ljava/lang/Object;)Z
public final fun getNode ()Lhep/dataforge/meta/Meta;
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}
public final class hep/dataforge/meta/MetaItem$NodeItem$Companion {
public final fun serializer (Lkotlinx/serialization/KSerializer;)Lkotlinx/serialization/KSerializer;
}
public final class hep/dataforge/meta/MetaItem$ValueItem : hep/dataforge/meta/MetaItem {
public static final field Companion Lhep/dataforge/meta/MetaItem$ValueItem$Companion;
public fun <init> (Lhep/dataforge/values/Value;)V
public fun equals (Ljava/lang/Object;)Z
public final fun getValue ()Lhep/dataforge/values/Value;
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}
public final class hep/dataforge/meta/MetaItem$ValueItem$Companion {
public final fun serializer ()Lkotlinx/serialization/KSerializer;
}
public final class hep/dataforge/meta/MetaItemKt {
public static final fun asMetaItem (Lhep/dataforge/meta/Meta;)Lhep/dataforge/meta/MetaItem$NodeItem;
public static final fun asMetaItem (Lhep/dataforge/values/Value;)Lhep/dataforge/meta/MetaItem$ValueItem;
public static final fun getBoolean (Lhep/dataforge/meta/MetaItem;)Ljava/lang/Boolean;
public static final fun getDouble (Lhep/dataforge/meta/MetaItem;)Ljava/lang/Double;
public static final fun getFloat (Lhep/dataforge/meta/MetaItem;)Ljava/lang/Float;
public static final fun getInt (Lhep/dataforge/meta/MetaItem;)Ljava/lang/Integer;
public static final fun getLong (Lhep/dataforge/meta/MetaItem;)Ljava/lang/Long;
public static final fun getNode (Lhep/dataforge/meta/MetaItem;)Lhep/dataforge/meta/Meta;
public static final fun getNumber (Lhep/dataforge/meta/MetaItem;)Ljava/lang/Number;
public static final fun getShort (Lhep/dataforge/meta/MetaItem;)Ljava/lang/Short;
public static final fun getString (Lhep/dataforge/meta/MetaItem;)Ljava/lang/String;
public static final fun getStringList (Lhep/dataforge/meta/MetaItem;)Ljava/util/List;
public static final fun getValue (Lhep/dataforge/meta/MetaItem;)Lhep/dataforge/values/Value;
}
public final class hep/dataforge/meta/MetaItemSerializer : kotlinx/serialization/KSerializer {
public static final field INSTANCE Lhep/dataforge/meta/MetaItemSerializer;
public fun deserialize (Lkotlinx/serialization/encoding/Decoder;)Lhep/dataforge/meta/MetaItem;
public fun deserialize (Lkotlinx/serialization/encoding/Decoder;)Lhep/dataforge/meta/TypedMetaItem;
public synthetic fun deserialize (Lkotlinx/serialization/encoding/Decoder;)Ljava/lang/Object;
public fun getDescriptor ()Lkotlinx/serialization/descriptors/SerialDescriptor;
public fun serialize (Lkotlinx/serialization/encoding/Encoder;Lhep/dataforge/meta/MetaItem;)V
public fun serialize (Lkotlinx/serialization/encoding/Encoder;Lhep/dataforge/meta/TypedMetaItem;)V
public synthetic fun serialize (Lkotlinx/serialization/encoding/Encoder;Ljava/lang/Object;)V
}
public final class hep/dataforge/meta/MetaKt {
public static final fun get (Lhep/dataforge/meta/Meta;Lhep/dataforge/names/NameToken;)Lhep/dataforge/meta/MetaItem;
public static final fun get (Lhep/dataforge/meta/TypedMeta;Lhep/dataforge/names/Name;)Lhep/dataforge/meta/MetaItem;
public static final fun get (Lhep/dataforge/meta/TypedMeta;Lhep/dataforge/names/NameToken;)Lhep/dataforge/meta/MetaItem;
public static final fun get (Lhep/dataforge/meta/TypedMeta;Ljava/lang/String;)Lhep/dataforge/meta/MetaItem;
public static final fun get (Lhep/dataforge/meta/Meta;Lhep/dataforge/names/NameToken;)Lhep/dataforge/meta/TypedMetaItem;
public static final fun get (Lhep/dataforge/meta/TypedMeta;Lhep/dataforge/names/Name;)Lhep/dataforge/meta/TypedMetaItem;
public static final fun get (Lhep/dataforge/meta/TypedMeta;Lhep/dataforge/names/NameToken;)Lhep/dataforge/meta/TypedMetaItem;
public static final fun get (Lhep/dataforge/meta/TypedMeta;Ljava/lang/String;)Lhep/dataforge/meta/TypedMetaItem;
public static final fun isEmpty (Lhep/dataforge/meta/Meta;)Z
public static final fun itemSequence (Lhep/dataforge/meta/Meta;)Lkotlin/sequences/Sequence;
public static final fun iterator (Lhep/dataforge/meta/Meta;)Ljava/util/Iterator;
@ -342,7 +288,7 @@ public final class hep/dataforge/meta/MetaSerializer : kotlinx/serialization/KSe
public final class hep/dataforge/meta/MetaWithDefault : hep/dataforge/meta/MetaBase {
public fun <init> (Lhep/dataforge/meta/Meta;Lhep/dataforge/meta/ItemProvider;)V
public final fun getDefault ()Lhep/dataforge/meta/ItemProvider;
public fun getItem (Lhep/dataforge/names/Name;)Lhep/dataforge/meta/MetaItem;
public fun getItem (Lhep/dataforge/names/Name;)Lhep/dataforge/meta/TypedMetaItem;
public fun getItems ()Ljava/util/Map;
public final fun getMeta ()Lhep/dataforge/meta/Meta;
}
@ -410,7 +356,7 @@ public final class hep/dataforge/meta/MutableItemDelegateKt {
}
public abstract interface class hep/dataforge/meta/MutableItemProvider : hep/dataforge/meta/ItemProvider {
public abstract fun setItem (Lhep/dataforge/names/Name;Lhep/dataforge/meta/MetaItem;)V
public abstract fun setItem (Lhep/dataforge/names/Name;Lhep/dataforge/meta/TypedMetaItem;)V
}
public final class hep/dataforge/meta/MutableItemProviderKt {
@ -420,13 +366,13 @@ public final class hep/dataforge/meta/MutableItemProviderKt {
public static final fun remove (Lhep/dataforge/meta/MutableItemProvider;Lhep/dataforge/names/Name;)V
public static final fun remove (Lhep/dataforge/meta/MutableItemProvider;Ljava/lang/String;)V
public static final fun set (Lhep/dataforge/meta/MutableItemProvider;Lhep/dataforge/names/Name;Lhep/dataforge/meta/Meta;)V
public static final fun set (Lhep/dataforge/meta/MutableItemProvider;Lhep/dataforge/names/Name;Lhep/dataforge/meta/MetaItem;)V
public static final fun set (Lhep/dataforge/meta/MutableItemProvider;Lhep/dataforge/names/Name;Lhep/dataforge/meta/TypedMetaItem;)V
public static final fun set (Lhep/dataforge/meta/MutableItemProvider;Lhep/dataforge/names/Name;Lhep/dataforge/values/Value;)V
public static final fun set (Lhep/dataforge/meta/MutableItemProvider;Lhep/dataforge/names/Name;Ljava/lang/Iterable;)V
public static final fun set (Lhep/dataforge/meta/MutableItemProvider;Lhep/dataforge/names/Name;Ljava/lang/Object;)V
public static final fun set (Lhep/dataforge/meta/MutableItemProvider;Lhep/dataforge/names/NameToken;Ljava/lang/Object;)V
public static final fun set (Lhep/dataforge/meta/MutableItemProvider;Ljava/lang/String;Lhep/dataforge/meta/Meta;)V
public static final fun set (Lhep/dataforge/meta/MutableItemProvider;Ljava/lang/String;Lhep/dataforge/meta/MetaItem;)V
public static final fun set (Lhep/dataforge/meta/MutableItemProvider;Ljava/lang/String;Lhep/dataforge/meta/TypedMetaItem;)V
public static final fun set (Lhep/dataforge/meta/MutableItemProvider;Ljava/lang/String;Ljava/lang/Iterable;)V
public static final fun set (Lhep/dataforge/meta/MutableItemProvider;Ljava/lang/String;Ljava/lang/Object;)V
public static final fun set (Lhep/dataforge/meta/MutableItemProvider;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Object;)V
@ -443,7 +389,7 @@ public abstract interface class hep/dataforge/meta/MutableMeta : hep/dataforge/m
}
public final class hep/dataforge/meta/MutableMeta$DefaultImpls {
public static fun getItem (Lhep/dataforge/meta/MutableMeta;Lhep/dataforge/names/Name;)Lhep/dataforge/meta/MetaItem;
public static fun getItem (Lhep/dataforge/meta/MutableMeta;Lhep/dataforge/names/Name;)Lhep/dataforge/meta/TypedMetaItem;
public static fun toMeta (Lhep/dataforge/meta/MutableMeta;)Lhep/dataforge/meta/Meta;
}
@ -453,6 +399,19 @@ public final class hep/dataforge/meta/MutableMetaKt {
public static final fun edit (Lhep/dataforge/meta/AbstractMutableMeta;Lhep/dataforge/names/Name;Lkotlin/jvm/functions/Function1;)V
}
public final class hep/dataforge/meta/NodeItem : hep/dataforge/meta/TypedMetaItem {
public static final field Companion Lhep/dataforge/meta/NodeItem$Companion;
public fun <init> (Lhep/dataforge/meta/Meta;)V
public fun equals (Ljava/lang/Object;)Z
public final fun getNode ()Lhep/dataforge/meta/Meta;
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}
public final class hep/dataforge/meta/NodeItem$Companion {
public final fun serializer (Lkotlinx/serialization/KSerializer;)Lkotlinx/serialization/KSerializer;
}
public abstract interface class hep/dataforge/meta/ObservableItemProvider : hep/dataforge/meta/ItemProvider {
public abstract fun onChange (Ljava/lang/Object;Lkotlin/jvm/functions/Function3;)V
public abstract fun removeListener (Ljava/lang/Object;)V
@ -465,13 +424,13 @@ public class hep/dataforge/meta/Scheme : hep/dataforge/meta/MetaRepr, hep/datafo
public fun getDefaultLayer ()Lhep/dataforge/meta/Meta;
public synthetic fun getDescriptor ()Lhep/dataforge/meta/descriptors/ItemDescriptor;
public fun getDescriptor ()Lhep/dataforge/meta/descriptors/NodeDescriptor;
public fun getItem (Lhep/dataforge/names/Name;)Lhep/dataforge/meta/MetaItem;
public fun getItem (Lhep/dataforge/names/Name;)Lhep/dataforge/meta/TypedMetaItem;
public final fun getItems ()Lhep/dataforge/meta/MutableItemProvider;
public final fun isEmpty ()Z
public fun setItem (Lhep/dataforge/names/Name;Lhep/dataforge/meta/MetaItem;)V
public fun setItem (Lhep/dataforge/names/Name;Lhep/dataforge/meta/TypedMetaItem;)V
public fun toMeta ()Lhep/dataforge/meta/Laminate;
public synthetic fun toMeta ()Lhep/dataforge/meta/Meta;
public fun validateItem (Lhep/dataforge/names/Name;Lhep/dataforge/meta/MetaItem;)Z
public fun validateItem (Lhep/dataforge/names/Name;Lhep/dataforge/meta/TypedMetaItem;)Z
}
public final class hep/dataforge/meta/SchemeKt {
@ -502,7 +461,7 @@ public final class hep/dataforge/meta/SealedMeta : hep/dataforge/meta/AbstractTy
public final class hep/dataforge/meta/SealedMetaKt {
public static final fun seal (Lhep/dataforge/meta/Meta;)Lhep/dataforge/meta/SealedMeta;
public static final fun seal (Lhep/dataforge/meta/MetaItem;)Lhep/dataforge/meta/MetaItem;
public static final fun seal (Lhep/dataforge/meta/TypedMetaItem;)Lhep/dataforge/meta/TypedMetaItem;
}
public abstract interface class hep/dataforge/meta/Specification {
@ -525,19 +484,59 @@ public final class hep/dataforge/meta/SpecificationKt {
public static synthetic fun spec$default (Lhep/dataforge/meta/MutableItemProvider;Lhep/dataforge/meta/Specification;Lhep/dataforge/names/Name;ILjava/lang/Object;)Lkotlin/properties/ReadWriteProperty;
public static final fun update (Lhep/dataforge/meta/Config;Lhep/dataforge/meta/Specification;Lkotlin/jvm/functions/Function1;)Lhep/dataforge/meta/MutableItemProvider;
public static final fun update (Lhep/dataforge/meta/Configurable;Lhep/dataforge/meta/Specification;Lkotlin/jvm/functions/Function1;)Lhep/dataforge/meta/Configurable;
public static final fun withSpec (Lhep/dataforge/meta/MetaItem;Lhep/dataforge/meta/Specification;)Lhep/dataforge/meta/MutableItemProvider;
public static final fun withSpec (Lhep/dataforge/meta/TypedMetaItem;Lhep/dataforge/meta/Specification;)Lhep/dataforge/meta/MutableItemProvider;
}
public abstract interface class hep/dataforge/meta/TypedMeta : hep/dataforge/meta/Meta {
public abstract fun getItem (Lhep/dataforge/names/Name;)Lhep/dataforge/meta/MetaItem;
public abstract fun getItem (Lhep/dataforge/names/Name;)Lhep/dataforge/meta/TypedMetaItem;
public abstract fun getItems ()Ljava/util/Map;
}
public final class hep/dataforge/meta/TypedMeta$DefaultImpls {
public static fun getItem (Lhep/dataforge/meta/TypedMeta;Lhep/dataforge/names/Name;)Lhep/dataforge/meta/MetaItem;
public static fun getItem (Lhep/dataforge/meta/TypedMeta;Lhep/dataforge/names/Name;)Lhep/dataforge/meta/TypedMetaItem;
public static fun toMeta (Lhep/dataforge/meta/TypedMeta;)Lhep/dataforge/meta/Meta;
}
public abstract class hep/dataforge/meta/TypedMetaItem {
public static final field Companion Lhep/dataforge/meta/TypedMetaItem$Companion;
public abstract fun equals (Ljava/lang/Object;)Z
public abstract fun hashCode ()I
}
public final class hep/dataforge/meta/TypedMetaItem$Companion {
public final fun of (Ljava/lang/Object;)Lhep/dataforge/meta/TypedMetaItem;
public final fun serializer (Lkotlinx/serialization/KSerializer;)Lkotlinx/serialization/KSerializer;
}
public final class hep/dataforge/meta/TypedMetaItemKt {
public static final fun asMetaItem (Lhep/dataforge/meta/Meta;)Lhep/dataforge/meta/NodeItem;
public static final fun asMetaItem (Lhep/dataforge/values/Value;)Lhep/dataforge/meta/ValueItem;
public static final fun getBoolean (Lhep/dataforge/meta/TypedMetaItem;)Ljava/lang/Boolean;
public static final fun getDouble (Lhep/dataforge/meta/TypedMetaItem;)Ljava/lang/Double;
public static final fun getFloat (Lhep/dataforge/meta/TypedMetaItem;)Ljava/lang/Float;
public static final fun getInt (Lhep/dataforge/meta/TypedMetaItem;)Ljava/lang/Integer;
public static final fun getLong (Lhep/dataforge/meta/TypedMetaItem;)Ljava/lang/Long;
public static final fun getNode (Lhep/dataforge/meta/TypedMetaItem;)Lhep/dataforge/meta/Meta;
public static final fun getNumber (Lhep/dataforge/meta/TypedMetaItem;)Ljava/lang/Number;
public static final fun getShort (Lhep/dataforge/meta/TypedMetaItem;)Ljava/lang/Short;
public static final fun getString (Lhep/dataforge/meta/TypedMetaItem;)Ljava/lang/String;
public static final fun getStringList (Lhep/dataforge/meta/TypedMetaItem;)Ljava/util/List;
public static final fun getValue (Lhep/dataforge/meta/TypedMetaItem;)Lhep/dataforge/values/Value;
}
public final class hep/dataforge/meta/ValueItem : hep/dataforge/meta/TypedMetaItem {
public static final field Companion Lhep/dataforge/meta/ValueItem$Companion;
public fun <init> (Lhep/dataforge/values/Value;)V
public fun equals (Ljava/lang/Object;)Z
public final fun getValue ()Lhep/dataforge/values/Value;
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}
public final class hep/dataforge/meta/ValueItem$Companion {
public final fun serializer ()Lkotlinx/serialization/KSerializer;
}
public abstract interface class hep/dataforge/meta/descriptors/Described {
public static final field Companion Lhep/dataforge/meta/descriptors/Described$Companion;
public abstract fun getDescriptor ()Lhep/dataforge/meta/descriptors/ItemDescriptor;
@ -547,7 +546,7 @@ public final class hep/dataforge/meta/descriptors/Described$Companion {
}
public final class hep/dataforge/meta/descriptors/DescriptorMetaKt {
public static final fun defaultItem (Lhep/dataforge/meta/descriptors/ItemDescriptor;)Lhep/dataforge/meta/MetaItem;
public static final fun defaultItem (Lhep/dataforge/meta/descriptors/ItemDescriptor;)Lhep/dataforge/meta/TypedMetaItem;
public static final fun defaultMeta (Lhep/dataforge/meta/descriptors/NodeDescriptor;)Lhep/dataforge/meta/Laminate;
}
@ -577,7 +576,7 @@ public final class hep/dataforge/meta/descriptors/ItemDescriptorKt {
public static final fun get (Lhep/dataforge/meta/descriptors/ItemDescriptor;Lhep/dataforge/names/Name;)Lhep/dataforge/meta/descriptors/ItemDescriptor;
public static final fun get (Lhep/dataforge/meta/descriptors/ItemDescriptor;Ljava/lang/String;)Lhep/dataforge/meta/descriptors/ItemDescriptor;
public static final fun plus (Lhep/dataforge/meta/descriptors/NodeDescriptor;Lhep/dataforge/meta/descriptors/NodeDescriptor;)Lhep/dataforge/meta/descriptors/NodeDescriptor;
public static final fun validateItem (Lhep/dataforge/meta/descriptors/ItemDescriptor;Lhep/dataforge/meta/MetaItem;)Z
public static final fun validateItem (Lhep/dataforge/meta/descriptors/ItemDescriptor;Lhep/dataforge/meta/TypedMetaItem;)Z
}
public final class hep/dataforge/meta/descriptors/NodeDescriptor : hep/dataforge/meta/descriptors/ItemDescriptor {
@ -634,16 +633,16 @@ public final class hep/dataforge/meta/transformations/KeepTransformationRule : h
public fun equals (Ljava/lang/Object;)Z
public final fun getSelector ()Lkotlin/jvm/functions/Function1;
public fun hashCode ()I
public fun matches (Lhep/dataforge/names/Name;Lhep/dataforge/meta/MetaItem;)Z
public fun matches (Lhep/dataforge/names/Name;Lhep/dataforge/meta/TypedMetaItem;)Z
public fun selectItems (Lhep/dataforge/meta/Meta;)Lkotlin/sequences/Sequence;
public fun toString ()Ljava/lang/String;
public fun transformItem (Lhep/dataforge/names/Name;Lhep/dataforge/meta/MetaItem;Lhep/dataforge/meta/MutableMeta;)V
public fun transformItem (Lhep/dataforge/names/Name;Lhep/dataforge/meta/TypedMetaItem;Lhep/dataforge/meta/MutableMeta;)V
}
public abstract interface class hep/dataforge/meta/transformations/MetaConverter {
public static final field Companion Lhep/dataforge/meta/transformations/MetaConverter$Companion;
public abstract fun itemToObject (Lhep/dataforge/meta/MetaItem;)Ljava/lang/Object;
public abstract fun objectToMetaItem (Ljava/lang/Object;)Lhep/dataforge/meta/MetaItem;
public abstract fun itemToObject (Lhep/dataforge/meta/TypedMetaItem;)Ljava/lang/Object;
public abstract fun objectToMetaItem (Ljava/lang/Object;)Lhep/dataforge/meta/TypedMetaItem;
}
public final class hep/dataforge/meta/transformations/MetaConverter$Companion {
@ -663,8 +662,8 @@ public final class hep/dataforge/meta/transformations/MetaConverter$Companion {
public final class hep/dataforge/meta/transformations/MetaConverterKt {
public static final fun metaToObject (Lhep/dataforge/meta/transformations/MetaConverter;Lhep/dataforge/meta/Meta;)Ljava/lang/Object;
public static final fun nullableItemToObject (Lhep/dataforge/meta/transformations/MetaConverter;Lhep/dataforge/meta/MetaItem;)Ljava/lang/Object;
public static final fun nullableObjectToMetaItem (Lhep/dataforge/meta/transformations/MetaConverter;Ljava/lang/Object;)Lhep/dataforge/meta/MetaItem;
public static final fun nullableItemToObject (Lhep/dataforge/meta/transformations/MetaConverter;Lhep/dataforge/meta/TypedMetaItem;)Ljava/lang/Object;
public static final fun nullableObjectToMetaItem (Lhep/dataforge/meta/transformations/MetaConverter;Ljava/lang/Object;)Lhep/dataforge/meta/TypedMetaItem;
public static final fun valueToObject (Lhep/dataforge/meta/transformations/MetaConverter;Lhep/dataforge/values/Value;)Ljava/lang/Object;
}
@ -711,10 +710,10 @@ public final class hep/dataforge/meta/transformations/RegexItemTransformationRul
public final fun getFrom ()Lkotlin/text/Regex;
public final fun getTransform ()Lkotlin/jvm/functions/Function4;
public fun hashCode ()I
public fun matches (Lhep/dataforge/names/Name;Lhep/dataforge/meta/MetaItem;)Z
public fun matches (Lhep/dataforge/names/Name;Lhep/dataforge/meta/TypedMetaItem;)Z
public fun selectItems (Lhep/dataforge/meta/Meta;)Lkotlin/sequences/Sequence;
public fun toString ()Ljava/lang/String;
public fun transformItem (Lhep/dataforge/names/Name;Lhep/dataforge/meta/MetaItem;Lhep/dataforge/meta/MutableMeta;)V
public fun transformItem (Lhep/dataforge/names/Name;Lhep/dataforge/meta/TypedMetaItem;Lhep/dataforge/meta/MutableMeta;)V
}
public final class hep/dataforge/meta/transformations/SingleItemTransformationRule : hep/dataforge/meta/transformations/TransformationRule {
@ -727,16 +726,16 @@ public final class hep/dataforge/meta/transformations/SingleItemTransformationRu
public final fun getFrom ()Lhep/dataforge/names/Name;
public final fun getTransform ()Lkotlin/jvm/functions/Function3;
public fun hashCode ()I
public fun matches (Lhep/dataforge/names/Name;Lhep/dataforge/meta/MetaItem;)Z
public fun matches (Lhep/dataforge/names/Name;Lhep/dataforge/meta/TypedMetaItem;)Z
public fun selectItems (Lhep/dataforge/meta/Meta;)Lkotlin/sequences/Sequence;
public fun toString ()Ljava/lang/String;
public fun transformItem (Lhep/dataforge/names/Name;Lhep/dataforge/meta/MetaItem;Lhep/dataforge/meta/MutableMeta;)V
public fun transformItem (Lhep/dataforge/names/Name;Lhep/dataforge/meta/TypedMetaItem;Lhep/dataforge/meta/MutableMeta;)V
}
public abstract interface class hep/dataforge/meta/transformations/TransformationRule {
public abstract fun matches (Lhep/dataforge/names/Name;Lhep/dataforge/meta/MetaItem;)Z
public abstract fun matches (Lhep/dataforge/names/Name;Lhep/dataforge/meta/TypedMetaItem;)Z
public abstract fun selectItems (Lhep/dataforge/meta/Meta;)Lkotlin/sequences/Sequence;
public abstract fun transformItem (Lhep/dataforge/names/Name;Lhep/dataforge/meta/MetaItem;Lhep/dataforge/meta/MutableMeta;)V
public abstract fun transformItem (Lhep/dataforge/names/Name;Lhep/dataforge/meta/TypedMetaItem;Lhep/dataforge/meta/MutableMeta;)V
}
public final class hep/dataforge/meta/transformations/TransformationRule$DefaultImpls {

View File

@ -16,11 +16,11 @@ import kotlin.jvm.Synchronized
public data class ItemListener(
val owner: Any? = null,
val action: (name: Name, oldItem: MetaItem<*>?, newItem: MetaItem<*>?) -> Unit
val action: (name: Name, oldItem: MetaItem?, newItem: MetaItem?) -> Unit
)
public interface ObservableItemProvider : ItemProvider {
public fun onChange(owner: Any?, action: (name: Name, oldItem: MetaItem<*>?, newItem: MetaItem<*>?) -> Unit)
public fun onChange(owner: Any?, action: (name: Name, oldItem: MetaItem?, newItem: MetaItem?) -> Unit)
public fun removeListener(owner: Any?)
}
@ -33,7 +33,7 @@ public class Config() : AbstractMutableMeta<Config>(), ObservableItemProvider {
private val listeners = HashSet<ItemListener>()
@Synchronized
private fun itemChanged(name: Name, oldItem: MetaItem<*>?, newItem: MetaItem<*>?) {
private fun itemChanged(name: Name, oldItem: MetaItem?, newItem: MetaItem?) {
listeners.forEach { it.action(name, oldItem, newItem) }
}
@ -41,7 +41,7 @@ public class Config() : AbstractMutableMeta<Config>(), ObservableItemProvider {
* Add change listener to this meta. Owner is declared to be able to remove listeners later. Listener without owner could not be removed
*/
@Synchronized
override fun onChange(owner: Any?, action: (Name, MetaItem<*>?, MetaItem<*>?) -> Unit) {
override fun onChange(owner: Any?, action: (Name, MetaItem?, MetaItem?) -> Unit) {
listeners.add(ItemListener(owner, action))
}
@ -53,15 +53,15 @@ public class Config() : AbstractMutableMeta<Config>(), ObservableItemProvider {
listeners.removeAll { it.owner === owner }
}
override fun replaceItem(key: NameToken, oldItem: MetaItem<Config>?, newItem: MetaItem<Config>?) {
override fun replaceItem(key: NameToken, oldItem: TypedMetaItem<Config>?, newItem: TypedMetaItem<Config>?) {
if (newItem == null) {
children.remove(key)
if (oldItem != null && oldItem is MetaItem.NodeItem<Config>) {
if (oldItem != null && oldItem is NodeItem<Config>) {
oldItem.node.removeListener(this)
}
} else {
children[key] = newItem
if (newItem is MetaItem.NodeItem) {
if (newItem is NodeItem) {
newItem.node.onChange(this) { name, oldChild, newChild ->
itemChanged(key + name, oldChild, newChild)
}
@ -92,7 +92,7 @@ public class Config() : AbstractMutableMeta<Config>(), ObservableItemProvider {
}
}
public operator fun Config.get(token: NameToken): MetaItem<Config>? = items[token]
public operator fun Config.get(token: NameToken): TypedMetaItem<Config>? = items[token]
/**
* Create a mutable copy of this [Meta]. The copy is created event if initial [Meta] is a [Config].
@ -102,8 +102,8 @@ public fun Meta.toConfig(): Config = Config().also { builder ->
this.items.mapValues { entry ->
val item = entry.value
builder[entry.key.asName()] = when (item) {
is MetaItem.ValueItem -> item.value
is MetaItem.NodeItem -> MetaItem.NodeItem(item.node.asConfig())
is ValueItem -> item.value
is NodeItem -> NodeItem(item.node.asConfig())
}
}
}

View File

@ -8,7 +8,7 @@ import kotlin.properties.ReadOnlyProperty
/* Meta delegates */
public typealias ItemDelegate = ReadOnlyProperty<Any?, MetaItem<*>?>
public typealias ItemDelegate = ReadOnlyProperty<Any?, MetaItem?>
public fun ItemProvider.item(key: Name? = null): ItemDelegate = ReadOnlyProperty { _, property ->
get(key ?: property.name.asName())
@ -40,7 +40,7 @@ public fun <R : Any> ItemDelegate.convert(
* A converter with a custom reader transformation
*/
public fun <R> ItemDelegate.convert(
reader: (MetaItem<*>?) -> R,
reader: (MetaItem?) -> R,
): ReadOnlyProperty<Any?, R> = ReadOnlyProperty<Any?, R> { thisRef, property ->
this@convert.getValue(thisRef, property).let(reader)
}

View File

@ -1,11 +1,10 @@
package hep.dataforge.meta
import hep.dataforge.meta.MetaItem.NodeItem
import hep.dataforge.names.*
public fun interface ItemProvider {
//getItem used instead of get in order to provide extension freedom
public fun getItem(name: Name): MetaItem<*>?
public fun getItem(name: Name): MetaItem?
public companion object {
public val EMPTY: ItemProvider = ItemProvider { null }
@ -20,7 +19,7 @@ public fun interface ItemProvider {
*
* If [name] is empty return current [Meta] as a [NodeItem]
*/
public operator fun ItemProvider?.get(name: Name): MetaItem<*>? = this?.getItem(name)
public operator fun ItemProvider?.get(name: Name): MetaItem? = this?.getItem(name)
/**
* The root node of this item provider if it is present
@ -30,7 +29,7 @@ public val ItemProvider.rootNode: Meta? get() = get(Name.EMPTY).node
/**
* Parse [Name] from [key] using full name notation and pass it to [Meta.get]
*/
public operator fun ItemProvider?.get(key: String): MetaItem<*>? = this?.get(key.toName())
public operator fun ItemProvider?.get(key: String): MetaItem? = this?.get(key.toName())
/**
* Create a provider that uses given provider for default values if those are not found in this provider
@ -43,7 +42,7 @@ public fun ItemProvider.withDefault(default: ItemProvider): ItemProvider = ItemP
* Get all items matching given name. The index of the last element, if present is used as a [Regex],
* against which indexes of elements are matched.
*/
public fun ItemProvider.getIndexed(name: Name): Map<String?, MetaItem<*>> {
public fun ItemProvider.getIndexed(name: Name): Map<String?, MetaItem> {
val root: Meta = when (name.length) {
0 -> error("Can't use empty name for 'getIndexed'")
1 -> this.rootNode ?: return emptyMap()
@ -60,7 +59,7 @@ public fun ItemProvider.getIndexed(name: Name): Map<String?, MetaItem<*>> {
}
}
public fun ItemProvider.getIndexed(name: String): Map<String?, MetaItem<*>> = this@getIndexed.getIndexed(name.toName())
public fun ItemProvider.getIndexed(name: String): Map<String?, MetaItem> = this@getIndexed.getIndexed(name.toName())
/**
* Return a provider referencing a child node

View File

@ -37,11 +37,11 @@ private fun Meta.toJsonWithIndex(descriptor: NodeDescriptor?, indexValue: String
val elementMap = HashMap<String, JsonElement>()
fun MetaItem<*>.toJsonElement(itemDescriptor: ItemDescriptor?, index: String?): JsonElement = when (this) {
is MetaItem.ValueItem -> {
fun MetaItem.toJsonElement(itemDescriptor: ItemDescriptor?, index: String?): JsonElement = when (this) {
is ValueItem -> {
value.toJson(itemDescriptor as? ValueDescriptor)
}
is MetaItem.NodeItem -> {
is NodeItem -> {
node.toJsonWithIndex(itemDescriptor as? NodeDescriptor, index)
}
}
@ -49,7 +49,7 @@ private fun Meta.toJsonWithIndex(descriptor: NodeDescriptor?, indexValue: String
fun addElement(key: String) {
val itemDescriptor = descriptor?.items?.get(key)
val jsonKey = key.toJsonKey(itemDescriptor)
val items: Map<String?, MetaItem<*>> = getIndexed(key)
val items: Map<String?, MetaItem> = getIndexed(key)
when (items.size) {
0 -> {
//do nothing
@ -96,14 +96,14 @@ public fun JsonPrimitive.toValue(descriptor: ValueDescriptor?): Value {
}
}
public fun JsonElement.toMetaItem(descriptor: ItemDescriptor? = null): MetaItem<JsonMeta> = when (this) {
public fun JsonElement.toMetaItem(descriptor: ItemDescriptor? = null): TypedMetaItem<JsonMeta> = when (this) {
is JsonPrimitive -> {
val value = this.toValue(descriptor as? ValueDescriptor)
MetaItem.ValueItem(value)
ValueItem(value)
}
is JsonObject -> {
val meta = JsonMeta(this, descriptor as? NodeDescriptor)
MetaItem.NodeItem(meta)
NodeItem(meta)
}
is JsonArray -> {
if (this.all { it is JsonPrimitive }) {
@ -115,7 +115,7 @@ public fun JsonElement.toMetaItem(descriptor: ItemDescriptor? = null): MetaItem<
(it as JsonPrimitive).toValue(descriptor as? ValueDescriptor)
}.asValue()
}
MetaItem.ValueItem(value)
ValueItem(value)
} else {
//We can't return multiple items therefore we create top level node
buildJsonObject { put(JSON_ARRAY_KEY, this@toMetaItem) }.toMetaItem(descriptor)
@ -128,18 +128,18 @@ public fun JsonElement.toMetaItem(descriptor: ItemDescriptor? = null): MetaItem<
*/
public class JsonMeta(private val json: JsonObject, private val descriptor: NodeDescriptor? = null) : MetaBase() {
private fun buildItems(): Map<NameToken, MetaItem<JsonMeta>> {
val map = LinkedHashMap<NameToken, MetaItem<JsonMeta>>()
private fun buildItems(): Map<NameToken, TypedMetaItem<JsonMeta>> {
val map = LinkedHashMap<NameToken, TypedMetaItem<JsonMeta>>()
json.forEach { (jsonKey, value) ->
val key = NameToken(jsonKey)
val itemDescriptor = descriptor?.items?.get(jsonKey)
when (value) {
is JsonPrimitive -> {
map[key] = MetaItem.ValueItem(value.toValue(itemDescriptor as? ValueDescriptor))
map[key] = ValueItem(value.toValue(itemDescriptor as? ValueDescriptor))
}
is JsonObject -> {
map[key] = MetaItem.NodeItem(
map[key] = NodeItem(
JsonMeta(
value,
itemDescriptor as? NodeDescriptor
@ -153,7 +153,7 @@ public class JsonMeta(private val json: JsonObject, private val descriptor: Node
(it as JsonPrimitive).toValue(itemDescriptor as? ValueDescriptor)
}
)
map[key] = MetaItem.ValueItem(listValue)
map[key] = ValueItem(listValue)
} else value.forEachIndexed { index, jsonElement ->
val indexKey = (itemDescriptor as? NodeDescriptor)?.indexKey ?: DEFAULT_INDEX_KEY
val indexValue: String = (jsonElement as? JsonObject)
@ -168,7 +168,7 @@ public class JsonMeta(private val json: JsonObject, private val descriptor: Node
return map
}
override val items: Map<NameToken, MetaItem<JsonMeta>> by lazy(::buildItems)
override val items: Map<NameToken, TypedMetaItem<JsonMeta>> by lazy(::buildItems)
public companion object {
/**

View File

@ -17,7 +17,7 @@ public class Laminate(layers: List<Meta>) : MetaBase() {
}
}
override val items: Map<NameToken, MetaItem<Meta>> by lazy {
override val items: Map<NameToken, TypedMetaItem<Meta>> by lazy {
layers.map { it.items.keys }.flatten().associateWith { key ->
layers.asSequence().map { it.items[key] }.filterNotNull().let(replaceRule)
}
@ -40,15 +40,15 @@ public class Laminate(layers: List<Meta>) : MetaBase() {
*
* TODO add picture
*/
public val replaceRule: (Sequence<MetaItem<*>>) -> MetaItem<SealedMeta> = { it.first().seal() }
public val replaceRule: (Sequence<MetaItem>) -> TypedMetaItem<SealedMeta> = { it.first().seal() }
private fun Sequence<MetaItem<*>>.merge(): MetaItem<SealedMeta> {
private fun Sequence<MetaItem>.merge(): TypedMetaItem<SealedMeta> {
return when {
all { it is MetaItem.ValueItem } -> //If all items are values, take first
all { it is ValueItem } -> //If all items are values, take first
first().seal()
all { it is MetaItem.NodeItem } -> {
all { it is NodeItem } -> {
//list nodes in item
val nodes = map { (it as MetaItem.NodeItem).node }
val nodes = map { (it as NodeItem).node }
//represent as key->value entries
val entries = nodes.flatMap { it.items.entries.asSequence() }
//group by keys
@ -57,13 +57,13 @@ public class Laminate(layers: List<Meta>) : MetaBase() {
val items = groups.mapValues { entry ->
entry.value.asSequence().map { it.value }.merge()
}
MetaItem.NodeItem(SealedMeta(items))
NodeItem(SealedMeta(items))
}
else -> map {
when (it) {
is MetaItem.ValueItem -> MetaItem.NodeItem(Meta { Meta.VALUE_KEY put it.value })
is MetaItem.NodeItem -> it
is ValueItem -> NodeItem(Meta { Meta.VALUE_KEY put it.value })
is NodeItem -> it
}
}.merge()
}
@ -74,7 +74,7 @@ public class Laminate(layers: List<Meta>) : MetaBase() {
* The values a replaced but meta children are joined
* TODO add picture
*/
public val mergeRule: (Sequence<MetaItem<*>>) -> MetaItem<SealedMeta> = { it.merge() }
public val mergeRule: (Sequence<MetaItem>) -> TypedMetaItem<SealedMeta> = { it.merge() }
}
}
@ -84,7 +84,7 @@ public fun Laminate(vararg layers: Meta?): Laminate = Laminate(layers.filterNotN
/**
* Performance optimized version of get method
*/
public fun Laminate.getFirst(name: Name): MetaItem<*>? {
public fun Laminate.getFirst(name: Name): MetaItem? {
layers.forEach { layer ->
layer[name]?.let { return it }
}

View File

@ -1,7 +1,5 @@
package hep.dataforge.meta
import hep.dataforge.meta.MetaItem.NodeItem
import hep.dataforge.meta.MetaItem.ValueItem
import hep.dataforge.names.*
import hep.dataforge.values.Value
import kotlinx.serialization.Serializable
@ -18,9 +16,9 @@ public interface MetaRepr {
}
/**
* Generic meta tree representation. Elements are [MetaItem] objects that could be represented by three different entities:
* * [MetaItem.ValueItem] (leaf)
* * [MetaItem.NodeItem] single node
* Generic meta tree representation. Elements are [TypedMetaItem] objects that could be represented by three different entities:
* * [ValueItem] (leaf)
* * [NodeItem] single node
*
* * Same name siblings are supported via elements with the same [Name] but different queries
*/
@ -28,9 +26,9 @@ public interface Meta : MetaRepr, ItemProvider {
/**
* Top level items of meta tree
*/
public val items: Map<NameToken, MetaItem<*>>
public val items: Map<NameToken, MetaItem>
override fun getItem(name: Name): MetaItem<*>? {
override fun getItem(name: Name): MetaItem? {
if (name.isEmpty()) return NodeItem(this)
return name.firstOrNull()?.let { token ->
val tail = name.cutFirst()
@ -58,12 +56,12 @@ public interface Meta : MetaRepr, ItemProvider {
public const val VALUE_KEY: String = "@value"
public val EMPTY: Meta = object : MetaBase() {
override val items: Map<NameToken, MetaItem<*>> = emptyMap()
override val items: Map<NameToken, MetaItem> = emptyMap()
}
}
}
public operator fun Meta.get(token: NameToken): MetaItem<*>? = items.get(token)
public operator fun Meta.get(token: NameToken): MetaItem? = items.get(token)
/**
* Get a sequence of [Name]-[Value] pairs
@ -78,9 +76,9 @@ public fun Meta.valueSequence(): Sequence<Pair<Name, Value>> {
}
/**
* Get a sequence of all [Name]-[MetaItem] pairs for all items including nodes
* Get a sequence of all [Name]-[TypedMetaItem] pairs for all items including nodes
*/
public fun Meta.itemSequence(): Sequence<Pair<Name, MetaItem<*>>> = sequence {
public fun Meta.itemSequence(): Sequence<Pair<Name, MetaItem>> = sequence {
items.forEach { (key, item) ->
yield(key.asName() to item)
if (item is NodeItem) {
@ -91,27 +89,27 @@ public fun Meta.itemSequence(): Sequence<Pair<Name, MetaItem<*>>> = sequence {
}
}
public operator fun Meta.iterator(): Iterator<Pair<Name, MetaItem<*>>> = itemSequence().iterator()
public operator fun Meta.iterator(): Iterator<Pair<Name, MetaItem>> = itemSequence().iterator()
/**
* A meta node that ensures that all of its descendants has at least the same type
*/
public interface TypedMeta<out M : TypedMeta<M>> : Meta {
override val items: Map<NameToken, MetaItem<M>>
override val items: Map<NameToken, TypedMetaItem<M>>
@Suppress("UNCHECKED_CAST")
override fun getItem(name: Name): MetaItem<M>? = super.getItem(name)?.let { it as MetaItem<M> }
override fun getItem(name: Name): TypedMetaItem<M>? = super.getItem(name)?.let { it as TypedMetaItem<M> }
//Typed meta guarantees that all children have M type
}
/**
* The same as [Meta.get], but with specific node type
*/
public operator fun <M : TypedMeta<M>> M?.get(name: Name): MetaItem<M>? = this?.getItem(name)
public operator fun <M : TypedMeta<M>> M?.get(name: Name): TypedMetaItem<M>? = this?.getItem(name)
public operator fun <M : TypedMeta<M>> M?.get(key: String): MetaItem<M>? = this[key.toName()]
public operator fun <M : TypedMeta<M>> M?.get(key: NameToken): MetaItem<M>? = this[key.asName()]
public operator fun <M : TypedMeta<M>> M?.get(key: String): TypedMetaItem<M>? = this[key.toName()]
public operator fun <M : TypedMeta<M>> M?.get(key: NameToken): TypedMetaItem<M>? = this[key.asName()]
/**
* Equals, hashcode and to string for any meta

View File

@ -15,7 +15,7 @@ public class MetaBuilder : AbstractMutableMeta<MetaBuilder>() {
override fun wrapNode(meta: Meta): MetaBuilder = if (meta is MetaBuilder) meta else meta.builder()
override fun empty(): MetaBuilder = MetaBuilder()
public infix fun String.put(item: MetaItem<*>?) {
public infix fun String.put(item: MetaItem?) {
set(this, item)
}
@ -126,8 +126,8 @@ public fun Meta.builder(): MetaBuilder {
items.mapValues { entry ->
val item = entry.value
builder[entry.key.asName()] = when (item) {
is MetaItem.ValueItem -> item.value
is MetaItem.NodeItem -> MetaItem.NodeItem(item.node.builder())
is ValueItem -> item.value
is NodeItem -> NodeItem(item.node.builder())
}
}
}

View File

@ -1,88 +0,0 @@
package hep.dataforge.meta
import hep.dataforge.meta.MetaItem.NodeItem
import hep.dataforge.meta.MetaItem.ValueItem
import hep.dataforge.values.*
import kotlinx.serialization.Serializable
/**
* A member of the meta tree. Could be represented as one of following:
* * a [ValueItem] (leaf)
* * a [NodeItem] (node)
*/
@Serializable(MetaItemSerializer::class)
public sealed class MetaItem<out M : Meta>() {
abstract override fun equals(other: Any?): Boolean
abstract override fun hashCode(): Int
@Serializable(MetaItemSerializer::class)
public class ValueItem(public val value: Value) : MetaItem<Nothing>() {
override fun toString(): String = value.toString()
override fun equals(other: Any?): Boolean {
return this.value == (other as? ValueItem)?.value
}
override fun hashCode(): Int {
return value.hashCode()
}
}
@Serializable(MetaItemSerializer::class)
public class NodeItem<M : Meta>(public 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()
override fun equals(other: Any?): Boolean = node == (other as? NodeItem<*>)?.node
override fun hashCode(): Int = node.hashCode()
}
public companion object {
public fun of(arg: Any?): MetaItem<*> {
return when (arg) {
null -> ValueItem(Null)
is MetaItem<*> -> arg
is Meta -> NodeItem(arg)
else -> ValueItem(Value.of(arg))
}
}
}
}
public fun Value.asMetaItem(): ValueItem = ValueItem(this)
public fun <M : Meta> M.asMetaItem(): NodeItem<M> = NodeItem(this)
/**
* Unsafe methods to access values and nodes directly from [MetaItem]
*/
public val MetaItem<*>?.value: Value?
get() = (this as? ValueItem)?.value
?: (this?.node?.get(Meta.VALUE_KEY) as? ValueItem)?.value
public val MetaItem<*>?.string: String? get() = value?.string
public val MetaItem<*>?.boolean: Boolean? get() = value?.boolean
public val MetaItem<*>?.number: Number? get() = value?.numberOrNull
public val MetaItem<*>?.double: Double? get() = number?.toDouble()
public val MetaItem<*>?.float: Float? get() = number?.toFloat()
public val MetaItem<*>?.int: Int? get() = number?.toInt()
public val MetaItem<*>?.long: Long? get() = number?.toLong()
public val MetaItem<*>?.short: Short? get() = number?.toShort()
public inline fun <reified E : Enum<E>> MetaItem<*>?.enum(): E? = if (this is ValueItem && this.value is EnumValue<*>) {
this.value.value as E
} else {
string?.let { enumValueOf<E>(it) }
}
public val MetaItem<*>.stringList: List<String>? get() = value?.list?.map { it.string }
public val <M : Meta> MetaItem<M>?.node: M?
get() = when (this) {
null -> null
is ValueItem -> null//error("Trying to interpret value meta item as node item")
is NodeItem -> node
}

View File

@ -12,7 +12,7 @@ import kotlinx.serialization.json.JsonDecoder
import kotlinx.serialization.json.JsonEncoder
import kotlinx.serialization.json.JsonObject
public object MetaItemSerializer : KSerializer<MetaItem<*>> {
public object MetaItemSerializer : KSerializer<MetaItem> {
@OptIn(InternalSerializationApi::class, ExperimentalSerializationApi::class)
override val descriptor: SerialDescriptor = buildSerialDescriptor("MetaItem", PolymorphicKind.SEALED) {
@ -20,7 +20,7 @@ public object MetaItemSerializer : KSerializer<MetaItem<*>> {
element("value", buildSerialDescriptor("MetaItem.value", SerialKind.CONTEXTUAL))
}
override fun deserialize(decoder: Decoder): MetaItem<*> {
override fun deserialize(decoder: Decoder): MetaItem {
decoder.decodeStructure(descriptor) {
//Force strict serialization order
require(decodeElementIndex(descriptor) == 0) { "Node flag must be first item serialized" }
@ -36,12 +36,12 @@ public object MetaItemSerializer : KSerializer<MetaItem<*>> {
}
}
override fun serialize(encoder: Encoder, value: MetaItem<*>) {
override fun serialize(encoder: Encoder, value: MetaItem) {
encoder.encodeStructure(descriptor) {
encodeBooleanElement(descriptor, 0, value is MetaItem.NodeItem)
encodeBooleanElement(descriptor, 0, value is NodeItem)
when (value) {
is MetaItem.ValueItem -> encodeSerializableElement(descriptor, 1, ValueSerializer, value.value)
is MetaItem.NodeItem -> encodeSerializableElement(descriptor, 1, MetaSerializer, value.node)
is ValueItem -> encodeSerializableElement(descriptor, 1, ValueSerializer, value.value)
is NodeItem -> encodeSerializableElement(descriptor, 1, MetaSerializer, value.node)
}
}
}
@ -52,7 +52,7 @@ public object MetaItemSerializer : KSerializer<MetaItem<*>> {
*/
public object MetaSerializer : KSerializer<Meta> {
private val mapSerializer: KSerializer<Map<NameToken, MetaItem<Meta>>> = MapSerializer(
private val mapSerializer: KSerializer<Map<NameToken, TypedMetaItem<Meta>>> = MapSerializer(
NameToken,
MetaItemSerializer//MetaItem.serializer(MetaSerializer)
)
@ -64,7 +64,7 @@ public object MetaSerializer : KSerializer<Meta> {
JsonObject.serializer().deserialize(decoder).toMeta()
} else {
object : MetaBase() {
override val items: Map<NameToken, MetaItem<*>> = mapSerializer.deserialize(decoder)
override val items: Map<NameToken, MetaItem> = mapSerializer.deserialize(decoder)
}
}
}

View File

@ -7,10 +7,10 @@ import hep.dataforge.names.NameToken
* Meta object with default provider for items not present in the initial meta.
*/
public class MetaWithDefault(public val meta: Meta, public val default: ItemProvider) : MetaBase() {
override val items: Map<NameToken, MetaItem<*>>
override val items: Map<NameToken, MetaItem>
get() = meta.items
override fun getItem(name: Name): MetaItem<*>? {
override fun getItem(name: Name): MetaItem? {
return meta[name] ?: default[name]
}
}

View File

@ -9,14 +9,14 @@ import kotlin.reflect.KProperty
/* Read-write delegates */
public typealias MutableItemDelegate = ReadWriteProperty<Any?, MetaItem<*>?>
public typealias MutableItemDelegate = ReadWriteProperty<Any?, MetaItem?>
public fun MutableItemProvider.item(key: Name? = null): MutableItemDelegate = object : MutableItemDelegate {
override fun getValue(thisRef: Any?, property: KProperty<*>): MetaItem<*>? {
override fun getValue(thisRef: Any?, property: KProperty<*>): MetaItem? {
return get(key ?: property.name.asName())
}
override fun setValue(thisRef: Any?, property: KProperty<*>, value: MetaItem<*>?) {
override fun setValue(thisRef: Any?, property: KProperty<*>, value: MetaItem?) {
val name = key ?: property.name.asName()
set(name, value)
}
@ -25,7 +25,7 @@ public fun MutableItemProvider.item(key: Name? = null): MutableItemDelegate = ob
/* Mutable converters */
/**
* A type converter for a mutable [MetaItem] delegate
* A type converter for a mutable [TypedMetaItem] delegate
*/
public fun <R : Any> MutableItemDelegate.convert(
converter: MetaConverter<R>,
@ -55,8 +55,8 @@ public fun <R : Any> MutableItemDelegate.convert(
}
public fun <R> MutableItemDelegate.convert(
reader: (MetaItem<*>?) -> R,
writer: (R) -> MetaItem<*>?,
reader: (MetaItem?) -> R,
writer: (R) -> MetaItem?,
): ReadWriteProperty<Any?, R> = object : ReadWriteProperty<Any?, R> {
override fun getValue(thisRef: Any?, property: KProperty<*>): R =
@ -119,7 +119,7 @@ public fun MutableItemProvider.node(key: Name? = null): ReadWriteProperty<Any?,
)
public inline fun <reified M : MutableMeta<M>> M.node(key: Name? = null): ReadWriteProperty<Any?, M?> =
item(key).convert(reader = { it?.let { it.node as M } }, writer = { it?.let { MetaItem.NodeItem(it) } })
item(key).convert(reader = { it?.let { it.node as M } }, writer = { it?.let { NodeItem(it) } })
/* Number delegates */

View File

@ -5,16 +5,16 @@ import hep.dataforge.values.Value
import hep.dataforge.values.asValue
public interface MutableItemProvider : ItemProvider {
public fun setItem(name: Name, item: MetaItem<*>?)
public fun setItem(name: Name, item: MetaItem?)
}
public operator fun MutableItemProvider.set(name: Name, item: MetaItem<*>?): Unit = setItem(name, item)
public operator fun MutableItemProvider.set(name: Name, item: MetaItem?): Unit = setItem(name, item)
public operator fun MutableItemProvider.set(name: Name, value: Value?): Unit = set(name, value?.asMetaItem())
public operator fun MutableItemProvider.set(name: Name, meta: Meta?): Unit = set(name, meta?.asMetaItem())
public operator fun MutableItemProvider.set(key: String, item: MetaItem<*>?): Unit = set(key.toName(), item)
public operator fun MutableItemProvider.set(key: String, item: MetaItem?): Unit = set(key.toName(), item)
public operator fun MutableItemProvider.set(key: String, meta: Meta?): Unit = set(key, meta?.asMetaItem())
@ -34,7 +34,7 @@ public operator fun MutableItemProvider.set(name: Name, value: Any?) {
is Number -> set(name, value.asValue())
is String -> set(name, value.asValue())
is Boolean -> set(name, value.asValue())
is MetaItem<*> -> set(name, value)
is MetaItem -> set(name, value)
is Meta -> set(name, value)
is MetaRepr -> set(name, value.toMeta())
is Configurable -> set(name, value.config)
@ -56,8 +56,8 @@ public operator fun MutableItemProvider.set(key: String, index: String, value: A
public fun MutableItemProvider.setIndexedItems(
name: Name,
items: Iterable<MetaItem<*>>,
indexFactory: (MetaItem<*>, index: Int) -> String = { _, index -> index.toString() },
items: Iterable<MetaItem>,
indexFactory: (MetaItem, index: Int) -> String = { _, index -> index.toString() },
) {
val tokens = name.tokens.toMutableList()
val last = tokens.last()
@ -73,7 +73,7 @@ public fun MutableItemProvider.setIndexed(
metas: Iterable<Meta>,
indexFactory: (Meta, index: Int) -> String = { _, index -> index.toString() },
) {
setIndexedItems(name, metas.map { MetaItem.NodeItem(it) }) { item, index -> indexFactory(item.node!!, index) }
setIndexedItems(name, metas.map { NodeItem(it) }) { item, index -> indexFactory(item.node!!, index) }
}
public operator fun MutableItemProvider.set(name: Name, metas: Iterable<Meta>): Unit = setIndexed(name, metas)
@ -85,11 +85,11 @@ public operator fun MutableItemProvider.set(name: String, metas: Iterable<Meta>)
*/
public fun MutableItemProvider.getChild(childName: Name): MutableItemProvider {
fun createProvider() = object : MutableItemProvider {
override fun setItem(name: Name, item: MetaItem<*>?) {
override fun setItem(name: Name, item: MetaItem?) {
this@getChild.setItem(childName + name, item)
}
override fun getItem(name: Name): MetaItem<*>? = this@getChild.getItem(childName + name)
override fun getItem(name: Name): MetaItem? = this@getChild.getItem(childName + name)
}
return when {
@ -130,9 +130,9 @@ public fun MutableItemProvider.withDefault(default: ItemProvider): MutableItemPr
//Optimize for use with empty default
this
} else object : MutableItemProvider {
override fun setItem(name: Name, item: MetaItem<*>?) {
override fun setItem(name: Name, item: MetaItem?) {
this@withDefault.setItem(name, item)
}
override fun getItem(name: Name): MetaItem<*>? = this@withDefault.getItem(name) ?: default.getItem(name)
override fun getItem(name: Name): MetaItem? = this@withDefault.getItem(name) ?: default.getItem(name)
}

View File

@ -3,7 +3,7 @@ package hep.dataforge.meta
import hep.dataforge.names.*
public interface MutableMeta<out M : MutableMeta<M>> : TypedMeta<M>, MutableItemProvider {
override val items: Map<NameToken, MetaItem<M>>
override val items: Map<NameToken, TypedMetaItem<M>>
}
/**
@ -12,14 +12,14 @@ public interface MutableMeta<out M : MutableMeta<M>> : TypedMeta<M>, MutableItem
* Changes in Meta are not thread safe.
*/
public abstract class AbstractMutableMeta<M : MutableMeta<M>> : AbstractTypedMeta<M>(), MutableMeta<M> {
protected val children: MutableMap<NameToken, MetaItem<M>> = LinkedHashMap()
protected val children: MutableMap<NameToken, TypedMetaItem<M>> = LinkedHashMap()
override val items: Map<NameToken, MetaItem<M>>
override val items: Map<NameToken, TypedMetaItem<M>>
get() = children
//protected abstract fun itemChanged(name: Name, oldItem: MetaItem<*>?, newItem: MetaItem<*>?)
protected open fun replaceItem(key: NameToken, oldItem: MetaItem<M>?, newItem: MetaItem<M>?) {
protected open fun replaceItem(key: NameToken, oldItem: TypedMetaItem<M>?, newItem: TypedMetaItem<M>?) {
if (newItem == null) {
children.remove(key)
} else {
@ -28,10 +28,10 @@ public abstract class AbstractMutableMeta<M : MutableMeta<M>> : AbstractTypedMet
//itemChanged(key.asName(), oldItem, newItem)
}
private fun wrapItem(item: MetaItem<*>?): MetaItem<M>? = when (item) {
private fun wrapItem(item: MetaItem?): TypedMetaItem<M>? = when (item) {
null -> null
is MetaItem.ValueItem -> item
is MetaItem.NodeItem -> MetaItem.NodeItem(wrapNode(item.node))
is ValueItem -> item
is NodeItem -> NodeItem(wrapNode(item.node))
}
/**
@ -44,19 +44,19 @@ public abstract class AbstractMutableMeta<M : MutableMeta<M>> : AbstractTypedMet
*/
internal abstract fun empty(): M
override fun setItem(name: Name, item: MetaItem<*>?) {
override fun setItem(name: Name, item: MetaItem?) {
when (name.length) {
0 -> error("Can't set a meta item for empty name")
1 -> {
val token = name.firstOrNull()!!
val oldItem: MetaItem<M>? = getItem(name)
val oldItem: TypedMetaItem<M>? = getItem(name)
replaceItem(token, oldItem, wrapItem(item))
}
else -> {
val token = name.firstOrNull()!!
//get existing or create new node. Query is ignored for new node
if (items[token] == null) {
replaceItem(token, null, MetaItem.NodeItem(empty()))
replaceItem(token, null, NodeItem(empty()))
}
items[token]?.node!!.set(name.cutFirst(), item)
}
@ -87,7 +87,7 @@ public fun MutableItemProvider.append(name: String, value: Any?): Unit = append(
public fun <M : AbstractMutableMeta<M>> M.edit(name: Name, builder: M.() -> Unit) {
val item = when (val existingItem = get(name)) {
null -> empty().also { set(name, it) }
is MetaItem.NodeItem<M> -> existingItem.node
is NodeItem<M> -> existingItem.node
else -> error("Can't edit value meta item")
}
item.apply(builder)

View File

@ -25,19 +25,19 @@ public open class Scheme(
internal set
private fun getDefaultItem(name: Name): MetaItem<*>? {
private fun getDefaultItem(name: Name): MetaItem? {
return default?.get(name) ?: descriptor?.get(name)?.defaultItem()
}
/**
* Get a property with default
*/
override fun getItem(name: Name): MetaItem<*>? = items[name] ?: getDefaultItem(name)
override fun getItem(name: Name): MetaItem? = items[name] ?: getDefaultItem(name)
/**
* Check if property with given [name] could be assigned to [item]
*/
public open fun validateItem(name: Name, item: MetaItem<*>?): Boolean {
public open fun validateItem(name: Name, item: MetaItem?): Boolean {
val descriptor = descriptor?.get(name)
return descriptor?.validateItem(item) ?: true
}
@ -45,7 +45,7 @@ public open class Scheme(
/**
* Set a configurable property
*/
override fun setItem(name: Name, item: MetaItem<*>?) {
override fun setItem(name: Name, item: MetaItem?) {
if (validateItem(name, item)) {
items[name] = item
} else {
@ -60,7 +60,7 @@ public open class Scheme(
*/
public open val defaultLayer: Meta
get() = object : MetaBase() {
override val items: Map<NameToken, MetaItem<*>> = buildMap {
override val items: Map<NameToken, MetaItem> = buildMap {
descriptor?.items?.forEach { (key, itemDescriptor) ->
val token = NameToken(key)
val name = token.asName()

View File

@ -8,7 +8,7 @@ import hep.dataforge.names.NameToken
* If the argument is possibly mutable node, it is copied on creation
*/
public class SealedMeta internal constructor(
override val items: Map<NameToken, MetaItem<SealedMeta>>,
override val items: Map<NameToken, TypedMetaItem<SealedMeta>>,
) : AbstractTypedMeta<SealedMeta>()
/**
@ -17,7 +17,7 @@ public class SealedMeta internal constructor(
public fun Meta.seal(): SealedMeta = this as? SealedMeta ?: SealedMeta(items.mapValues { entry -> entry.value.seal() })
@Suppress("UNCHECKED_CAST")
public fun MetaItem<*>.seal(): MetaItem<SealedMeta> = when (this) {
is MetaItem.ValueItem -> this
is MetaItem.NodeItem -> MetaItem.NodeItem(node.seal())
public fun MetaItem.seal(): TypedMetaItem<SealedMeta> = when (this) {
is ValueItem -> this
is NodeItem -> NodeItem(node.seal())
}

View File

@ -46,7 +46,7 @@ public fun <C : MutableItemProvider, S : Specification<C>> Configurable.update(
action: C.() -> Unit,
): Configurable = apply { config.update(spec, action) }
public fun <T : MutableItemProvider> MetaItem<Config>.withSpec(spec: Specification<T>): T? =
public fun <T : MutableItemProvider> TypedMetaItem<Config>.withSpec(spec: Specification<T>): T? =
node?.let { spec.write(it) }
public fun <T : Scheme> MutableItemProvider.spec(

View File

@ -0,0 +1,88 @@
package hep.dataforge.meta
import hep.dataforge.values.*
import kotlinx.serialization.Serializable
/**
* A member of the meta tree. Could be represented as one of following:
* * a [ValueItem] (leaf)
* * a [NodeItem] (node)
*/
@Serializable(MetaItemSerializer::class)
public sealed class TypedMetaItem<out M : Meta>() {
abstract override fun equals(other: Any?): Boolean
abstract override fun hashCode(): Int
public companion object {
public fun of(arg: Any?): MetaItem {
return when (arg) {
null -> ValueItem(Null)
is MetaItem -> arg
is Meta -> NodeItem(arg)
else -> ValueItem(Value.of(arg))
}
}
}
}
public typealias MetaItem = TypedMetaItem<*>
@Serializable(MetaItemSerializer::class)
public class ValueItem(public val value: Value) : TypedMetaItem<Nothing>() {
override fun toString(): String = value.toString()
override fun equals(other: Any?): Boolean {
return this.value == (other as? ValueItem)?.value
}
override fun hashCode(): Int {
return value.hashCode()
}
}
@Serializable(MetaItemSerializer::class)
public class NodeItem<M : Meta>(public val node: M) : TypedMetaItem<M>() {
//Fixing serializer for node could cause class cast problems, but it should not since Meta descendants are not serializable
override fun toString(): String = node.toString()
override fun equals(other: Any?): Boolean = node == (other as? NodeItem<*>)?.node
override fun hashCode(): Int = node.hashCode()
}
public fun Value.asMetaItem(): ValueItem = ValueItem(this)
public fun <M : Meta> M.asMetaItem(): NodeItem<M> = NodeItem(this)
/**
* Unsafe methods to access values and nodes directly from [TypedMetaItem]
*/
public val MetaItem?.value: Value?
get() = (this as? ValueItem)?.value
?: (this?.node?.get(Meta.VALUE_KEY) as? ValueItem)?.value
public val MetaItem?.string: String? get() = value?.string
public val MetaItem?.boolean: Boolean? get() = value?.boolean
public val MetaItem?.number: Number? get() = value?.numberOrNull
public val MetaItem?.double: Double? get() = number?.toDouble()
public val MetaItem?.float: Float? get() = number?.toFloat()
public val MetaItem?.int: Int? get() = number?.toInt()
public val MetaItem?.long: Long? get() = number?.toLong()
public val MetaItem?.short: Short? get() = number?.toShort()
public inline fun <reified E : Enum<E>> MetaItem?.enum(): E? = if (this is ValueItem && this.value is EnumValue<*>) {
this.value.value as E
} else {
string?.let { enumValueOf<E>(it) }
}
public val MetaItem.stringList: List<String>? get() = value?.list?.map { it.string }
public val <M : Meta> TypedMetaItem<M>?.node: M?
get() = when (this) {
null -> null
is ValueItem -> null//error("Trying to interpret value meta item as node item")
is NodeItem -> node
}

View File

@ -1,16 +1,13 @@
package hep.dataforge.meta.descriptors
import hep.dataforge.meta.Laminate
import hep.dataforge.meta.Meta
import hep.dataforge.meta.MetaBase
import hep.dataforge.meta.MetaItem
import hep.dataforge.meta.*
import hep.dataforge.names.NameToken
/**
* A [Meta] that is constructed from [NodeDescriptor]
*/
private class DescriptorMeta(val descriptor: NodeDescriptor) : Meta, MetaBase() {
override val items: Map<NameToken, MetaItem<*>>
override val items: Map<NameToken, MetaItem>
get() = buildMap {
descriptor.items.forEach { (token, descriptorItem) ->
val item = descriptorItem.defaultItem()
@ -27,22 +24,22 @@ private class DescriptorMeta(val descriptor: NodeDescriptor) : Meta, MetaBase()
public fun NodeDescriptor.defaultMeta(): Laminate = Laminate(default, DescriptorMeta(this))
/**
* Build a default [MetaItem.NodeItem] from this node descriptor
* Build a default [NodeItem] from this node descriptor
*/
internal fun NodeDescriptor.defaultItem(): MetaItem.NodeItem<*> =
MetaItem.NodeItem(defaultMeta())
internal fun NodeDescriptor.defaultItem(): NodeItem<*> =
NodeItem(defaultMeta())
/**
* Build a default [MetaItem.ValueItem] from this descriptor
* Build a default [ValueItem] from this descriptor
*/
internal fun ValueDescriptor.defaultItem(): MetaItem.ValueItem? {
return MetaItem.ValueItem(default ?: return null)
internal fun ValueDescriptor.defaultItem(): ValueItem? {
return ValueItem(default ?: return null)
}
/**
* Build a default [MetaItem] from descriptor.
* Build a default [TypedMetaItem] from descriptor.
*/
public fun ItemDescriptor.defaultItem(): MetaItem<*>? {
public fun ItemDescriptor.defaultItem(): MetaItem? {
return when (this) {
is ValueDescriptor -> defaultItem()
is NodeDescriptor -> defaultItem()

View File

@ -5,7 +5,7 @@ import hep.dataforge.names.*
import hep.dataforge.values.*
/**
* A common parent for [ValueDescriptor] and [NodeDescriptor]. Describes a single [MetaItem] or a group of same-name-siblings.
* A common parent for [ValueDescriptor] and [NodeDescriptor]. Describes a single [TypedMetaItem] or a group of same-name-siblings.
*/
@DFBuilder
public sealed class ItemDescriptor(final override val config: Config) : Configurable {
@ -55,7 +55,7 @@ public inline fun ItemDescriptor.attributes(block: Config.() -> Unit) {
/**
* Check if given item suits the descriptor
*/
public fun ItemDescriptor.validateItem(item: MetaItem<*>?): Boolean {
public fun ItemDescriptor.validateItem(item: MetaItem?): Boolean {
if (item == null) return !required
return when (this) {
is ValueDescriptor -> isAllowedValue(item.value ?: return false)
@ -279,7 +279,7 @@ public class ValueDescriptor(config: Config = Config()) : ItemDescriptor(config)
}
},
writer = {
MetaItem.ValueItem(it.asValue())
ValueItem(it.asValue())
}
)

View File

@ -11,30 +11,30 @@ import hep.dataforge.values.Value
public fun Meta.toMap(descriptor: NodeDescriptor? = null): Map<String, Any?> {
return items.entries.associate { (token, item) ->
token.toString() to when (item) {
is MetaItem.NodeItem -> item.node.toMap()
is MetaItem.ValueItem -> item.value.value
is NodeItem -> item.node.toMap()
is ValueItem -> item.value.value
}
}
}
/**
* Convert map of maps to meta. This method will recognize [MetaItem], [Map]<String,Any?> and [List] of all mentioned above as value.
* Convert map of maps to meta. This method will recognize [TypedMetaItem], [Map]<String,Any?> and [List] of all mentioned above as value.
* All other values will be converted to values.
*/
@DFExperimental
public fun Map<String, Any?>.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<String, Any?>).toMeta())
else -> MetaItem.ValueItem(Value.of(value))
fun toItem(value: Any?): MetaItem = when (value) {
is MetaItem -> value
is Meta -> NodeItem(value)
is Map<*, *> -> NodeItem((value as Map<String, Any?>).toMeta())
else -> ValueItem(Value.of(value))
}
entries.forEach { (key, value) ->
if (value is List<*>) {
val items = value.map { toItem(it) }
if (items.all { it is MetaItem.ValueItem }) {
if (items.all { it is ValueItem }) {
set(key, ListValue(items.map { it.value!! }))
} else {
setIndexedItems(key.toName(), value.map { toItem(it) })

View File

@ -4,121 +4,121 @@ import hep.dataforge.meta.*
import hep.dataforge.values.*
/**
* A converter of generic object to and from [MetaItem]
* A converter of generic object to and from [TypedMetaItem]
*/
public interface MetaConverter<T : Any> {
public fun itemToObject(item: MetaItem<*>): T
public fun objectToMetaItem(obj: T): MetaItem<*>
public fun itemToObject(item: MetaItem): T
public fun objectToMetaItem(obj: T): MetaItem
public companion object {
public val item: MetaConverter<MetaItem<*>> = object : MetaConverter<MetaItem<*>> {
override fun itemToObject(item: MetaItem<*>): MetaItem<*> = item
override fun objectToMetaItem(obj: MetaItem<*>): MetaItem<*> = obj
public val item: MetaConverter<MetaItem> = object : MetaConverter<MetaItem> {
override fun itemToObject(item: MetaItem): MetaItem = item
override fun objectToMetaItem(obj: MetaItem): MetaItem = obj
}
public val meta: MetaConverter<Meta> = object : MetaConverter<Meta> {
override fun itemToObject(item: MetaItem<*>): Meta = when (item) {
is MetaItem.NodeItem -> item.node
is MetaItem.ValueItem -> item.value.toMeta()
override fun itemToObject(item: MetaItem): Meta = when (item) {
is NodeItem -> item.node
is ValueItem -> item.value.toMeta()
}
override fun objectToMetaItem(obj: Meta): MetaItem<*> = MetaItem.NodeItem(obj)
override fun objectToMetaItem(obj: Meta): MetaItem = NodeItem(obj)
}
public val value: MetaConverter<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
override fun itemToObject(item: MetaItem): Value = when (item) {
is NodeItem -> item.node[Meta.VALUE_KEY].value ?: error("Can't convert node to a value")
is ValueItem -> item.value
}
override fun objectToMetaItem(obj: Value): MetaItem<*> = MetaItem.ValueItem(obj)
override fun objectToMetaItem(obj: Value): MetaItem = ValueItem(obj)
}
public val string: MetaConverter<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
override fun itemToObject(item: MetaItem): String = when (item) {
is NodeItem -> item.node[Meta.VALUE_KEY].value ?: error("Can't convert node to a value")
is ValueItem -> item.value
}.string
override fun objectToMetaItem(obj: String): MetaItem<*> = MetaItem.ValueItem(obj.asValue())
override fun objectToMetaItem(obj: String): MetaItem = ValueItem(obj.asValue())
}
public val boolean: MetaConverter<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
override fun itemToObject(item: MetaItem): Boolean = when (item) {
is NodeItem -> item.node[Meta.VALUE_KEY].value ?: error("Can't convert node to a value")
is ValueItem -> item.value
}.boolean
override fun objectToMetaItem(obj: Boolean): MetaItem<*> = MetaItem.ValueItem(obj.asValue())
override fun objectToMetaItem(obj: Boolean): MetaItem = ValueItem(obj.asValue())
}
public val number: MetaConverter<Number> = object : MetaConverter<Number> {
override fun itemToObject(item: MetaItem<*>): Number = when (item) {
is MetaItem.NodeItem -> item.node[Meta.VALUE_KEY].value ?: error("Can't convert node to a value")
is MetaItem.ValueItem -> item.value
override fun itemToObject(item: MetaItem): Number = when (item) {
is NodeItem -> item.node[Meta.VALUE_KEY].value ?: error("Can't convert node to a value")
is ValueItem -> item.value
}.number
override fun objectToMetaItem(obj: Number): MetaItem<*> = MetaItem.ValueItem(obj.asValue())
override fun objectToMetaItem(obj: Number): MetaItem = ValueItem(obj.asValue())
}
public val double: MetaConverter<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
override fun itemToObject(item: MetaItem): Double = when (item) {
is NodeItem -> item.node[Meta.VALUE_KEY].value ?: error("Can't convert node to a value")
is ValueItem -> item.value
}.double
override fun objectToMetaItem(obj: Double): MetaItem<*> = MetaItem.ValueItem(obj.asValue())
override fun objectToMetaItem(obj: Double): MetaItem = ValueItem(obj.asValue())
}
public val float: MetaConverter<Float> = object : MetaConverter<Float> {
override fun itemToObject(item: MetaItem<*>): Float = when (item) {
is MetaItem.NodeItem -> item.node[Meta.VALUE_KEY].value ?: error("Can't convert node to a value")
is MetaItem.ValueItem -> item.value
override fun itemToObject(item: MetaItem): Float = when (item) {
is NodeItem -> item.node[Meta.VALUE_KEY].value ?: error("Can't convert node to a value")
is ValueItem -> item.value
}.float
override fun objectToMetaItem(obj: Float): MetaItem<*> = MetaItem.ValueItem(obj.asValue())
override fun objectToMetaItem(obj: Float): MetaItem = ValueItem(obj.asValue())
}
public val int: MetaConverter<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
override fun itemToObject(item: MetaItem): Int = when (item) {
is NodeItem -> item.node[Meta.VALUE_KEY].value ?: error("Can't convert node to a value")
is ValueItem -> item.value
}.int
override fun objectToMetaItem(obj: Int): MetaItem<*> = MetaItem.ValueItem(obj.asValue())
override fun objectToMetaItem(obj: Int): MetaItem = ValueItem(obj.asValue())
}
public val long: MetaConverter<Long> = object : MetaConverter<Long> {
override fun itemToObject(item: MetaItem<*>): Long = when (item) {
is MetaItem.NodeItem -> item.node[Meta.VALUE_KEY].value ?: error("Can't convert node to a value")
is MetaItem.ValueItem -> item.value
override fun itemToObject(item: MetaItem): Long = when (item) {
is NodeItem -> item.node[Meta.VALUE_KEY].value ?: error("Can't convert node to a value")
is ValueItem -> item.value
}.long
override fun objectToMetaItem(obj: Long): MetaItem<*> = MetaItem.ValueItem(obj.asValue())
override fun objectToMetaItem(obj: Long): MetaItem = ValueItem(obj.asValue())
}
public inline fun <reified E : Enum<E>> enum(): MetaConverter<E> = object : MetaConverter<E> {
@Suppress("USELESS_CAST")
override fun itemToObject(item: MetaItem<*>): E = item.enum<E>() as? E ?: error("The Item is not a Enum")
override fun itemToObject(item: MetaItem): E = item.enum<E>() as? E ?: error("The Item is not a Enum")
override fun objectToMetaItem(obj: E): MetaItem<*> = MetaItem.ValueItem(obj.asValue())
override fun objectToMetaItem(obj: E): MetaItem = ValueItem(obj.asValue())
}
public fun <T> valueList(writer: (T) -> Value = {Value.of(it)}, reader: (Value) -> T): MetaConverter<List<T>> =
object : MetaConverter<List<T>> {
override fun itemToObject(item: MetaItem<*>): List<T> =
override fun itemToObject(item: MetaItem): List<T> =
item.value?.list?.map(reader) ?: error("The item is not a value list")
override fun objectToMetaItem(obj: List<T>): MetaItem<*> =
MetaItem.ValueItem(obj.map(writer).asValue())
override fun objectToMetaItem(obj: List<T>): MetaItem =
ValueItem(obj.map(writer).asValue())
}
}
}
public fun <T : Any> MetaConverter<T>.nullableItemToObject(item: MetaItem<*>?): T? = item?.let { itemToObject(it) }
public fun <T : Any> MetaConverter<T>.nullableObjectToMetaItem(obj: T?): MetaItem<*>? = obj?.let { objectToMetaItem(it) }
public fun <T : Any> MetaConverter<T>.nullableItemToObject(item: MetaItem?): T? = item?.let { itemToObject(it) }
public fun <T : Any> MetaConverter<T>.nullableObjectToMetaItem(obj: T?): MetaItem? = obj?.let { objectToMetaItem(it) }
public fun <T : Any> MetaConverter<T>.metaToObject(meta: Meta): T = itemToObject(MetaItem.NodeItem(meta))
public fun <T : Any> MetaConverter<T>.valueToObject(value: Value): T = itemToObject(MetaItem.ValueItem(value))
public fun <T : Any> MetaConverter<T>.metaToObject(meta: Meta): T = itemToObject(NodeItem(meta))
public fun <T : Any> MetaConverter<T>.valueToObject(value: Value): T = itemToObject(ValueItem(value))

View File

@ -11,7 +11,7 @@ public interface TransformationRule {
/**
* Check if this transformation should be applied to a node with given name and value
*/
public fun matches(name: Name, item: MetaItem<*>?): Boolean
public fun matches(name: Name, item: MetaItem?): Boolean
/**
* Select all items to be transformed. Item could be a value as well as node
@ -24,7 +24,7 @@ public interface TransformationRule {
/**
* Apply transformation for a single item (Node or Value) to the target
*/
public fun <M : MutableMeta<M>> transformItem(name: Name, item: MetaItem<*>?, target: M): Unit
public fun <M : MutableMeta<M>> transformItem(name: Name, item: MetaItem?, target: M): Unit
}
/**
@ -32,14 +32,14 @@ public interface TransformationRule {
*/
public data class KeepTransformationRule(val selector: (Name) -> Boolean) :
TransformationRule {
override fun matches(name: Name, item: MetaItem<*>?): Boolean {
override fun matches(name: Name, item: MetaItem?): Boolean {
return selector(name)
}
override fun selectItems(meta: Meta): Sequence<Name> =
meta.itemSequence().map { it.first }.filter(selector)
override fun <M : MutableMeta<M>> transformItem(name: Name, item: MetaItem<*>?, target: M) {
override fun <M : MutableMeta<M>> transformItem(name: Name, item: MetaItem?, target: M) {
if (selector(name)) target.set(name, item)
}
}
@ -49,15 +49,15 @@ public data class KeepTransformationRule(val selector: (Name) -> Boolean) :
*/
public data class SingleItemTransformationRule(
val from: Name,
val transform: MutableMeta<*>.(Name, MetaItem<*>?) -> Unit,
val transform: MutableMeta<*>.(Name, MetaItem?) -> Unit,
) : TransformationRule {
override fun matches(name: Name, item: MetaItem<*>?): Boolean {
override fun matches(name: Name, item: MetaItem?): Boolean {
return name == from
}
override fun selectItems(meta: Meta): Sequence<Name> = sequenceOf(from)
override fun <M : MutableMeta<M>> transformItem(name: Name, item: MetaItem<*>?, target: M) {
override fun <M : MutableMeta<M>> transformItem(name: Name, item: MetaItem?, target: M) {
if (name == this.from) {
target.transform(name, item)
}
@ -66,13 +66,13 @@ public data class SingleItemTransformationRule(
public data class RegexItemTransformationRule(
val from: Regex,
val transform: MutableMeta<*>.(name: Name, MatchResult, MetaItem<*>?) -> Unit,
val transform: MutableMeta<*>.(name: Name, MatchResult, MetaItem?) -> Unit,
) : TransformationRule {
override fun matches(name: Name, item: MetaItem<*>?): Boolean {
override fun matches(name: Name, item: MetaItem?): Boolean {
return from.matches(name.toString())
}
override fun <M : MutableMeta<M>> transformItem(name: Name, item: MetaItem<*>?, target: M) {
override fun <M : MutableMeta<M>> transformItem(name: Name, item: MetaItem?, target: M) {
val match = from.matchEntire(name.toString())
if (match != null) {
target.transform(name, match, item)
@ -177,7 +177,7 @@ public class MetaTransformationBuilder {
/**
* Move an item from [from] to [to], optionally applying [operation] it defined
*/
public fun move(from: Name, to: Name, operation: (MetaItem<*>?) -> Any? = { it }) {
public fun move(from: Name, to: Name, operation: (MetaItem?) -> Any? = { it }) {
transformations.add(
SingleItemTransformationRule(from) { _, item ->
set(to, operation(item))

View File

@ -22,9 +22,9 @@ public fun Value.toDynamic(): dynamic {
public fun Meta.toDynamic(): dynamic {
if (this is DynamicMeta) return this.obj
fun MetaItem<*>.toDynamic(): dynamic = when (this) {
is MetaItem.ValueItem -> this.value.toDynamic()
is MetaItem.NodeItem -> this.node.toDynamic()
fun MetaItem.toDynamic(): dynamic = when (this) {
is ValueItem -> this.value.toDynamic()
is NodeItem -> this.node.toDynamic()
}
val res = js("{}")
@ -48,27 +48,27 @@ public class DynamicMeta(internal val obj: dynamic) : MetaBase() {
(jsTypeOf(obj) != "object")
@Suppress("UNCHECKED_CAST", "USELESS_CAST")
private fun asItem(obj: dynamic): MetaItem<DynamicMeta>? {
private fun asItem(obj: dynamic): TypedMetaItem<DynamicMeta>? {
return when {
obj == null -> MetaItem.ValueItem(Null)
isArray(obj) && (obj as Array<Any?>).all { isPrimitive(it) } -> MetaItem.ValueItem(Value.of(obj as Array<Any?>))
obj == null -> ValueItem(Null)
isArray(obj) && (obj as Array<Any?>).all { isPrimitive(it) } -> ValueItem(Value.of(obj as Array<Any?>))
else -> when (jsTypeOf(obj)) {
"boolean" -> MetaItem.ValueItem(Value.of(obj as Boolean))
"number" -> MetaItem.ValueItem(Value.of(obj as Number))
"string" -> MetaItem.ValueItem(Value.of(obj as String))
"object" -> MetaItem.NodeItem(DynamicMeta(obj))
"boolean" -> ValueItem(Value.of(obj as Boolean))
"number" -> ValueItem(Value.of(obj as Number))
"string" -> ValueItem(Value.of(obj as String))
"object" -> NodeItem(DynamicMeta(obj))
else -> null
}
}
}
override val items: Map<NameToken, MetaItem<DynamicMeta>>
get() = keys().flatMap<String, Pair<NameToken, MetaItem<DynamicMeta>>> { key ->
override val items: Map<NameToken, TypedMetaItem<DynamicMeta>>
get() = keys().flatMap<String, Pair<NameToken, TypedMetaItem<DynamicMeta>>> { key ->
val value = obj[key] ?: return@flatMap emptyList()
if (isArray(value)) {
val array = value as Array<Any?>
return@flatMap if (array.all { isPrimitive(it) }) {
listOf(NameToken(key) to MetaItem.ValueItem(Value.of(array)))
listOf(NameToken(key) to ValueItem(Value.of(array)))
} else {
array.mapIndexedNotNull { index, it ->
val item = asItem(it) ?: return@mapIndexedNotNull null