This commit is contained in:
Alexander Nozik 2020-12-24 09:48:14 +03:00
parent e931994b75
commit 397a19fb32
10 changed files with 48 additions and 21 deletions

View File

@ -128,6 +128,7 @@ public final class hep/dataforge/meta/ItemProvider$Companion {
public final class hep/dataforge/meta/ItemProviderKt { 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;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;Ljava/lang/String;)Lhep/dataforge/meta/MetaItem;
public static final fun getChild (Lhep/dataforge/meta/ItemProvider;Lhep/dataforge/names/Name;)Lhep/dataforge/meta/ItemProvider;
public static final fun getIndexed (Lhep/dataforge/meta/ItemProvider;Lhep/dataforge/names/Name;)Ljava/util/Map; public static final fun getIndexed (Lhep/dataforge/meta/ItemProvider;Lhep/dataforge/names/Name;)Ljava/util/Map;
public static final fun getIndexed (Lhep/dataforge/meta/ItemProvider;Ljava/lang/String;)Ljava/util/Map; public static final fun getIndexed (Lhep/dataforge/meta/ItemProvider;Ljava/lang/String;)Ljava/util/Map;
public static final fun getIndexed (Lhep/dataforge/meta/TypedMeta;Lhep/dataforge/names/Name;)Ljava/util/Map; public static final fun getIndexed (Lhep/dataforge/meta/TypedMeta;Lhep/dataforge/names/Name;)Ljava/util/Map;
@ -251,7 +252,6 @@ public final class hep/dataforge/meta/MetaBuilder : hep/dataforge/meta/AbstractM
public final class hep/dataforge/meta/MetaBuilderKt { public final class hep/dataforge/meta/MetaBuilderKt {
public static final fun Meta (Lkotlin/jvm/functions/Function1;)Lhep/dataforge/meta/MetaBuilder; public static final fun Meta (Lkotlin/jvm/functions/Function1;)Lhep/dataforge/meta/MetaBuilder;
public static final fun builder (Lhep/dataforge/meta/Meta;)Lhep/dataforge/meta/MetaBuilder; public static final fun builder (Lhep/dataforge/meta/Meta;)Lhep/dataforge/meta/MetaBuilder;
public static final fun edit (Lhep/dataforge/meta/Meta;Lkotlin/jvm/functions/Function1;)Lhep/dataforge/meta/MetaBuilder;
} }
public abstract class hep/dataforge/meta/MetaItem { public abstract class hep/dataforge/meta/MetaItem {
@ -415,6 +415,8 @@ public abstract interface class hep/dataforge/meta/MutableItemProvider : hep/dat
} }
public final class hep/dataforge/meta/MutableItemProviderKt { public final class hep/dataforge/meta/MutableItemProviderKt {
public static final fun editChild (Lhep/dataforge/meta/MutableItemProvider;Lhep/dataforge/names/Name;Lkotlin/jvm/functions/Function1;)Lhep/dataforge/meta/MutableItemProvider;
public static final fun getChild (Lhep/dataforge/meta/MutableItemProvider;Lhep/dataforge/names/Name;)Lhep/dataforge/meta/MutableItemProvider;
public static final fun remove (Lhep/dataforge/meta/MutableItemProvider;Lhep/dataforge/names/Name;)V 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 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/Meta;)V

View File

@ -62,6 +62,13 @@ 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
*/
public fun ItemProvider.getChild(childName: Name): ItemProvider = get(childName).node ?: ItemProvider.EMPTY
public fun ItemProvider.getChild(childName: String): ItemProvider = getChild(childName.toName())
/** /**
* Get all items matching given name. * Get all items matching given name.
*/ */

View File

@ -2,6 +2,7 @@
package hep.dataforge.meta package hep.dataforge.meta
import hep.dataforge.meta.JsonMeta.Companion.JSON_ARRAY_KEY
import hep.dataforge.meta.descriptors.ItemDescriptor import hep.dataforge.meta.descriptors.ItemDescriptor
import hep.dataforge.meta.descriptors.ItemDescriptor.Companion.DEFAULT_INDEX_KEY import hep.dataforge.meta.descriptors.ItemDescriptor.Companion.DEFAULT_INDEX_KEY
import hep.dataforge.meta.descriptors.NodeDescriptor import hep.dataforge.meta.descriptors.NodeDescriptor

View File

@ -4,6 +4,7 @@ import hep.dataforge.meta.MetaItem.NodeItem
import hep.dataforge.meta.MetaItem.ValueItem import hep.dataforge.meta.MetaItem.ValueItem
import hep.dataforge.names.* import hep.dataforge.names.*
import hep.dataforge.values.Value import hep.dataforge.values.Value
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json

View File

@ -133,11 +133,6 @@ public fun Meta.builder(): MetaBuilder {
} }
} }
/**
* Create a deep copy of this meta and apply builder to it
*/
public inline fun Meta.edit(builder: MetaBuilder.() -> Unit): MetaBuilder = builder().apply(builder)
/** /**
* Build a [MetaBuilder] using given transformation * Build a [MetaBuilder] using given transformation
*/ */

View File

@ -3,6 +3,7 @@ package hep.dataforge.meta
import hep.dataforge.meta.MetaItem.NodeItem import hep.dataforge.meta.MetaItem.NodeItem
import hep.dataforge.meta.MetaItem.ValueItem import hep.dataforge.meta.MetaItem.ValueItem
import hep.dataforge.values.* import hep.dataforge.values.*
import kotlinx.serialization.Serializable
/** /**
* A member of the meta tree. Could be represented as one of following: * A member of the meta tree. Could be represented as one of following:

View File

@ -74,6 +74,31 @@ public operator fun MutableItemProvider.set(name: Name, metas: Iterable<Meta>):
public operator fun MutableItemProvider.set(name: String, metas: Iterable<Meta>): Unit = public operator fun MutableItemProvider.set(name: String, metas: Iterable<Meta>): Unit =
setIndexed(name.toName(), metas) setIndexed(name.toName(), metas)
/**
* Get a [MutableItemProvider] referencing a child node
*/
public fun MutableItemProvider.getChild(childName: Name): MutableItemProvider {
fun createProvider() = object : MutableItemProvider {
override fun setItem(name: Name, item: MetaItem<*>?) {
this@getChild.setItem(childName + name, item)
}
override fun getItem(name: Name): MetaItem<*>? = this@getChild.getItem(childName + name)
}
return when {
childName.isEmpty() -> this
this is MutableMeta<*> -> {
get(childName).node ?: createProvider()
}
else -> {
createProvider()
}
}
}
public fun MutableItemProvider.getChild(childName: String): MutableItemProvider = getChild(childName.toName())
/** /**
* Update existing mutable node with another node. The rules are following: * Update existing mutable node with another node. The rules are following:
* * value replaces anything * * value replaces anything
@ -84,6 +109,12 @@ public fun MutableItemProvider.update(meta: Meta) {
meta.valueSequence().forEach { (name, value) -> set(name, value) } meta.valueSequence().forEach { (name, value) -> set(name, value) }
} }
/**
* Edit a provider child at given name location
*/
public fun MutableItemProvider.editChild(name: Name, builder: MutableItemProvider.() -> Unit): MutableItemProvider =
getChild(name).apply(builder)
/** /**
* Create a mutable item provider that uses given provider for default values if those are not found in this provider. * Create a mutable item provider that uses given provider for default values if those are not found in this provider.
* Changes are propagated only to this provider. * Changes are propagated only to this provider.

View File

@ -46,7 +46,7 @@ public abstract class AbstractMutableMeta<M : MutableMeta<M>> : AbstractTypedMet
override fun setItem(name: Name, item: MetaItem<*>?) { override fun setItem(name: Name, item: MetaItem<*>?) {
when (name.length) { when (name.length) {
0 -> error("Can't setValue meta item for empty name") 0 -> error("Can't set a meta item for empty name")
1 -> { 1 -> {
val token = name.firstOrNull()!! val token = name.firstOrNull()!!
val oldItem: MetaItem<M>? = getItem(name) val oldItem: MetaItem<M>? = getItem(name)

View File

@ -110,17 +110,6 @@ public open class SchemeSpec<T : Scheme>(
final override inline operator fun invoke(action: T.() -> Unit): T = empty().apply(action) final override inline operator fun invoke(action: T.() -> Unit): T = empty().apply(action)
} }
///**
// * A scheme that uses [Meta] as a default layer
// */
//public open class MetaScheme(
// private val meta: Meta,
// override val descriptor: NodeDescriptor? = null,
// config: Config = Config(),
//) : Scheme(config, meta) {
// override val defaultLayer: Meta get() = Laminate(meta, descriptor?.defaultItem().node)
//}
public fun Meta.asScheme(): Scheme = Scheme().apply { public fun Meta.asScheme(): Scheme = Scheme().apply {
items = this@asScheme.asConfig() items = this@asScheme.asConfig()
} }

View File

@ -49,7 +49,7 @@ public data class KeepTransformationRule(val selector: (Name) -> Boolean) :
*/ */
public data class SingleItemTransformationRule( public data class SingleItemTransformationRule(
val from: Name, val from: Name,
val transform: MutableMeta<*>.(Name, MetaItem<*>?) -> Unit val transform: MutableMeta<*>.(Name, MetaItem<*>?) -> Unit,
) : TransformationRule { ) : TransformationRule {
override fun matches(name: Name, item: MetaItem<*>?): Boolean { override fun matches(name: Name, item: MetaItem<*>?): Boolean {
return name == from return name == from
@ -66,7 +66,7 @@ public data class SingleItemTransformationRule(
public data class RegexItemTransformationRule( public data class RegexItemTransformationRule(
val from: Regex, val from: Regex,
val transform: MutableMeta<*>.(name: Name, MatchResult, MetaItem<*>?) -> Unit val transform: MutableMeta<*>.(name: Name, MatchResult, MetaItem<*>?) -> Unit,
) : TransformationRule { ) : TransformationRule {
override fun matches(name: Name, item: MetaItem<*>?): Boolean { override fun matches(name: Name, item: MetaItem<*>?): Boolean {
return from.matches(name.toString()) return from.matches(name.toString())
@ -116,7 +116,7 @@ public inline class MetaTransformation(public val transformations: Collection<Tr
* Transform a meta, replacing all elements found in rules with transformed entries * Transform a meta, replacing all elements found in rules with transformed entries
*/ */
public fun apply(source: Meta): Meta = public fun apply(source: Meta): Meta =
source.edit { source.builder().apply {
transformations.forEach { rule -> transformations.forEach { rule ->
rule.selectItems(source).forEach { name -> rule.selectItems(source).forEach { name ->
remove(name) remove(name)