Add retarget method to change the target of a Scheme

This commit is contained in:
Alexander Nozik 2020-12-26 13:34:53 +03:00
parent 7a9f7da7f2
commit f946777f40
3 changed files with 19 additions and 9 deletions

View File

@ -455,10 +455,11 @@ public class hep/dataforge/meta/Scheme : hep/dataforge/meta/MetaRepr, hep/datafo
} }
public final class hep/dataforge/meta/SchemeKt { public final class hep/dataforge/meta/SchemeKt {
public static final fun inflate (Lhep/dataforge/meta/Specification;Lhep/dataforge/meta/MutableItemProvider;Lhep/dataforge/meta/ItemProvider;Lhep/dataforge/meta/descriptors/NodeDescriptor;)Lhep/dataforge/meta/Scheme;
public static synthetic fun inflate$default (Lhep/dataforge/meta/Specification;Lhep/dataforge/meta/MutableItemProvider;Lhep/dataforge/meta/ItemProvider;Lhep/dataforge/meta/descriptors/NodeDescriptor;ILjava/lang/Object;)Lhep/dataforge/meta/Scheme;
public static final fun invoke (Lhep/dataforge/meta/Scheme;Lkotlin/jvm/functions/Function1;)Lhep/dataforge/meta/Scheme; public static final fun invoke (Lhep/dataforge/meta/Scheme;Lkotlin/jvm/functions/Function1;)Lhep/dataforge/meta/Scheme;
public static final fun isEmpty (Lhep/dataforge/meta/Scheme;)Z public static final fun isEmpty (Lhep/dataforge/meta/Scheme;)Z
public static final fun retarget (Lhep/dataforge/meta/Scheme;Lhep/dataforge/meta/MutableItemProvider;)Lhep/dataforge/meta/Scheme;
public static final fun wrap (Lhep/dataforge/meta/Specification;Lhep/dataforge/meta/MutableItemProvider;Lhep/dataforge/meta/ItemProvider;Lhep/dataforge/meta/descriptors/NodeDescriptor;)Lhep/dataforge/meta/Scheme;
public static synthetic fun wrap$default (Lhep/dataforge/meta/Specification;Lhep/dataforge/meta/MutableItemProvider;Lhep/dataforge/meta/ItemProvider;Lhep/dataforge/meta/descriptors/NodeDescriptor;ILjava/lang/Object;)Lhep/dataforge/meta/Scheme;
} }
public class hep/dataforge/meta/SchemeSpec : hep/dataforge/meta/Specification, hep/dataforge/meta/descriptors/Described { public class hep/dataforge/meta/SchemeSpec : hep/dataforge/meta/Specification, hep/dataforge/meta/descriptors/Described {

View File

@ -17,7 +17,7 @@ public open class Scheme() : MutableItemProvider, Described, MetaRepr {
final override var descriptor: NodeDescriptor? = null final override var descriptor: NodeDescriptor? = null
internal fun inflate( internal fun wrap(
items: MutableItemProvider, items: MutableItemProvider,
default: ItemProvider? = null, default: ItemProvider? = null,
descriptor: NodeDescriptor? = null, descriptor: NodeDescriptor? = null,
@ -84,14 +84,23 @@ public open class Scheme() : MutableItemProvider, Described, MetaRepr {
*/ */
public fun Scheme.isEmpty(): Boolean = rootItem == null public fun Scheme.isEmpty(): Boolean = rootItem == null
public fun <T : Scheme, S : Specification<T>> S.inflate( /**
* Create a new empty [Scheme] object (including defaults) and inflate it around existing [MutableItemProvider].
* Items already present in the scheme are used as defaults.
*/
public fun <T : Scheme, S : Specification<T>> S.wrap(
items: MutableItemProvider, items: MutableItemProvider,
default: ItemProvider? = null, default: ItemProvider? = null,
descriptor: NodeDescriptor? = null, descriptor: NodeDescriptor? = null,
): T = empty().apply { ): T = empty().apply {
inflate(items, default, descriptor) wrap(items, default, descriptor)
} }
/**
* Relocate scheme target onto given [MutableItemProvider]. Old provider does not get updates anymore.
* Current state of the scheme used as a default.
*/
public fun <T : Scheme> T.retarget(provider: MutableItemProvider) :T = apply { wrap(provider) }
/** /**
* A shortcut to edit a [Scheme] object in-place * A shortcut to edit a [Scheme] object in-place
@ -107,10 +116,10 @@ public open class SchemeSpec<out T : Scheme>(
override fun empty(): T = builder() override fun empty(): T = builder()
override fun read(items: ItemProvider): T = inflate(Config(), items, descriptor) override fun read(items: ItemProvider): T = wrap(Config(), items, descriptor)
override fun write(target: MutableItemProvider, defaultProvider: ItemProvider): T = override fun write(target: MutableItemProvider, defaultProvider: ItemProvider): T =
inflate(target, defaultProvider, descriptor) wrap(target, defaultProvider, descriptor)
//TODO Generate descriptor from Scheme class //TODO Generate descriptor from Scheme class
override val descriptor: NodeDescriptor? get() = null override val descriptor: NodeDescriptor? get() = null

View File

@ -14,10 +14,10 @@ class SpecificationTest {
override fun empty(): TestScheme = TestScheme() override fun empty(): TestScheme = TestScheme()
override fun read(items: ItemProvider): TestScheme = override fun read(items: ItemProvider): TestScheme =
inflate(Config(), items) wrap(Config(), items)
override fun write(target: MutableItemProvider, defaultProvider: ItemProvider): TestScheme = override fun write(target: MutableItemProvider, defaultProvider: ItemProvider): TestScheme =
inflate(target, defaultProvider) wrap(target, defaultProvider)
} }
} }