Separate ReadOnlySpecification. Cleanup Scheme inflation
This commit is contained in:
parent
9c3f9420ea
commit
81375d4644
@ -431,16 +431,24 @@ public abstract interface class hep/dataforge/meta/ObservableItemProvider : hep/
|
||||
public abstract fun removeListener (Ljava/lang/Object;)V
|
||||
}
|
||||
|
||||
public abstract interface class hep/dataforge/meta/ReadOnlySpecification {
|
||||
public abstract fun empty ()Lhep/dataforge/meta/ItemProvider;
|
||||
public abstract fun invoke (Lkotlin/jvm/functions/Function1;)Lhep/dataforge/meta/ItemProvider;
|
||||
public abstract fun read (Lhep/dataforge/meta/ItemProvider;)Lhep/dataforge/meta/ItemProvider;
|
||||
}
|
||||
|
||||
public final class hep/dataforge/meta/ReadOnlySpecification$DefaultImpls {
|
||||
public static fun invoke (Lhep/dataforge/meta/ReadOnlySpecification;Lkotlin/jvm/functions/Function1;)Lhep/dataforge/meta/ItemProvider;
|
||||
}
|
||||
|
||||
public class hep/dataforge/meta/Scheme : hep/dataforge/meta/MetaRepr, hep/dataforge/meta/MutableItemProvider, hep/dataforge/meta/descriptors/Described {
|
||||
public fun <init> ()V
|
||||
public fun <init> (Lhep/dataforge/meta/MutableItemProvider;Lhep/dataforge/meta/ItemProvider;Lhep/dataforge/meta/descriptors/NodeDescriptor;)V
|
||||
public synthetic fun <init> (Lhep/dataforge/meta/MutableItemProvider;Lhep/dataforge/meta/ItemProvider;Lhep/dataforge/meta/descriptors/NodeDescriptor;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
|
||||
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 final fun getDescriptor ()Lhep/dataforge/meta/descriptors/NodeDescriptor;
|
||||
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 final fun setDescriptor (Lhep/dataforge/meta/descriptors/NodeDescriptor;)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;
|
||||
@ -448,22 +456,21 @@ public class hep/dataforge/meta/Scheme : hep/dataforge/meta/MetaRepr, hep/datafo
|
||||
}
|
||||
|
||||
public final class hep/dataforge/meta/SchemeKt {
|
||||
public static final fun asScheme (Lhep/dataforge/meta/Meta;)Lhep/dataforge/meta/Scheme;
|
||||
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 toScheme (Lhep/dataforge/meta/Meta;Lhep/dataforge/meta/Specification;Lkotlin/jvm/functions/Function1;)Lhep/dataforge/meta/MutableItemProvider;
|
||||
public static synthetic fun toScheme$default (Lhep/dataforge/meta/Meta;Lhep/dataforge/meta/Specification;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lhep/dataforge/meta/MutableItemProvider;
|
||||
}
|
||||
|
||||
public class hep/dataforge/meta/SchemeSpec : hep/dataforge/meta/Specification, hep/dataforge/meta/descriptors/Described {
|
||||
public fun <init> (Lkotlin/jvm/functions/Function0;)V
|
||||
public fun <init> (Lkotlin/jvm/functions/Function3;)V
|
||||
public synthetic fun empty ()Lhep/dataforge/meta/MutableItemProvider;
|
||||
public synthetic fun empty ()Lhep/dataforge/meta/ItemProvider;
|
||||
public fun empty ()Lhep/dataforge/meta/Scheme;
|
||||
public synthetic fun getDescriptor ()Lhep/dataforge/meta/descriptors/ItemDescriptor;
|
||||
public fun getDescriptor ()Lhep/dataforge/meta/descriptors/NodeDescriptor;
|
||||
public synthetic fun invoke (Lkotlin/jvm/functions/Function1;)Lhep/dataforge/meta/ItemProvider;
|
||||
public synthetic fun invoke (Lkotlin/jvm/functions/Function1;)Lhep/dataforge/meta/MutableItemProvider;
|
||||
public final fun invoke (Lkotlin/jvm/functions/Function1;)Lhep/dataforge/meta/Scheme;
|
||||
public synthetic fun read (Lhep/dataforge/meta/ItemProvider;)Lhep/dataforge/meta/MutableItemProvider;
|
||||
public synthetic fun read (Lhep/dataforge/meta/ItemProvider;)Lhep/dataforge/meta/ItemProvider;
|
||||
public fun read (Lhep/dataforge/meta/ItemProvider;)Lhep/dataforge/meta/Scheme;
|
||||
public synthetic fun write (Lhep/dataforge/meta/MutableItemProvider;Lhep/dataforge/meta/ItemProvider;)Lhep/dataforge/meta/MutableItemProvider;
|
||||
public fun write (Lhep/dataforge/meta/MutableItemProvider;Lhep/dataforge/meta/ItemProvider;)Lhep/dataforge/meta/Scheme;
|
||||
@ -478,26 +485,20 @@ public final class hep/dataforge/meta/SealedMetaKt {
|
||||
public static final fun seal (Lhep/dataforge/meta/TypedMetaItem;)Lhep/dataforge/meta/TypedMetaItem;
|
||||
}
|
||||
|
||||
public abstract interface class hep/dataforge/meta/Specification {
|
||||
public abstract fun empty ()Lhep/dataforge/meta/MutableItemProvider;
|
||||
public abstract fun invoke (Lkotlin/jvm/functions/Function1;)Lhep/dataforge/meta/MutableItemProvider;
|
||||
public abstract fun read (Lhep/dataforge/meta/ItemProvider;)Lhep/dataforge/meta/MutableItemProvider;
|
||||
public abstract interface class hep/dataforge/meta/Specification : hep/dataforge/meta/ReadOnlySpecification {
|
||||
public abstract fun write (Lhep/dataforge/meta/MutableItemProvider;Lhep/dataforge/meta/ItemProvider;)Lhep/dataforge/meta/MutableItemProvider;
|
||||
}
|
||||
|
||||
public final class hep/dataforge/meta/Specification$DefaultImpls {
|
||||
public static fun empty (Lhep/dataforge/meta/Specification;)Lhep/dataforge/meta/MutableItemProvider;
|
||||
public static fun invoke (Lhep/dataforge/meta/Specification;Lkotlin/jvm/functions/Function1;)Lhep/dataforge/meta/MutableItemProvider;
|
||||
public static synthetic fun write$default (Lhep/dataforge/meta/Specification;Lhep/dataforge/meta/MutableItemProvider;Lhep/dataforge/meta/ItemProvider;ILjava/lang/Object;)Lhep/dataforge/meta/MutableItemProvider;
|
||||
}
|
||||
|
||||
public final class hep/dataforge/meta/SpecificationKt {
|
||||
public static final fun spec (Lhep/dataforge/meta/MutableItemProvider;Lhep/dataforge/meta/Specification;Lhep/dataforge/meta/Scheme;Lhep/dataforge/names/Name;)Lkotlin/properties/ReadWriteProperty;
|
||||
public static final fun spec (Lhep/dataforge/meta/MutableItemProvider;Lhep/dataforge/meta/Specification;Lhep/dataforge/names/Name;)Lkotlin/properties/ReadWriteProperty;
|
||||
public static synthetic fun spec$default (Lhep/dataforge/meta/MutableItemProvider;Lhep/dataforge/meta/Specification;Lhep/dataforge/meta/Scheme;Lhep/dataforge/names/Name;ILjava/lang/Object;)Lkotlin/properties/ReadWriteProperty;
|
||||
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/Configurable;Lhep/dataforge/meta/Specification;Lkotlin/jvm/functions/Function1;)Lhep/dataforge/meta/Configurable;
|
||||
public static final fun update (Lhep/dataforge/meta/MutableItemProvider;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;)V
|
||||
public static final fun update (Lhep/dataforge/meta/MutableItemProvider;Lhep/dataforge/meta/Specification;Lkotlin/jvm/functions/Function1;)V
|
||||
public static final fun withSpec (Lhep/dataforge/meta/TypedMetaItem;Lhep/dataforge/meta/Specification;)Lhep/dataforge/meta/MutableItemProvider;
|
||||
}
|
||||
|
||||
|
@ -116,8 +116,8 @@ public fun MutableItemProvider.editChild(name: Name, builder: MutableItemProvide
|
||||
* 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.
|
||||
*/
|
||||
public fun MutableItemProvider.withDefault(default: ItemProvider): MutableItemProvider =
|
||||
if (default is Meta && default.isEmpty()) {
|
||||
public fun MutableItemProvider.withDefault(default: ItemProvider?): MutableItemProvider =
|
||||
if (default == null || (default is Meta && default.isEmpty())) {
|
||||
//Optimize for use with empty default
|
||||
this
|
||||
} else object : MutableItemProvider {
|
||||
|
@ -11,30 +11,24 @@ import hep.dataforge.names.asName
|
||||
*/
|
||||
public open class Scheme() : MutableItemProvider, Described, MetaRepr {
|
||||
|
||||
public var items: MutableItemProvider = MetaBuilder()
|
||||
internal set(value) {
|
||||
//Fix problem with `init` blocks in specifications
|
||||
field = value.apply {
|
||||
field.rootNode?.let { update(it) }
|
||||
}
|
||||
}
|
||||
private var items: MutableItemProvider = MetaBuilder()
|
||||
|
||||
internal var default: ItemProvider? = null
|
||||
private var default: ItemProvider? = null
|
||||
|
||||
final override var descriptor: NodeDescriptor? = null
|
||||
internal set
|
||||
|
||||
public constructor(
|
||||
internal fun inflate(
|
||||
items: MutableItemProvider,
|
||||
default: ItemProvider? = null,
|
||||
descriptor: NodeDescriptor? = null,
|
||||
) : this(){
|
||||
) {
|
||||
//use properties in the init block as default
|
||||
this.default = items.withDefault(default)
|
||||
//reset values, defaults are already saved
|
||||
this.items = items
|
||||
this.default = default
|
||||
this.descriptor = descriptor
|
||||
}
|
||||
|
||||
|
||||
private fun getDefaultItem(name: Name): MetaItem? {
|
||||
return default?.get(name) ?: descriptor?.get(name)?.defaultItem()
|
||||
}
|
||||
@ -87,6 +81,16 @@ public open class Scheme() : MutableItemProvider, Described, MetaRepr {
|
||||
public fun isEmpty(): Boolean = toMeta().isEmpty()
|
||||
}
|
||||
|
||||
|
||||
public fun <T : Scheme, S : Specification<T>> S.inflate(
|
||||
items: MutableItemProvider,
|
||||
default: ItemProvider? = null,
|
||||
descriptor: NodeDescriptor? = null,
|
||||
): T = empty().apply {
|
||||
inflate(items, default, descriptor)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A shortcut to edit a [Scheme] object in-place
|
||||
*/
|
||||
@ -95,23 +99,16 @@ public inline operator fun <T : Scheme> T.invoke(block: T.() -> Unit): T = apply
|
||||
/**
|
||||
* A specification for simplified generation of wrappers
|
||||
*/
|
||||
public open class SchemeSpec<T : Scheme>(
|
||||
private val builder: (target: MutableItemProvider, defaultProvider: ItemProvider, descriptor: NodeDescriptor?) -> T,
|
||||
public open class SchemeSpec<out T : Scheme>(
|
||||
private val builder: () -> T,
|
||||
) : Specification<T>, Described {
|
||||
|
||||
public constructor(emptyBuilder: () -> T) : this({ target: MutableItemProvider, defaultProvider: ItemProvider, descriptor: NodeDescriptor? ->
|
||||
emptyBuilder().apply {
|
||||
this.items = target
|
||||
this.default = defaultProvider
|
||||
this.descriptor = descriptor
|
||||
}
|
||||
})
|
||||
override fun empty(): T = builder()
|
||||
|
||||
override fun read(items: ItemProvider): T =
|
||||
builder(Config(), items, descriptor)
|
||||
override fun read(items: ItemProvider): T = inflate(Config(), items, descriptor)
|
||||
|
||||
override fun write(target: MutableItemProvider, defaultProvider: ItemProvider): T =
|
||||
builder(target, defaultProvider, descriptor)
|
||||
inflate(target, defaultProvider, descriptor)
|
||||
|
||||
//TODO Generate descriptor from Scheme class
|
||||
override val descriptor: NodeDescriptor? get() = null
|
||||
@ -119,7 +116,3 @@ public open class SchemeSpec<T : Scheme>(
|
||||
@Suppress("OVERRIDE_BY_INLINE")
|
||||
final override inline operator fun invoke(action: T.() -> Unit): T = empty().apply(action)
|
||||
}
|
||||
|
||||
public fun Meta.asScheme(): Scheme = Scheme().apply {
|
||||
items = this@asScheme.asConfig()
|
||||
}
|
@ -5,26 +5,18 @@ import hep.dataforge.names.asName
|
||||
import kotlin.properties.ReadWriteProperty
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
/**
|
||||
* Allows to apply custom configuration in a type safe way to simple untyped configuration.
|
||||
* By convention [Scheme] companion should inherit this class
|
||||
*
|
||||
*/
|
||||
public interface Specification<T : MutableItemProvider> {
|
||||
public interface ReadOnlySpecification<out T : ItemProvider> {
|
||||
|
||||
/**
|
||||
* Read generic read-only meta with this [Specification] producing instance of desired type.
|
||||
*/
|
||||
public fun read(items: ItemProvider): T
|
||||
|
||||
/**
|
||||
* Wrap [MutableItemProvider], using it as inner storage (changes to [Specification] are reflected on [MutableItemProvider]
|
||||
*/
|
||||
public fun write(target: MutableItemProvider, defaultProvider: ItemProvider = ItemProvider.EMPTY): T
|
||||
|
||||
/**
|
||||
* Generate an empty object
|
||||
*/
|
||||
public fun empty(): T = read(Meta.EMPTY)
|
||||
public fun empty(): T
|
||||
|
||||
/**
|
||||
* A convenience method to use specifications in builders
|
||||
@ -32,6 +24,19 @@ public interface Specification<T : MutableItemProvider> {
|
||||
public operator fun invoke(action: T.() -> Unit): T = empty().apply(action)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Allows to apply custom configuration in a type safe way to simple untyped configuration.
|
||||
* By convention [Scheme] companion should inherit this class
|
||||
*
|
||||
*/
|
||||
public interface Specification<out T : MutableItemProvider>: ReadOnlySpecification<T> {
|
||||
/**
|
||||
* Wrap [MutableItemProvider], using it as inner storage (changes to [Specification] are reflected on [MutableItemProvider]
|
||||
*/
|
||||
public fun write(target: MutableItemProvider, defaultProvider: ItemProvider = ItemProvider.EMPTY): T
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a [MutableItemProvider] using given specification
|
||||
*/
|
||||
|
@ -4,22 +4,22 @@ import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class SpecificationTest {
|
||||
class TestStyled(target: MutableItemProvider, default: ItemProvider?) : Scheme(target, default) {
|
||||
class TestScheme : Scheme() {
|
||||
var list by numberList(1, 2, 3)
|
||||
|
||||
companion object : Specification<TestStyled> {
|
||||
override fun read(items: ItemProvider): TestStyled = TestStyled(Config(), items)
|
||||
|
||||
override fun write(target: MutableItemProvider, defaultProvider: ItemProvider): TestStyled =
|
||||
TestStyled(target, defaultProvider)
|
||||
}
|
||||
}
|
||||
|
||||
class TestScheme : Scheme() {
|
||||
var a by int()
|
||||
var b by string()
|
||||
|
||||
companion object : SchemeSpec<TestScheme>(::TestScheme)
|
||||
companion object : Specification<TestScheme> {
|
||||
override fun empty(): TestScheme = TestScheme()
|
||||
|
||||
override fun read(items: ItemProvider): TestScheme =
|
||||
inflate(Config(), items)
|
||||
|
||||
override fun write(target: MutableItemProvider, defaultProvider: ItemProvider): TestScheme =
|
||||
inflate(target, defaultProvider)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// @Test
|
||||
@ -49,7 +49,7 @@ class SpecificationTest {
|
||||
|
||||
@Test
|
||||
fun testSpecific() {
|
||||
val testObject = TestStyled {
|
||||
val testObject = TestScheme {
|
||||
list = emptyList()
|
||||
}
|
||||
assertEquals(emptyList(), testObject.list)
|
||||
@ -65,4 +65,16 @@ class SpecificationTest {
|
||||
assertEquals(22, config["child.a"].int)
|
||||
assertEquals("test", config["child.b"].string)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testChildUpdate() {
|
||||
val config = Config()
|
||||
val child = config.getChild("child")
|
||||
val scheme = child.update(TestScheme) {
|
||||
a = 22
|
||||
b = "test"
|
||||
}
|
||||
assertEquals(22, config["child.a"].int)
|
||||
assertEquals("test", config["child.b"].string)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user