From 93c806c3bfe5a9d7a9a43d6c8af92502161f4045 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Mon, 2 Mar 2020 19:17:36 +0300 Subject: [PATCH] Basic design for external connectors --- .../kotlin/hep/dataforge/context/PluginManager.kt | 2 +- .../kotlin/hep/dataforge/meta/MetaBuilder.kt | 4 ++++ .../kotlin/hep/dataforge/meta/MutableMetaDelegate.kt | 12 +++++++++++- .../kotlin/hep/dataforge/meta/scheme/Scheme.kt | 3 +-- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/PluginManager.kt b/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/PluginManager.kt index dd114f79..afac0392 100644 --- a/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/PluginManager.kt +++ b/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/PluginManager.kt @@ -62,7 +62,7 @@ class PluginManager(override val context: Context) : ContextAware, Iterable get(type: KClass, tag: PluginTag? = null, recursive: Boolean = true): T? = + operator fun get(type: KClass, tag: PluginTag? = null, recursive: Boolean = true): T? = find(recursive) { type.isInstance(it) && (tag == null || tag.matches(it.tag)) } as T? inline operator fun get(tag: PluginTag? = null, recursive: Boolean = true): T? = diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/MetaBuilder.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/MetaBuilder.kt index 243d40bf..b36b3316 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/MetaBuilder.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/MetaBuilder.kt @@ -15,6 +15,10 @@ class MetaBuilder : AbstractMutableMeta() { override fun wrapNode(meta: Meta): MetaBuilder = if (meta is MetaBuilder) meta else meta.builder() override fun empty(): MetaBuilder = MetaBuilder() + infix fun String.put(item: MetaItem<*>?) { + set(this, item) + } + infix fun String.put(value: Value?) { set(this, value) } diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/MutableMetaDelegate.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/MutableMetaDelegate.kt index 5ab6ba0c..3fcdc9da 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/MutableMetaDelegate.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/MutableMetaDelegate.kt @@ -49,9 +49,19 @@ fun ReadWriteProperty.map(reader: (T) -> R, writer: (R) -> T): R fun ReadWriteProperty?>.transform(reader: (MetaItem<*>?) -> R): ReadWriteProperty = map(reader = reader, writer = { MetaItem.of(it) }) -fun ReadWriteProperty.transform(reader: (Value?) -> R) = +fun ReadWriteProperty.transform(reader: (Value?) -> R): ReadWriteDelegateWrapper = map(reader = reader, writer = { Value.of(it) }) +/** + * A delegate that throws + */ +fun ReadWriteProperty.notNull(default: () -> R): ReadWriteProperty { + return ReadWriteDelegateWrapper(this, + reader = { it ?: default() }, + writer = { it } + ) +} + fun > M.item(default: Any? = null, key: Name? = null): MutableMetaDelegate = MutableMetaDelegate(this, key, default?.let { MetaItem.of(it) }) diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/scheme/Scheme.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/scheme/Scheme.kt index 7ac49de0..78d0a511 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/scheme/Scheme.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/scheme/Scheme.kt @@ -59,8 +59,7 @@ open class Scheme() : Configurable, Described, MetaRepr { /** * A specification for simplified generation of wrappers */ -open class SchemeSpec(val builder: () -> T) : - Specification { +open class SchemeSpec(val builder: () -> T) : Specification { override fun wrap(config: Config, defaultProvider: (Name) -> MetaItem<*>?): T { return builder().apply { this.config = config