Added common ancestor to descriptors

This commit is contained in:
Alexander Nozik 2019-06-09 18:48:26 +03:00
parent 4e6cf3e785
commit 07ff982d08
5 changed files with 32 additions and 14 deletions

View File

@ -0,0 +1,9 @@
package hep.dataforge.descriptors
interface ItemDescriptor {
val name: String
val multiple: Boolean
val required: Boolean
val info: String?
val tags: List<String>
}

View File

@ -31,14 +31,14 @@ import hep.dataforge.names.toName
*
* @author Alexander Nozik
*/
class NodeDescriptor(override val config: Config) : Specific {
class NodeDescriptor(override val config: Config) : ItemDescriptor, Specific {
/**
* The name of this node
*
* @return
*/
var name: String by string { error("Anonymous descriptors are not allowed") }
override var name: String by string { error("Anonymous descriptors are not allowed") }
/**
@ -54,28 +54,28 @@ class NodeDescriptor(override val config: Config) : Specific {
*
* @return
*/
var multiple: Boolean by boolean(false)
override var multiple: Boolean by boolean(false)
/**
* True if the node is required
*
* @return
*/
var required: Boolean by boolean { default == null }
override var required: Boolean by boolean { default == null }
/**
* The node description
*
* @return
*/
var info: String? by string()
override var info: String? by string()
/**
* A list of tags for this node. Tags used to customize node usage
*
* @return
*/
var tags: List<String> by value{ value ->
override var tags: List<String> by value { value ->
value?.list?.map { it.string } ?: emptyList()
}
@ -117,6 +117,8 @@ class NodeDescriptor(override val config: Config) : Specific {
node(name, build { this.name = name }.apply(block))
}
val items: Map<String, ItemDescriptor> get() = nodes + values
//override val descriptor: NodeDescriptor = empty("descriptor")

View File

@ -29,7 +29,7 @@ import hep.dataforge.values.ValueType
*
* @author Alexander Nozik
*/
class ValueDescriptor(override val config: Config) : Specific {
class ValueDescriptor(override val config: Config) : ItemDescriptor, Specific {
/**
* The default for this value. Null if there is no default.
@ -47,28 +47,28 @@ class ValueDescriptor(override val config: Config) : Specific {
*
* @return
*/
var multiple: Boolean by boolean(false)
override var multiple: Boolean by boolean(false)
/**
* True if the value is required
*
* @return
*/
var required: Boolean by boolean { default == null }
override var required: Boolean by boolean { default == null }
/**
* Value name
*
* @return
*/
var name: String by string { error("Anonymous descriptors are not allowed") }
override var name: String by string { error("Anonymous descriptors are not allowed") }
/**
* The value info
*
* @return
*/
var info: String? by string()
override var info: String? by string()
/**
* A list of allowed ValueTypes. Empty if any value type allowed
@ -83,7 +83,7 @@ class ValueDescriptor(override val config: Config) : Specific {
this.type = listOf(*t)
}
var tags: List<String> by value { value ->
override var tags: List<String> by value { value ->
value?.list?.map { it.string } ?: emptyList()
}

View File

@ -19,7 +19,7 @@ class Laminate(layers: List<Meta>) : Meta {
constructor(vararg layers: Meta) : this(layers.asList())
override val items: Map<NameToken, MetaItem<out Meta>>
override val items: Map<NameToken, MetaItem<Meta>>
get() = layers.map { it.items.keys }.flatten().associateWith { key ->
layers.asSequence().map { it.items[key] }.filterNotNull().let(replaceRule)
}

View File

@ -143,7 +143,8 @@ fun <M : MetaNode<M>> M.getAll(name: Name): Map<String, MetaItem<M>> {
fun <M : MetaNode<M>> M.getAll(name: String): Map<String, MetaItem<M>> = getAll(name.toName())
operator fun <M : MetaNode<M>> MetaNode<M>.get(name: Name): MetaItem<M>? {
operator fun <M : MetaNode<M>> MetaNode<M>?.get(name: Name): MetaItem<M>? {
if (this == null) return null
return name.first()?.let { token ->
val tail = name.cutFirst()
when (tail.length) {
@ -159,6 +160,12 @@ operator fun <M : MetaNode<M>> MetaNode<M>?.get(key: String): MetaItem<M>? = if
this[key.toName()]
}
operator fun <M : MetaNode<M>> MetaNode<M>?.get(key: NameToken): MetaItem<M>? = if (this == null) {
null
} else {
this[key.asName()]
}
/**
* Equals and hash code implementation for meta node
*/