Remove KClass from dataforge-data
This commit is contained in:
parent
1970243785
commit
7d3df24568
@ -12,44 +12,15 @@ import kotlin.reflect.full.isSubtypeOf
|
|||||||
import kotlin.reflect.typeOf
|
import kotlin.reflect.typeOf
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if data could be safely cast to given class
|
|
||||||
*/
|
|
||||||
private fun <R : Any> Data<*>.canCast(type: KType): Boolean = this.type.isSubtypeOf(type)
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cast the node to given type if the cast is possible or return null
|
* Cast the node to given type if the cast is possible or return null
|
||||||
*/
|
*/
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
private fun <R : Any> Data<*>.castOrNull(type: KType): Data<R>? =
|
private fun <R : Any> Data<*>.castOrNull(type: KType): Data<R>? =
|
||||||
if (!canCast<R>(type)) null else object : Data<R> by (this as Data<R>) {
|
if (!this.type.isSubtypeOf(type)) null else object : Data<R> by (this as Data<R>) {
|
||||||
override val type: KType = type
|
override val type: KType = type
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Unsafe cast of data node
|
|
||||||
*/
|
|
||||||
private fun <R : Any> Data<*>.cast(type: KType): Data<R> =
|
|
||||||
castOrNull(type) ?: error("Can't cast ${this.type} to $type")
|
|
||||||
|
|
||||||
private inline fun <reified R : Any> Data<*>.cast(): Data<R> = cast(typeOf<R>())
|
|
||||||
|
|
||||||
@Suppress("UNCHECKED_CAST")
|
|
||||||
private fun <R : Any> DataSet<*>.castOrNull(type: KType): DataSet<R>? =
|
|
||||||
if (!canCast<R>(type)) null else object : DataSet<R> by (this as DataSet<R>) {
|
|
||||||
override val dataType: KType = type
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private fun <R : Any> DataSet<*>.cast(type: KType): DataSet<R> =
|
|
||||||
castOrNull(type) ?: error("Can't cast ${this.dataType} to $type")
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check that node is compatible with given type meaning that each element could be cast to the type
|
|
||||||
*/
|
|
||||||
private fun <R : Any> DataSet<*>.canCast(type: KType): Boolean =
|
|
||||||
type.isSubtypeOf(this.dataType)
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Select all data matching given type and filters. Does not modify paths
|
* Select all data matching given type and filters. Does not modify paths
|
||||||
*/
|
*/
|
||||||
@ -61,10 +32,11 @@ internal fun <R : Any> DataSet<*>.select(
|
|||||||
): ActiveDataSet<R> = object : ActiveDataSet<R> {
|
): ActiveDataSet<R> = object : ActiveDataSet<R> {
|
||||||
override val dataType = type
|
override val dataType = type
|
||||||
|
|
||||||
@Suppress("UNCHECKED_CAST")
|
|
||||||
override fun flow(): Flow<NamedData<R>> = this@select.flow().filter {
|
override fun flow(): Flow<NamedData<R>> = this@select.flow().filter { datum ->
|
||||||
it.type.isSubtypeOf(type) && (namePattern == null || it.name.matches(namePattern))
|
datum.type.isSubtypeOf(type) && (namePattern == null || datum.name.matches(namePattern))
|
||||||
}.map {
|
}.map {
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
it as NamedData<R>
|
it as NamedData<R>
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,7 +44,7 @@ internal fun <R : Any> DataSet<*>.select(
|
|||||||
|
|
||||||
override val updates: Flow<Name> = this@select.updates.filter {
|
override val updates: Flow<Name> = this@select.updates.filter {
|
||||||
val datum = this@select.getData(it)
|
val datum = this@select.getData(it)
|
||||||
datum?.canCast<R>(type) ?: false
|
datum?.type?.isSubtypeOf(type) ?: false
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user