Move scheme descriptors to core

This commit is contained in:
Alexander Nozik 2021-08-07 12:02:34 +03:00
parent d3c129526d
commit 24187722e4
2 changed files with 77 additions and 19 deletions

View File

@ -12,7 +12,7 @@ import kotlin.collections.set
public class MetaDescriptorBuilder internal constructor() { public class MetaDescriptorBuilder internal constructor() {
public var info: String? = null public var info: String? = null
public var children: MutableMap<String, MetaDescriptorBuilder> = hashMapOf() public var children: MutableMap<String, MetaDescriptorBuilder> = linkedMapOf()
public var multiple: Boolean = false public var multiple: Boolean = false
public var valueRequirement: ValueRequirement = ValueRequirement.NONE public var valueRequirement: ValueRequirement = ValueRequirement.NONE
@ -35,24 +35,6 @@ public class MetaDescriptorBuilder internal constructor() {
attributes.apply(block) attributes.apply(block)
} }
public fun item(
name: Name,
descriptor: MetaDescriptor,
block: MetaDescriptorBuilder.() -> Unit = {}
): MetaDescriptorBuilder {
return when (name.length) {
0 -> error("Can't set descriptor to root")
1 -> {
val item = descriptor.toBuilder().apply(block)
children[name.first().body] = item
item
}
else -> children.getOrPut(name.first().body) {
MetaDescriptorBuilder()
}.item(name.cutFirst(), descriptor, block)
}
}
public fun item(name: Name, block: MetaDescriptorBuilder.() -> Unit = {}): MetaDescriptorBuilder { public fun item(name: Name, block: MetaDescriptorBuilder.() -> Unit = {}): MetaDescriptorBuilder {
return when (name.length) { return when (name.length) {
0 -> apply(block) 0 -> apply(block)
@ -67,6 +49,24 @@ public class MetaDescriptorBuilder internal constructor() {
} }
} }
public fun node(
name: Name,
descriptor: MetaDescriptor,
block: MetaDescriptorBuilder.() -> Unit = {}
): MetaDescriptorBuilder = when (name.length) {
0 -> error("Can't set descriptor to root")
1 -> {
val item = descriptor.toBuilder().apply {
valueRequirement = ValueRequirement.ABSENT
}.apply(block)
children[name.first().body] = item
item
}
else -> children.getOrPut(name.first().body) {
MetaDescriptorBuilder()
}.node(name.cutFirst(), descriptor, block)
}
public var allowedValues: List<Value> public var allowedValues: List<Value>
get() = attributes[MetaDescriptor.ALLOWED_VALUES_KEY]?.value?.list ?: emptyList() get() = attributes[MetaDescriptor.ALLOWED_VALUES_KEY]?.value?.list ?: emptyList()
set(value) { set(value) {
@ -131,6 +131,16 @@ public fun MetaDescriptorBuilder.node(name: String, block: MetaDescriptorBuilder
node(Name.parse(name), block) node(Name.parse(name), block)
} }
public fun MetaDescriptorBuilder.node(
key: String,
described: Described,
block: MetaDescriptorBuilder.() -> Unit = {},
) {
described.descriptor?.let {
node(Name.parse(key), it, block)
}
}
public fun MetaDescriptorBuilder.required() { public fun MetaDescriptorBuilder.required() {
valueRequirement = ValueRequirement.REQUIRED valueRequirement = ValueRequirement.REQUIRED
} }

View File

@ -0,0 +1,48 @@
package space.kscience.dataforge.meta.descriptors
import space.kscience.dataforge.meta.Scheme
import space.kscience.dataforge.meta.SchemeSpec
import space.kscience.dataforge.values.ValueType
import kotlin.reflect.KProperty1
import kotlin.reflect.typeOf
public inline fun <S : Scheme, reified T> MetaDescriptorBuilder.value(
property: KProperty1<S, T>,
noinline block: MetaDescriptorBuilder.() -> Unit = {},
) {
when (typeOf<T>()) {
typeOf<Number>(), typeOf<Int>(), typeOf<Double>(), typeOf<Short>(), typeOf<Long>(), typeOf<Float>() ->
value(property.name, ValueType.NUMBER) {
block()
}
typeOf<Number?>(), typeOf<Int?>(), typeOf<Double?>(), typeOf<Short?>(), typeOf<Long?>(), typeOf<Float?>() ->
value(property.name, ValueType.NUMBER) {
block()
}
typeOf<Boolean>() -> value(property.name, ValueType.BOOLEAN) {
block()
}
typeOf<List<Number>>(), typeOf<List<Int>>(), typeOf<List<Double>>(), typeOf<List<Short>>(), typeOf<List<Long>>(), typeOf<List<Float>>(),
typeOf<IntArray>(), typeOf<DoubleArray>(), typeOf<ShortArray>(), typeOf<LongArray>(), typeOf<FloatArray>(),
-> value(property.name, ValueType.NUMBER) {
multiple = true
block()
}
typeOf<String>() -> value(property.name, ValueType.STRING) {
block()
}
typeOf<List<String>>(), typeOf<Array<String>>() -> value(property.name, ValueType.STRING) {
multiple = true
block()
}
else -> item(property.name, block)
}
}
public inline fun <S : Scheme, reified T : Scheme> MetaDescriptorBuilder.scheme(
property: KProperty1<S, T>,
spec: SchemeSpec<T>,
noinline block: MetaDescriptorBuilder.() -> Unit = {},
) {
node(property.name, spec, block)
}