diff --git a/dataforge-io/build.gradle.kts b/dataforge-output/build.gradle.kts similarity index 100% rename from dataforge-io/build.gradle.kts rename to dataforge-output/build.gradle.kts diff --git a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/Output.kt b/dataforge-output/src/commonMain/kotlin/hep/dataforge/io/Output.kt similarity index 100% rename from dataforge-io/src/commonMain/kotlin/hep/dataforge/io/Output.kt rename to dataforge-output/src/commonMain/kotlin/hep/dataforge/io/Output.kt diff --git a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/OutputManager.kt b/dataforge-output/src/commonMain/kotlin/hep/dataforge/io/OutputManager.kt similarity index 58% rename from dataforge-io/src/commonMain/kotlin/hep/dataforge/io/OutputManager.kt rename to dataforge-output/src/commonMain/kotlin/hep/dataforge/io/OutputManager.kt index 3a96c458..f3ed4b53 100644 --- a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/OutputManager.kt +++ b/dataforge-output/src/commonMain/kotlin/hep/dataforge/io/OutputManager.kt @@ -1,6 +1,7 @@ package hep.dataforge.io import hep.dataforge.context.AbstractPlugin +import hep.dataforge.context.Context import hep.dataforge.context.Plugin import hep.dataforge.context.PluginTag import hep.dataforge.context.PluginTag.Companion.DATAFORGE_GROUP @@ -9,40 +10,45 @@ import hep.dataforge.meta.Meta import hep.dataforge.names.EmptyName import hep.dataforge.names.Name import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.Dispatchers import kotlin.reflect.KClass /** * A manager for outputs */ interface OutputManager : Plugin { + /** - * Provide an output for given name and stage. - * + * Get an output specialized for given type, name and stage. * @param stage represents the node or directory for the output. Empty means root node. * @param name represents the name inside the node. * @param meta configuration for [Output] (not for rendered object) - * */ - operator fun get(name: Name, stage: Name = EmptyName, meta: Meta = EmptyMeta): Output - - /** - * Get an output specialized for giver ntype - */ - fun typed(type: KClass, name: Name, stage: Name = EmptyName, meta: Meta = EmptyMeta): Output - + operator fun get(type: KClass, name: Name, stage: Name = EmptyName, meta: Meta = EmptyMeta): Output } +/** + * Get an output manager for a context + */ +val Context.output: OutputManager get() = plugins.get() ?: ConsoleOutputManager + /** * Get an output with given [name], [stage] and reified content type */ -inline fun OutputManager.typed( +inline operator fun OutputManager.get( name: Name, stage: Name = EmptyName, meta: Meta = EmptyMeta ): Output { - return typed(T::class, name, stage, meta) + return get(T::class, name, stage, meta) } +/** + * Directly render an object using the most suitable renderer + */ +fun OutputManager.render(obj: Any, name: Name, stage: Name = EmptyName, meta: Meta = EmptyMeta) = + get(obj::class, name,stage).render(obj,meta) + /** * System console output. * The [ConsoleOutput] is used when no other [OutputManager] is provided. @@ -52,12 +58,10 @@ expect val ConsoleOutput: Output object ConsoleOutputManager : AbstractPlugin(), OutputManager { override val tag: PluginTag = PluginTag("output.console", group = DATAFORGE_GROUP) - override fun get(name: Name, stage: Name, meta: Meta): Output = ConsoleOutput - - override fun typed(type: KClass, name: Name, stage: Name, meta: Meta): Output = ConsoleOutput + override fun get(type: KClass, name: Name, stage: Name, meta: Meta): Output = ConsoleOutput } /** * A dispatcher for output tasks. */ -expect val OutputDispatcher : CoroutineDispatcher \ No newline at end of file +expect val Dispatchers.Output: CoroutineDispatcher \ No newline at end of file diff --git a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/TextOutput.kt b/dataforge-output/src/commonMain/kotlin/hep/dataforge/io/TextOutput.kt similarity index 92% rename from dataforge-io/src/commonMain/kotlin/hep/dataforge/io/TextOutput.kt rename to dataforge-output/src/commonMain/kotlin/hep/dataforge/io/TextOutput.kt index 446eed31..1e8d976a 100644 --- a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/TextOutput.kt +++ b/dataforge-output/src/commonMain/kotlin/hep/dataforge/io/TextOutput.kt @@ -5,6 +5,7 @@ import hep.dataforge.io.TextRenderer.Companion.TEXT_RENDERER_TYPE import hep.dataforge.meta.Meta import hep.dataforge.provider.Type import hep.dataforge.provider.provideAll +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlin.reflect.KClass @@ -32,12 +33,15 @@ class TextOutput(override val context: Context, private val output: kotlinx.io.c value } } - context.launch(OutputDispatcher) { + context.launch(Dispatchers.Output) { renderer.run { output.render(obj) } } } } +/** + * A text or binary renderer based on [kotlinx.io.core.Output] + */ @Type(TEXT_RENDERER_TYPE) interface TextRenderer { /** diff --git a/dataforge-io/src/jsMain/kotlin/hep/dataforge/io/ConsoleOutput.kt b/dataforge-output/src/jsMain/kotlin/hep/dataforge/io/ConsoleOutput.kt similarity index 86% rename from dataforge-io/src/jsMain/kotlin/hep/dataforge/io/ConsoleOutput.kt rename to dataforge-output/src/jsMain/kotlin/hep/dataforge/io/ConsoleOutput.kt index 3aed2000..ccd9ad5a 100644 --- a/dataforge-io/src/jsMain/kotlin/hep/dataforge/io/ConsoleOutput.kt +++ b/dataforge-output/src/jsMain/kotlin/hep/dataforge/io/ConsoleOutput.kt @@ -19,4 +19,4 @@ actual val ConsoleOutput: Output = object : Output { } -actual val OutputDispatcher: CoroutineDispatcher = Dispatchers.Default \ No newline at end of file +actual val Dispatchers.Output: CoroutineDispatcher get() = Dispatchers.Default \ No newline at end of file diff --git a/dataforge-io/src/jvmMain/kotlin/hep/dataforge/io/ConsoleOutput.kt b/dataforge-output/src/jvmMain/kotlin/hep/dataforge/io/ConsoleOutput.kt similarity index 86% rename from dataforge-io/src/jvmMain/kotlin/hep/dataforge/io/ConsoleOutput.kt rename to dataforge-output/src/jvmMain/kotlin/hep/dataforge/io/ConsoleOutput.kt index b4f28887..ecb989a1 100644 --- a/dataforge-io/src/jvmMain/kotlin/hep/dataforge/io/ConsoleOutput.kt +++ b/dataforge-output/src/jvmMain/kotlin/hep/dataforge/io/ConsoleOutput.kt @@ -10,4 +10,4 @@ import kotlinx.io.streams.asOutput */ actual val ConsoleOutput: Output = TextOutput(Global, System.out.asOutput()) -actual val OutputDispatcher = Dispatchers.IO \ No newline at end of file +actual val Dispatchers.Output get() = Dispatchers.IO \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index c7ab404e..49dbc374 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -24,7 +24,7 @@ include( ":dataforge-meta-io", ":dataforge-context", ":dataforge-data", - ":dataforge-io", + ":dataforge-output", ":dataforge-workspace", ":dataforge-scripting" ) \ No newline at end of file