From 4cafac1f1b94937c6b7e6b578b1e8a7740517da8 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sun, 27 Jan 2019 17:16:40 +0300 Subject: [PATCH] Code cleanup --- README.md | 0 dataforge-context/build.gradle.kts | 2 +- .../kotlin/hep/dataforge/context/Context.kt | 4 +- .../hep/dataforge/context/PluginManager.kt | 3 +- .../hep/dataforge/context/PluginRepository.kt | 3 +- .../kotlin/hep/dataforge/context/PluginTag.kt | 6 +- .../kotlin/hep/dataforge/context/JSContext.kt | 12 +-- .../hep/dataforge/context/PluginRepository.kt | 2 +- .../hep/dataforge/context/JVMContext.kt | 13 ++-- .../hep/dataforge/context/PluginRepository.kt | 2 +- .../kotlin/hep/dataforge/data/Action.kt | 9 ++- .../kotlin/hep/dataforge/data/Goal.kt | 24 +++--- .../kotlin/hep/dataforge/io/OutputManager.kt | 6 +- .../hep/dataforge/meta/io/BinaryMetaFormat.kt | 2 +- .../hep/dataforge/meta/io/JsonMetaFormat.kt | 2 +- .../hep/dataforge/meta/io/MetaFormat.kt | 6 +- .../hep/dataforge/meta/io/MetaFormatTest.kt | 10 +-- .../kotlin/hep/dataforge/meta/Delegates.kt | 73 +++++++++++++------ .../kotlin/hep/dataforge/meta/Laminate.kt | 6 +- .../kotlin/hep/dataforge/meta/Meta.kt | 10 +-- .../hep/dataforge/meta/MutableMetaNode.kt | 23 ++++-- .../hep/dataforge/meta/Specification.kt | 21 ++++-- .../kotlin/hep/dataforge/meta/Styleable.kt | 2 +- .../hep/dataforge/meta/MetaBuilderTest.kt | 6 +- .../kotlin/hep/dataforge/names/NameTest.kt | 8 +- settings.gradle.kts | 10 +-- 26 files changed, 159 insertions(+), 106 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 00000000..e69de29b diff --git a/dataforge-context/build.gradle.kts b/dataforge-context/build.gradle.kts index 5b33659e..58da79a7 100644 --- a/dataforge-context/build.gradle.kts +++ b/dataforge-context/build.gradle.kts @@ -7,7 +7,7 @@ description = "Context and provider definitions" val coroutinesVersion: String by rootProject.extra kotlin { - jvm () + jvm() js() sourceSets { diff --git a/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/Context.kt b/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/Context.kt index 53cbd440..51f632ae 100644 --- a/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/Context.kt +++ b/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/Context.kt @@ -11,7 +11,6 @@ import kotlinx.coroutines.Dispatchers import mu.KLogger import mu.KotlinLogging import kotlin.coroutines.CoroutineContext -import kotlin.reflect.KClass /** * The local environment for anything being done in DataForge framework. Contexts are organized into tree structure with [Global] at the top. @@ -102,12 +101,11 @@ inline fun Context.list(target: String): Sequence { /** * A global root context */ -expect object Global : Context{ +expect object Global : Context { fun getContext(name: String): Context } - /** * The interface for something that encapsulated in context * 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 25acfc7a..73b42bd4 100644 --- a/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/PluginManager.kt +++ b/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/PluginManager.kt @@ -59,7 +59,8 @@ class PluginManager(override val context: Context) : ContextAware, Iterable get(type: KClass, recursive: Boolean = true): T? = get(recursive) { type.isInstance(it) } as T? + operator fun get(type: KClass, recursive: Boolean = true): T? = + get(recursive) { type.isInstance(it) } as T? inline fun get(recursive: Boolean = true): T? = get(T::class, recursive) diff --git a/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/PluginRepository.kt b/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/PluginRepository.kt index eca2bf05..60cdc3f0 100644 --- a/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/PluginRepository.kt +++ b/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/PluginRepository.kt @@ -24,5 +24,6 @@ expect object PluginRepository { /** * Fetch specific plugin and instantiate it with given meta */ -fun PluginRepository.fetch(tag: PluginTag, meta: Meta): Plugin = PluginRepository.list().find { it.tag.matches(tag) }?.build(meta) +fun PluginRepository.fetch(tag: PluginTag, meta: Meta): Plugin = + PluginRepository.list().find { it.tag.matches(tag) }?.build(meta) ?: error("Plugin with tag $tag not found in the repository") \ No newline at end of file diff --git a/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/PluginTag.kt b/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/PluginTag.kt index 6196a8cb..a45b66ab 100644 --- a/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/PluginTag.kt +++ b/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/PluginTag.kt @@ -11,9 +11,9 @@ import hep.dataforge.meta.buildMeta * @author Alexander Nozik */ data class PluginTag( - val name: String, - val group: String = "", - val version: String = "" + val name: String, + val group: String = "", + val version: String = "" ) : MetaRepr { /** diff --git a/dataforge-context/src/jsMain/kotlin/hep/dataforge/context/JSContext.kt b/dataforge-context/src/jsMain/kotlin/hep/dataforge/context/JSContext.kt index 95ae8aea..908f4f16 100644 --- a/dataforge-context/src/jsMain/kotlin/hep/dataforge/context/JSContext.kt +++ b/dataforge-context/src/jsMain/kotlin/hep/dataforge/context/JSContext.kt @@ -5,14 +5,14 @@ import mu.KLogger import mu.KotlinLogging import kotlin.jvm.Synchronized -actual object Global: Context, JSContext("GLOBAL", null){ +actual object Global : Context, JSContext("GLOBAL", null) { /** * Closing all contexts * * @throws Exception */ override fun close() { - logger.info{"Shutting down GLOBAL"} + logger.info { "Shutting down GLOBAL" } for (ctx in contextRegistry.values) { ctx.close() } @@ -34,10 +34,10 @@ actual object Global: Context, JSContext("GLOBAL", null){ } open class JSContext( - final override val name: String, - final override val parent: JSContext? = Global, - properties: Meta = EmptyMeta -): Context { + final override val name: String, + final override val parent: JSContext? = Global, + properties: Meta = EmptyMeta +) : Context { private val _properties = Config().apply { update(properties) } override val properties: Meta diff --git a/dataforge-context/src/jsMain/kotlin/hep/dataforge/context/PluginRepository.kt b/dataforge-context/src/jsMain/kotlin/hep/dataforge/context/PluginRepository.kt index f94928f5..830cd233 100644 --- a/dataforge-context/src/jsMain/kotlin/hep/dataforge/context/PluginRepository.kt +++ b/dataforge-context/src/jsMain/kotlin/hep/dataforge/context/PluginRepository.kt @@ -24,7 +24,7 @@ actual object PluginRepository { } inline fun register(tag: PluginTag, noinline constructor: (Meta) -> T) = - register(tag, T::class, constructor) + register(tag, T::class, constructor) /** * List plugins available in the repository diff --git a/dataforge-context/src/jvmMain/kotlin/hep/dataforge/context/JVMContext.kt b/dataforge-context/src/jvmMain/kotlin/hep/dataforge/context/JVMContext.kt index 96371db8..223c7d67 100644 --- a/dataforge-context/src/jvmMain/kotlin/hep/dataforge/context/JVMContext.kt +++ b/dataforge-context/src/jvmMain/kotlin/hep/dataforge/context/JVMContext.kt @@ -16,7 +16,6 @@ package hep.dataforge.context import hep.dataforge.meta.* -import kotlinx.coroutines.Dispatchers import mu.KLogger import mu.KotlinLogging import java.lang.ref.WeakReference @@ -24,15 +23,14 @@ import java.util.* import java.util.concurrent.ExecutorService import java.util.concurrent.Executors import kotlin.collections.HashSet -import kotlin.coroutines.CoroutineContext import kotlin.reflect.KClass import kotlin.reflect.full.cast open class JVMContext( - final override val name: String, - final override val parent: JVMContext? = Global, - classLoader: ClassLoader? = null, - properties: Meta = EmptyMeta + final override val name: String, + final override val parent: JVMContext? = Global, + classLoader: ClassLoader? = null, + properties: Meta = EmptyMeta ) : Context, AutoCloseable { private val _properties = Config().apply { update(properties) } @@ -75,7 +73,8 @@ open class JVMContext( private val serviceCache: MutableMap, ServiceLoader<*>> = HashMap() fun services(type: KClass): Sequence { - return serviceCache.getOrPut(type.java) { ServiceLoader.load(type.java, classLoader) }.asSequence().map { type.cast(it) } + return serviceCache.getOrPut(type.java) { ServiceLoader.load(type.java, classLoader) }.asSequence() + .map { type.cast(it) } } /** diff --git a/dataforge-context/src/jvmMain/kotlin/hep/dataforge/context/PluginRepository.kt b/dataforge-context/src/jvmMain/kotlin/hep/dataforge/context/PluginRepository.kt index 34e8ba77..e6008c34 100644 --- a/dataforge-context/src/jvmMain/kotlin/hep/dataforge/context/PluginRepository.kt +++ b/dataforge-context/src/jvmMain/kotlin/hep/dataforge/context/PluginRepository.kt @@ -12,6 +12,6 @@ actual object PluginRepository { * List plugins available in the repository */ actual fun list(): Sequence = - factories.asSequence() + Global.services(PluginFactory::class) + factories.asSequence() + Global.services(PluginFactory::class) } \ No newline at end of file diff --git a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/Action.kt b/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/Action.kt index 895cc764..2a8e2fb6 100644 --- a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/Action.kt +++ b/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/Action.kt @@ -50,10 +50,11 @@ class PipeAction(val transform: (Name, Data, Meta) - /** * A simple pipe that performs transformation on the data and copies input meta into the output */ - inline fun simple(noinline transform: suspend (Name, T, Meta) -> R) = PipeAction { name, data: Data, meta -> - val goal = data.goal.pipe { transform(name, it, meta) } - return@PipeAction Data.of(goal, data.meta) - } + inline fun simple(noinline transform: suspend (Name, T, Meta) -> R) = + PipeAction { name, data: Data, meta -> + val goal = data.goal.pipe { transform(name, it, meta) } + return@PipeAction Data.of(goal, data.meta) + } } } diff --git a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/Goal.kt b/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/Goal.kt index 707e2074..104a9037 100644 --- a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/Goal.kt +++ b/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/Goal.kt @@ -20,7 +20,8 @@ interface Goal : Deferred, CoroutineScope { /** * Create goal wrapping static value. This goal is always completed */ - fun static(context: CoroutineContext, value: T): Goal = StaticGoalImpl(context, CompletableDeferred(value)) + fun static(context: CoroutineContext, value: T): Goal = + StaticGoalImpl(context, CompletableDeferred(value)) } } @@ -48,16 +49,18 @@ class GoalMonitor { } private class GoalImpl( - override val dependencies: Collection>, - val monitor: GoalMonitor, - deferred: Deferred) : Goal, Deferred by deferred { + override val dependencies: Collection>, + val monitor: GoalMonitor, + deferred: Deferred +) : Goal, Deferred by deferred { override val coroutineContext: CoroutineContext get() = this override val totalWork: Double get() = dependencies.sumByDouble { totalWork } + monitor.totalWork override val workDone: Double get() = dependencies.sumByDouble { workDone } + monitor.workDone override val status: String get() = monitor.status } -private class StaticGoalImpl(val context: CoroutineContext, deferred: CompletableDeferred) : Goal, Deferred by deferred { +private class StaticGoalImpl(val context: CoroutineContext, deferred: CompletableDeferred) : Goal, + Deferred by deferred { override val dependencies: Collection> get() = emptyList() override val status: String get() = "" override val totalWork: Double get() = 0.0 @@ -94,7 +97,10 @@ fun Goal.pipe(block: suspend GoalMonitor.(T) -> R): Goal = createGo * Create a joining goal. * @param scope the scope for resulting goal. By default use first goal in list */ -fun Collection>.join(scope: CoroutineScope = first(), block: suspend GoalMonitor.(Collection) -> R): Goal = - scope.createGoal(this) { - block(map { it.await() }) - } \ No newline at end of file +fun Collection>.join( + scope: CoroutineScope = first(), + block: suspend GoalMonitor.(Collection) -> R +): Goal = + scope.createGoal(this) { + block(map { it.await() }) + } \ No newline at end of file diff --git a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/OutputManager.kt b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/OutputManager.kt index 7a8c1eee..3b454391 100644 --- a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/OutputManager.kt +++ b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/OutputManager.kt @@ -31,6 +31,10 @@ interface OutputManager : Plugin { /** * Get an output with given [name], [stage] and reified content type */ -inline fun OutputManager.typed(name: Name, stage: Name = EmptyName, meta: Meta = EmptyMeta): Output { +inline fun OutputManager.typed( + name: Name, + stage: Name = EmptyName, + meta: Meta = EmptyMeta +): Output { return typed(T::class, name, stage, meta) } diff --git a/dataforge-meta-io/src/commonMain/kotlin/hep/dataforge/meta/io/BinaryMetaFormat.kt b/dataforge-meta-io/src/commonMain/kotlin/hep/dataforge/meta/io/BinaryMetaFormat.kt index c2148ba4..176435da 100644 --- a/dataforge-meta-io/src/commonMain/kotlin/hep/dataforge/meta/io/BinaryMetaFormat.kt +++ b/dataforge-meta-io/src/commonMain/kotlin/hep/dataforge/meta/io/BinaryMetaFormat.kt @@ -124,7 +124,7 @@ object BinaryMetaFormat : MetaFormat { } } -class BinaryMetaFormatFactory: MetaFormatFactory{ +class BinaryMetaFormatFactory : MetaFormatFactory { override val name: String = "bin" override val key: Short = 0x4249//BI diff --git a/dataforge-meta-io/src/commonMain/kotlin/hep/dataforge/meta/io/JsonMetaFormat.kt b/dataforge-meta-io/src/commonMain/kotlin/hep/dataforge/meta/io/JsonMetaFormat.kt index cde54c25..c9c67201 100644 --- a/dataforge-meta-io/src/commonMain/kotlin/hep/dataforge/meta/io/JsonMetaFormat.kt +++ b/dataforge-meta-io/src/commonMain/kotlin/hep/dataforge/meta/io/JsonMetaFormat.kt @@ -74,7 +74,7 @@ class JsonMeta(val json: JsonObject) : Meta { } } -class JsonMetaFormatFactory: MetaFormatFactory{ +class JsonMetaFormatFactory : MetaFormatFactory { override val name: String = "json" override val key: Short = 0x4a53//"JS" diff --git a/dataforge-meta-io/src/commonMain/kotlin/hep/dataforge/meta/io/MetaFormat.kt b/dataforge-meta-io/src/commonMain/kotlin/hep/dataforge/meta/io/MetaFormat.kt index 2f619adf..2d7de871 100644 --- a/dataforge-meta-io/src/commonMain/kotlin/hep/dataforge/meta/io/MetaFormat.kt +++ b/dataforge-meta-io/src/commonMain/kotlin/hep/dataforge/meta/io/MetaFormat.kt @@ -21,13 +21,13 @@ interface MetaFormatFactory { fun build(): MetaFormat } -fun Meta.asString(format: MetaFormat = JsonMetaFormat): String{ +fun Meta.asString(format: MetaFormat = JsonMetaFormat): String { val builder = BytePacketBuilder() - format.write(this,builder) + format.write(this, builder) return builder.build().readText() } -fun MetaFormat.parse(str: String): Meta{ +fun MetaFormat.parse(str: String): Meta { return read(ByteReadPacket(str.toByteArray())) } diff --git a/dataforge-meta-io/src/commonTest/kotlin/hep/dataforge/meta/io/MetaFormatTest.kt b/dataforge-meta-io/src/commonTest/kotlin/hep/dataforge/meta/io/MetaFormatTest.kt index 6655a541..78966dc8 100644 --- a/dataforge-meta-io/src/commonTest/kotlin/hep/dataforge/meta/io/MetaFormatTest.kt +++ b/dataforge-meta-io/src/commonTest/kotlin/hep/dataforge/meta/io/MetaFormatTest.kt @@ -4,9 +4,9 @@ import hep.dataforge.meta.buildMeta import kotlin.test.Test import kotlin.test.assertEquals -class MetaFormatTest{ +class MetaFormatTest { @Test - fun testBinaryMetaFormat(){ + fun testBinaryMetaFormat() { val meta = buildMeta { "a" to 22 "node" to { @@ -14,13 +14,13 @@ class MetaFormatTest{ "c" to 11.1 } } - val string = meta.asString(BinaryMetaFormat) + val string = meta.asString(BinaryMetaFormat) val result = BinaryMetaFormat.parse(string) assertEquals(meta, result) } @Test - fun testJsonMetaFormat(){ + fun testJsonMetaFormat() { val meta = buildMeta { "a" to 22 "node" to { @@ -30,7 +30,7 @@ class MetaFormatTest{ } val string = meta.asString(JsonMetaFormat) val result = JsonMetaFormat.parse(string) - assertEquals(meta,result) + assertEquals(meta, result) } } \ No newline at end of file diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Delegates.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Delegates.kt index f63a271b..fadfc673 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Delegates.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Delegates.kt @@ -11,25 +11,29 @@ import kotlin.reflect.KProperty //TODO add caching for sealed nodes -class ValueDelegate(private val key: String? = null, private val default: Value? = null) : ReadOnlyProperty { +class ValueDelegate(private val key: String? = null, private val default: Value? = null) : + ReadOnlyProperty { override fun getValue(thisRef: Metoid, property: KProperty<*>): Value? { return thisRef.meta[key ?: property.name]?.value ?: default } } -class StringDelegate(private val key: String? = null, private val default: String? = null) : ReadOnlyProperty { +class StringDelegate(private val key: String? = null, private val default: String? = null) : + ReadOnlyProperty { override fun getValue(thisRef: Metoid, property: KProperty<*>): String? { return thisRef.meta[key ?: property.name]?.string ?: default } } -class BooleanDelegate(private val key: String? = null, private val default: Boolean? = null) : ReadOnlyProperty { +class BooleanDelegate(private val key: String? = null, private val default: Boolean? = null) : + ReadOnlyProperty { override fun getValue(thisRef: Metoid, property: KProperty<*>): Boolean? { return thisRef.meta[key ?: property.name]?.boolean ?: default } } -class NumberDelegate(private val key: String? = null, private val default: Number? = null) : ReadOnlyProperty { +class NumberDelegate(private val key: String? = null, private val default: Number? = null) : + ReadOnlyProperty { override fun getValue(thisRef: Metoid, property: KProperty<*>): Number? { return thisRef.meta[key ?: property.name]?.number ?: default } @@ -37,25 +41,32 @@ class NumberDelegate(private val key: String? = null, private val default: Numbe //Delegates with non-null values -class SafeStringDelegate(private val key: String? = null, private val default: String) : ReadOnlyProperty { +class SafeStringDelegate(private val key: String? = null, private val default: String) : + ReadOnlyProperty { override fun getValue(thisRef: Metoid, property: KProperty<*>): String { return thisRef.meta[key ?: property.name]?.string ?: default } } -class SafeBooleanDelegate(private val key: String? = null, private val default: Boolean) : ReadOnlyProperty { +class SafeBooleanDelegate(private val key: String? = null, private val default: Boolean) : + ReadOnlyProperty { override fun getValue(thisRef: Metoid, property: KProperty<*>): Boolean { return thisRef.meta[key ?: property.name]?.boolean ?: default } } -class SafeNumberDelegate(private val key: String? = null, private val default: Number) : ReadOnlyProperty { +class SafeNumberDelegate(private val key: String? = null, private val default: Number) : + ReadOnlyProperty { override fun getValue(thisRef: Metoid, property: KProperty<*>): Number { return thisRef.meta[key ?: property.name]?.number ?: default } } -class SafeEnumDelegate>(private val key: String? = null, private val default: E, private val resolver: (String) -> E) : ReadOnlyProperty { +class SafeEnumDelegate>( + private val key: String? = null, + private val default: E, + private val resolver: (String) -> E +) : ReadOnlyProperty { override fun getValue(thisRef: Metoid, property: KProperty<*>): E { return (thisRef.meta[key ?: property.name]?.string)?.let { resolver(it) } ?: default } @@ -63,9 +74,10 @@ class SafeEnumDelegate>(private val key: String? = null, private val //Child node delegate -class ChildDelegate(private val key: String? = null, private val converter: (Meta) -> T) : ReadOnlyProperty { +class ChildDelegate(private val key: String? = null, private val converter: (Meta) -> T) : + ReadOnlyProperty { override fun getValue(thisRef: Metoid, property: KProperty<*>): T? { - return thisRef.meta[key ?: property.name]?.node?.let { converter(it)} + return thisRef.meta[key ?: property.name]?.node?.let { converter(it) } } } @@ -95,18 +107,20 @@ fun Metoid.boolean(default: Boolean, key: String? = null) = SafeBooleanDelegate( @JvmName("safeNumber") fun Metoid.number(default: Number, key: String? = null) = SafeNumberDelegate(key, default) -inline fun > Metoid.enum(default: E, key: String? = null) = SafeEnumDelegate(key, default) { enumValueOf(it) } +inline fun > Metoid.enum(default: E, key: String? = null) = + SafeEnumDelegate(key, default) { enumValueOf(it) } /* Config delegates */ -class ValueConfigDelegate(private val key: String? = null, private val default: Value? = null) : ReadWriteProperty { +class ValueConfigDelegate(private val key: String? = null, private val default: Value? = null) : + ReadWriteProperty { override fun getValue(thisRef: Configurable, property: KProperty<*>): Value? { return thisRef.config[key ?: property.name]?.value ?: default } override fun setValue(thisRef: Configurable, property: KProperty<*>, value: Value?) { val name = key ?: property.name - if(value == null){ + if (value == null) { thisRef.config.remove(name) } else { thisRef.config[name] = value @@ -114,7 +128,8 @@ class ValueConfigDelegate(private val key: String? = null, private val default: } } -class StringConfigDelegate(private val key: String? = null, private val default: String? = null) : ReadWriteProperty { +class StringConfigDelegate(private val key: String? = null, private val default: String? = null) : + ReadWriteProperty { override fun getValue(thisRef: Configurable, property: KProperty<*>): String? { return thisRef.config[key ?: property.name]?.string ?: default } @@ -124,7 +139,8 @@ class StringConfigDelegate(private val key: String? = null, private val default: } } -class BooleanConfigDelegate(private val key: String? = null, private val default: Boolean? = null) : ReadWriteProperty { +class BooleanConfigDelegate(private val key: String? = null, private val default: Boolean? = null) : + ReadWriteProperty { override fun getValue(thisRef: Configurable, property: KProperty<*>): Boolean? { return thisRef.config[key ?: property.name]?.boolean ?: default } @@ -134,7 +150,8 @@ class BooleanConfigDelegate(private val key: String? = null, private val default } } -class NumberConfigDelegate(private val key: String? = null, private val default: Number? = null) : ReadWriteProperty { +class NumberConfigDelegate(private val key: String? = null, private val default: Number? = null) : + ReadWriteProperty { override fun getValue(thisRef: Configurable, property: KProperty<*>): Number? { return thisRef.config[key ?: property.name]?.number ?: default } @@ -146,7 +163,8 @@ class NumberConfigDelegate(private val key: String? = null, private val default: //Delegates with non-null values -class SafeStringConfigDelegate(private val key: String? = null, private val default: String) : ReadWriteProperty { +class SafeStringConfigDelegate(private val key: String? = null, private val default: String) : + ReadWriteProperty { override fun getValue(thisRef: Configurable, property: KProperty<*>): String { return thisRef.config[key ?: property.name]?.string ?: default } @@ -156,7 +174,8 @@ class SafeStringConfigDelegate(private val key: String? = null, private val defa } } -class SafeBooleanConfigDelegate(private val key: String? = null, private val default: Boolean) : ReadWriteProperty { +class SafeBooleanConfigDelegate(private val key: String? = null, private val default: Boolean) : + ReadWriteProperty { override fun getValue(thisRef: Configurable, property: KProperty<*>): Boolean { return thisRef.config[key ?: property.name]?.boolean ?: default } @@ -166,7 +185,8 @@ class SafeBooleanConfigDelegate(private val key: String? = null, private val def } } -class SafeNumberConfigDelegate(private val key: String? = null, private val default: Number) : ReadWriteProperty { +class SafeNumberConfigDelegate(private val key: String? = null, private val default: Number) : + ReadWriteProperty { override fun getValue(thisRef: Configurable, property: KProperty<*>): Number { return thisRef.config[key ?: property.name]?.number ?: default } @@ -176,7 +196,11 @@ class SafeNumberConfigDelegate(private val key: String? = null, private val defa } } -class SafeEnumvConfigDelegate>(private val key: String? = null, private val default: E, private val resolver: (String) -> E) : ReadWriteProperty { +class SafeEnumvConfigDelegate>( + private val key: String? = null, + private val default: E, + private val resolver: (String) -> E +) : ReadWriteProperty { override fun getValue(thisRef: Configurable, property: KProperty<*>): E { return (thisRef.config[key ?: property.name]?.string)?.let { resolver(it) } ?: default } @@ -188,7 +212,8 @@ class SafeEnumvConfigDelegate>(private val key: String? = null, priv //Child node delegate -class ChildConfigDelegate(private val key: String? = null, private val converter: (Config) -> T) : ReadWriteProperty { +class ChildConfigDelegate(private val key: String? = null, private val converter: (Config) -> T) : + ReadWriteProperty { override fun getValue(thisRef: Configurable, property: KProperty<*>): T { return converter(thisRef.config[key ?: property.name]?.node ?: Config()) } @@ -214,7 +239,8 @@ fun Configurable.number(default: Number? = null, key: String? = null) = NumberCo fun Configurable.child(key: String? = null) = ChildConfigDelegate(key) { SimpleConfigurable(it) } -fun Configurable.child(key: String? = null, converter: (Config) -> T) = ChildConfigDelegate(key, converter) +fun Configurable.child(key: String? = null, converter: (Config) -> T) = + ChildConfigDelegate(key, converter) //fun Configurable.spec(spec: Specification, key: String? = null) = ChildConfigDelegate(key) { spec.wrap(this) } @@ -227,4 +253,5 @@ fun Configurable.boolean(default: Boolean, key: String? = null) = SafeBooleanCon @JvmName("safeNumber") fun Configurable.number(default: Number, key: String? = null) = SafeNumberConfigDelegate(key, default) -inline fun > Configurable.enum(default: E, key: String? = null) = SafeEnumvConfigDelegate(key, default) { enumValueOf(it) } \ No newline at end of file +inline fun > Configurable.enum(default: E, key: String? = null) = + SafeEnumvConfigDelegate(key, default) { enumValueOf(it) } \ No newline at end of file diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Laminate.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Laminate.kt index 5d8f13c5..094380c8 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Laminate.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Laminate.kt @@ -10,14 +10,14 @@ import hep.dataforge.names.NameToken class Laminate(layers: List) : Meta { val layers: List = layers.flatMap { - if(it is Laminate){ + if (it is Laminate) { it.layers - } else{ + } else { listOf(it) } } - constructor(vararg layers: Meta): this(layers.asList()) + constructor(vararg layers: Meta) : this(layers.asList()) override val items: Map> get() = layers.map { it.items.keys }.flatten().associateWith { key -> diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Meta.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Meta.kt index 12dc3e2f..56590dbd 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Meta.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Meta.kt @@ -77,9 +77,9 @@ fun Meta.getAll(name: Name): Map> { val (body, query) = name.last()!! val regex = query.toRegex() return (this[name.cutLast()] as? NodeItem<*>)?.node?.items - ?.filter { it.key.body == body && (query.isEmpty() || regex.matches(it.key.query)) } - ?.mapKeys { it.key.query } - ?: emptyMap() + ?.filter { it.key.body == body && (query.isEmpty() || regex.matches(it.key.query)) } + ?.mapKeys { it.key.query } + ?: emptyMap() } @@ -155,8 +155,8 @@ object EmptyMeta : Meta { val MetaItem<*>.value get() = (this as? MetaItem.ValueItem)?.value - ?: (this.node[VALUE_KEY] as? MetaItem.ValueItem)?.value - ?: error("Trying to interpret node meta item as value item") + ?: (this.node[VALUE_KEY] as? MetaItem.ValueItem)?.value + ?: error("Trying to interpret node meta item as value item") val MetaItem<*>.string get() = value.string val MetaItem<*>.boolean get() = value.boolean val MetaItem<*>.number get() = value.number diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/MutableMetaNode.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/MutableMetaNode.kt index 48729efb..fe48a299 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/MutableMetaNode.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/MutableMetaNode.kt @@ -6,7 +6,10 @@ import hep.dataforge.names.plus import hep.dataforge.names.toName import hep.dataforge.values.Value -class MetaListener(val owner: Any? = null, val action: (name: Name, oldItem: MetaItem<*>?, newItem: MetaItem<*>?) -> Unit) { +class MetaListener( + val owner: Any? = null, + val action: (name: Name, oldItem: MetaItem<*>?, newItem: MetaItem<*>?) -> Unit +) { operator fun invoke(name: Name, oldItem: MetaItem<*>?, newItem: MetaItem<*>?) = action(name, oldItem, newItem) } @@ -87,7 +90,7 @@ abstract class MutableMetaNode> : MetaNode(), MutableM val token = name.first()!! //get existing or create new node. Query is ignored for new node val child = this.items[token]?.node - ?: empty().also { this[token.body.toName()] = MetaItem.NodeItem(it) } + ?: empty().also { this[token.body.toName()] = MetaItem.NodeItem(it) } child[name.cutFirst()] = item } } @@ -130,16 +133,20 @@ fun > M.update(meta: Meta) { meta.items.forEach { entry -> val value = entry.value when (value) { - is MetaItem.ValueItem -> setValue(entry.key.toName(),value.value) + is MetaItem.ValueItem -> setValue(entry.key.toName(), value.value) is MetaItem.NodeItem -> (this[entry.key.toName()] as? MetaItem.NodeItem)?.node?.update(value.node) - ?: run { setNode(entry.key.toName(),value.node)} + ?: run { setNode(entry.key.toName(), value.node) } } } } /* Same name siblings generation */ -fun > M.setIndexed(name: Name, items: Iterable>, queryFactory: (Int) -> String = { it.toString() }) { +fun > M.setIndexed( + name: Name, + items: Iterable>, + queryFactory: (Int) -> String = { it.toString() } +) { val tokens = name.tokens.toMutableList() val last = tokens.last() items.forEachIndexed { index, meta -> @@ -149,7 +156,11 @@ fun > M.setIndexed(name: Name, items: Iterable>, } } -fun > M.setIndexed(name: Name, metas: Iterable, queryFactory: (Int) -> String = { it.toString() }) { +fun > M.setIndexed( + name: Name, + metas: Iterable, + queryFactory: (Int) -> String = { it.toString() } +) { setIndexed(name, metas.map { wrap(name, it) }, queryFactory) } diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Specification.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Specification.kt index 132fba06..bdd11429 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Specification.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Specification.kt @@ -3,7 +3,7 @@ package hep.dataforge.meta /** * Marker interface for specifications */ -interface Specification: Configurable{ +interface Specification : Configurable { operator fun get(name: String): MetaItem? = config[name] } @@ -26,24 +26,29 @@ interface SpecificationBuilder { fun wrap(meta: Meta): T = wrap(meta.toConfig()) } -fun specification(wrapper: (Config) -> T): SpecificationBuilder = object : SpecificationBuilder { - override fun wrap(config: Config): T = wrapper(config) -} +fun specification(wrapper: (Config) -> T): SpecificationBuilder = + object : SpecificationBuilder { + override fun wrap(config: Config): T = wrapper(config) + } /** * Apply specified configuration to configurable */ -fun > T.configure(spec: S, action: C.() -> Unit) = apply { spec.update(config, action) } +fun > T.configure(spec: S, action: C.() -> Unit) = + apply { spec.update(config, action) } /** * Update configuration using given specification */ -fun > Specification.update(spec: S, action: C.() -> Unit) = apply { spec.update(config, action) } +fun > Specification.update(spec: S, action: C.() -> Unit) = + apply { spec.update(config, action) } /** * Create a style based on given specification */ -fun > S.createStyle(action: C.() -> Unit): Meta = Config().also { update(it, action) } +fun > S.createStyle(action: C.() -> Unit): Meta = + Config().also { update(it, action) } -fun Specification.spec(spec: SpecificationBuilder, key: String? = null) = ChildConfigDelegate(key) { spec.wrap(config) } \ No newline at end of file +fun Specification.spec(spec: SpecificationBuilder, key: String? = null) = + ChildConfigDelegate(key) { spec.wrap(config) } \ No newline at end of file diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Styleable.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Styleable.kt index 5b4af6d2..d8aaf24e 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Styleable.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Styleable.kt @@ -44,7 +44,7 @@ class StyledConfig(val config: Config, style: Meta = EmptyMeta) : Config() { } is MetaItem.ValueItem -> MetaItem.ValueItem(value.value) is MetaItem.NodeItem -> MetaItem.NodeItem( - StyledConfig(value.node, styleValue?.node ?: EmptyMeta) + StyledConfig(value.node, styleValue?.node ?: EmptyMeta) ) } key to item diff --git a/dataforge-meta/src/commonTest/kotlin/hep/dataforge/meta/MetaBuilderTest.kt b/dataforge-meta/src/commonTest/kotlin/hep/dataforge/meta/MetaBuilderTest.kt index 8cda9003..3f34882c 100644 --- a/dataforge-meta/src/commonTest/kotlin/hep/dataforge/meta/MetaBuilderTest.kt +++ b/dataforge-meta/src/commonTest/kotlin/hep/dataforge/meta/MetaBuilderTest.kt @@ -5,12 +5,12 @@ import kotlin.test.Test import kotlin.test.assertEquals -class MetaBuilderTest{ +class MetaBuilderTest { @Test - fun testBuilder(){ + fun testBuilder() { val meta = buildMeta { "a" to 22 - "b" to listOf(1,2,3) + "b" to listOf(1, 2, 3) this["c"] = "myValue".asValue() "node" to { "e" to 12.2 diff --git a/dataforge-meta/src/commonTest/kotlin/hep/dataforge/names/NameTest.kt b/dataforge-meta/src/commonTest/kotlin/hep/dataforge/names/NameTest.kt index 52fb478d..1302777e 100644 --- a/dataforge-meta/src/commonTest/kotlin/hep/dataforge/names/NameTest.kt +++ b/dataforge-meta/src/commonTest/kotlin/hep/dataforge/names/NameTest.kt @@ -3,17 +3,17 @@ package hep.dataforge.names import kotlin.test.Test import kotlin.test.assertEquals -class NameTest{ +class NameTest { @Test - fun simpleName(){ + fun simpleName() { val name = "token1.token2.token3".toName() assertEquals("token2", name[1].toString()) } @Test - fun equalityTest(){ + fun equalityTest() { val name1 = "token1.token2[2].token3".toName() val name2 = "token1".toName() + "token2[2].token3" - assertEquals(name1,name2) + assertEquals(name1, name2) } } \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index 774ee96a..216dd001 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -9,9 +9,9 @@ pluginManagement { rootProject.name = "dataforge-core" include( - ":dataforge-meta", - ":dataforge-meta-io", - ":dataforge-context", - ":dataforge-data", - ":dataforge-io" + ":dataforge-meta", + ":dataforge-meta-io", + ":dataforge-context", + ":dataforge-data", + ":dataforge-io" )