Replace branch data builder with putAll

This commit is contained in:
Alexander Nozik 2024-02-18 20:11:53 +03:00
parent aa4c745819
commit f95e278b2d
8 changed files with 39 additions and 24 deletions

View File

@ -6,6 +6,7 @@
- Name index comparator
### Changed
- DataSink `branch` is replaced with `putAll` to avoid confusion with DataTree methods
### Deprecated

View File

@ -98,15 +98,15 @@ public val DataTree<*>.meta: Meta? get() = data?.meta
/**
* Provide subtree if it exists
*/
public tailrec fun <T, TR : GenericDataTree<T, TR>> GenericDataTree<T, TR>.branch(name: Name): TR? =
public tailrec fun <T, TR : GenericDataTree<T, TR>> GenericDataTree<T, TR>.putAll(name: Name): TR? =
when (name.length) {
0 -> self
1 -> items[name.first()]
else -> items[name.first()]?.branch(name.cutFirst())
else -> items[name.first()]?.putAll(name.cutFirst())
}
public fun <T, TR : GenericDataTree<T, TR>> GenericDataTree<T, TR>.branch(name: String): TR? =
branch(name.parseAsName())
public fun <T, TR : GenericDataTree<T, TR>> GenericDataTree<T, TR>.putAll(name: String): TR? =
this@branch.putAll(name.parseAsName())
public fun GenericDataTree<*, *>.isEmpty(): Boolean = data == null && items.isEmpty()

View File

@ -16,11 +16,7 @@ public fun <T> DataSink<T>.put(value: NamedData<T>) {
put(value.name, value.data)
}
public fun <T> DataSink<T>.branch(dataTree: DataTree<T>) {
putAll(dataTree.asSequence())
}
public inline fun <T> DataSink<T>.branch(
public inline fun <T> DataSink<T>.putAll(
prefix: Name,
block: DataSink<T>.() -> Unit,
) {
@ -35,24 +31,42 @@ public inline fun <T> DataSink<T>.branch(
}
}
@Deprecated("Use putAll", ReplaceWith("putAll(prefix, block)"))
public inline fun <T> DataSink<T>.branch(
prefix: Name,
block: DataSink<T>.() -> Unit,
): Unit = putAll(prefix, block)
public inline fun <T> DataSink<T>.putAll(
prefix: String,
block: DataSink<T>.() -> Unit,
): Unit = putAll(prefix.asName(), block)
@Deprecated("Use putAll", ReplaceWith("putAll(prefix, block)"))
public inline fun <T> DataSink<T>.branch(
prefix: String,
block: DataSink<T>.() -> Unit,
): Unit = branch(prefix.asName(), block)
): Unit = putAll(prefix, block)
public fun <T> DataSink<T>.put(name: String, value: Data<T>) {
put(Name.parse(name), value)
}
public fun <T> DataSink<T>.branch(name: Name, set: DataTree<T>) {
branch(name) { putAll(set.asSequence()) }
public fun <T> DataSink<T>.putAll(name: Name, tree: DataTree<T>) {
putAll(name) { putAll(tree.asSequence()) }
}
public fun <T> DataSink<T>.branch(name: String, set: DataTree<T>) {
branch(Name.parse(name)) { putAll(set.asSequence()) }
@Deprecated("Use putAll", ReplaceWith("putAll(name, tree)"))
public fun <T> DataSink<T>.branch(name: Name, tree: DataTree<T>): Unit = putAll(name,tree)
public fun <T> DataSink<T>.putAll(name: String, tree: DataTree<T>) {
putAll(Name.parse(name)) { putAll(tree.asSequence()) }
}
@Deprecated("Use putAll", ReplaceWith("putAll(name, tree)"))
public fun <T> DataSink<T>.branch(name: String, tree: DataTree<T>): Unit = putAll(name,tree)
/**
* Produce lazy [Data] and emit it into the [MutableDataTree]
*/
@ -124,7 +138,7 @@ public fun <T : Any> DataSink<T>.watchBranch(
name: Name,
dataSet: ObservableDataTree<T>,
): Job {
branch(name, dataSet)
putAll(name, dataSet)
return dataSet.updates().onEach {
put(name + it.name, it.data)
}.launchIn(dataSet.updatesScope)

View File

@ -15,7 +15,7 @@ public infix fun <T : Any> String.put(data: Data<T>): Unit =
*/
context(DataSink<T>)
public infix fun <T : Any> String.put(dataSet: DataTree<T>): Unit =
branch(this, dataSet)
this.putAll(this, dataSet)
/**
* Build and append node
@ -23,5 +23,5 @@ public infix fun <T : Any> String.put(dataSet: DataTree<T>): Unit =
context(DataSink<T>)
public infix fun <T : Any> String.put(
block: DataSink<T>.() -> Unit,
): Unit = branch(Name.parse(this), block)
): Unit = this.putAll(Name.parse(this), block)

View File

@ -43,7 +43,7 @@ internal class DataTreeBuilderTest {
wrap("b", "b")
}
wrap("root", "root")
putAll(updateData)
this.putAll(updateData)
}
assertEquals("a", node["update.a"]?.await())

View File

@ -11,7 +11,7 @@ import space.kscience.dataforge.provider.Provider
import kotlin.coroutines.CoroutineContext
public fun interface DataSelector<T> {
public fun interface DataSelector<out T> {
public suspend fun select(workspace: Workspace, meta: Meta): DataTree<T>
}

View File

@ -3,8 +3,8 @@ package space.kscience.dataforge.workspace
import space.kscience.dataforge.actions.Action
import space.kscience.dataforge.context.PluginFactory
import space.kscience.dataforge.data.DataTree
import space.kscience.dataforge.data.branch
import space.kscience.dataforge.data.forEach
import space.kscience.dataforge.data.putAll
import space.kscience.dataforge.data.transform
import space.kscience.dataforge.meta.*
import space.kscience.dataforge.misc.DFExperimental
@ -101,7 +101,7 @@ public suspend inline fun <T, reified R> TaskResultBuilder<R>.transformEach(
* Set given [dataSet] as a task result.
*/
public fun <T> TaskResultBuilder<T>.result(dataSet: DataTree<T>) {
branch(dataSet)
this.putAll(dataSet)
}
/**
@ -113,7 +113,7 @@ public suspend inline fun <T, reified R> TaskResultBuilder<R>.actionFrom(
action: Action<T, R>,
dependencyMeta: Meta = defaultDependencyMeta,
) {
branch(action.execute(from(selector, dependencyMeta), dependencyMeta))
this.putAll(action.execute(from(selector, dependencyMeta), dependencyMeta))
}

View File

@ -23,7 +23,7 @@ import kotlin.test.assertEquals
class FileDataTest {
val dataNode = DataTree<String> {
branch("dir") {
putAll("dir") {
wrap("a", "Some string") {
"content" put "Some string"
}