Convenience methods to access DataNode elements

This commit is contained in:
Alexander Nozik 2019-09-11 20:43:55 +03:00
parent c44e004495
commit b730d49e6e
5 changed files with 21 additions and 7 deletions

View File

@ -5,12 +5,23 @@ import hep.dataforge.names.toName
class DataFilter(override val config: Config) : Specific {
/**
* A source node for the filter
*/
var from by string()
/**
* A target placement for the filtered node
*/
var to by string()
/**
* A regular expression pattern for the filter
*/
var pattern by string("*.")
// val prefix by string()
// val suffix by string()
fun isEmpty(): Boolean = config.isEmpty()
companion object : Specification<DataFilter> {
override fun wrap(config: Config): DataFilter = DataFilter(config)
}

View File

@ -72,6 +72,8 @@ operator fun <T : Any> DataNode<T>.get(name: Name): DataItem<T>? = when (name.le
else -> get(name.first()!!.asName()).node?.get(name.cutFirst())
}
operator fun <T : Any> DataNode<T>.get(name: String): DataItem<T>? = get(name.toString())
/**
* Sequence of all children including nodes
*/

View File

@ -8,7 +8,7 @@ import kotlin.reflect.KClass
import kotlin.reflect.full.isSubclassOf
@Suppress("UNCHECKED_CAST")
private fun <T : Any, R : Any> Data<T>.safeCast(type: KClass<out R>): Data<R>? {
fun <T : Any, R : Any> Data<T>.safeCast(type: KClass<out R>): Data<R>? {
return if (this.type.isSubclassOf(type)) {
return object : Data<R> {
override val meta: Meta get() = this@safeCast.meta
@ -23,6 +23,8 @@ private fun <T : Any, R : Any> Data<T>.safeCast(type: KClass<out R>): Data<R>? {
}
}
inline fun <reified R : Any> Data<*>.safeCast(): Data<R>? = safeCast(R::class)
class CastDataNode<out T : Any>(val origin: DataNode<Any>, override val type: KClass<out T>) : DataNode<T> {
override val items: Map<NameToken, DataItem<T>> by lazy {
origin.items.mapNotNull { (key, item) ->

View File

@ -15,18 +15,16 @@ kotlin {
commonMain{
dependencies {
api(project(":dataforge-context"))
api("org.jetbrains.kotlinx:kotlinx-io:0.1.14")
}
}
jvmMain{
dependencies {
api("org.jetbrains.kotlinx:kotlinx-io-jvm:0.1.14")
}
}
jsMain{
dependencies{
//api(npm("text-encoding"))
api("org.jetbrains.kotlinx:kotlinx-io-js:0.1.14")
api(npm("text-encoding"))
}
}
}

View File

@ -12,6 +12,7 @@ import hep.dataforge.data.dataSequence
import hep.dataforge.meta.*
import hep.dataforge.names.EmptyName
import hep.dataforge.names.Name
import hep.dataforge.names.asName
import hep.dataforge.names.toName
import hep.dataforge.workspace.TaskModel.Companion.MODEL_TARGET_KEY
@ -43,7 +44,7 @@ data class TaskModel(
}
companion object {
const val MODEL_TARGET_KEY = "@target"
val MODEL_TARGET_KEY = "@target".asName()
}
}