Remove name from descriptors. It is never used

This commit is contained in:
Alexander Nozik 2019-12-24 22:10:48 +03:00
parent ce8be78549
commit e532e8358e
2 changed files with 8 additions and 18 deletions

View File

@ -72,7 +72,7 @@ fun Value.toJson(descriptor: ValueDescriptor? = null): JsonElement {
//Use these methods to customize JSON key mapping //Use these methods to customize JSON key mapping
private fun NameToken.toJsonKey(descriptor: ItemDescriptor?) = toString() private fun NameToken.toJsonKey(descriptor: ItemDescriptor?) = toString()
private fun NodeDescriptor?.getDescriptor(key: String) = this?.items?.get(key) //private fun NodeDescriptor?.getDescriptor(key: String) = this?.items?.get(key)
fun Meta.toJson(descriptor: NodeDescriptor? = null): JsonObject { fun Meta.toJson(descriptor: NodeDescriptor? = null): JsonObject {
@ -141,15 +141,13 @@ class JsonMeta(val json: JsonObject, val descriptor: NodeDescriptor? = null) : M
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
private operator fun MutableMap<String, MetaItem<JsonMeta>>.set(key: String, value: JsonElement): Unit { private operator fun MutableMap<String, MetaItem<JsonMeta>>.set(key: String, value: JsonElement): Unit {
val itemDescriptor = descriptor.getDescriptor(key) val itemDescriptor = descriptor?.items?.get(key)
//use name from descriptor in case descriptor name differs from json key
val name = itemDescriptor?.name ?: key
return when (value) { return when (value) {
is JsonPrimitive -> { is JsonPrimitive -> {
this[name] = MetaItem.ValueItem(value.toValue(itemDescriptor as? ValueDescriptor)) as MetaItem<JsonMeta> this[key] = MetaItem.ValueItem(value.toValue(itemDescriptor as? ValueDescriptor)) as MetaItem<JsonMeta>
} }
is JsonObject -> { is JsonObject -> {
this[name] = MetaItem.NodeItem(JsonMeta(value, itemDescriptor as? NodeDescriptor)) this[key] = MetaItem.NodeItem(JsonMeta(value, itemDescriptor as? NodeDescriptor))
} }
is JsonArray -> { is JsonArray -> {
when { when {
@ -160,10 +158,10 @@ class JsonMeta(val json: JsonObject, val descriptor: NodeDescriptor? = null) : M
(it as JsonPrimitive).toValue(itemDescriptor as? ValueDescriptor) (it as JsonPrimitive).toValue(itemDescriptor as? ValueDescriptor)
} }
) )
this[name] = MetaItem.ValueItem(listValue) as MetaItem<JsonMeta> this[key] = MetaItem.ValueItem(listValue) as MetaItem<JsonMeta>
} }
else -> value.forEachIndexed { index, jsonElement -> else -> value.forEachIndexed { index, jsonElement ->
this["$name[$index]"] = jsonElement.toMetaItem(itemDescriptor) this["$key[$index]"] = jsonElement.toMetaItem(itemDescriptor)
} }
} }
} }

View File

@ -11,13 +11,6 @@ import hep.dataforge.values.ValueType
sealed class ItemDescriptor(override val config: Config) : Specific { sealed class ItemDescriptor(override val config: Config) : Specific {
/**
* The name of this item
*
* @return
*/
var name: String by string { error("Anonymous descriptors are not allowed") }
/** /**
* True if same name siblings with this name are allowed * True if same name siblings with this name are allowed
* *
@ -128,12 +121,12 @@ class NodeDescriptor(config: Config) : ItemDescriptor(config) {
* Add a value descriptor using block for * Add a value descriptor using block for
*/ */
fun value(name: String, block: ValueDescriptor.() -> Unit) { fun value(name: String, block: ValueDescriptor.() -> Unit) {
value(name, ValueDescriptor { this.name = name }.apply(block)) value(name, ValueDescriptor(block))
} }
fun value(name: Name, block: ValueDescriptor.() -> Unit) { fun value(name: Name, block: ValueDescriptor.() -> Unit) {
require(name.length >= 1) { "Name length for value descriptor must be non-empty" } require(name.length >= 1) { "Name length for value descriptor must be non-empty" }
buildNode(name.cutLast()).value(name.last().toString()) buildNode(name.cutLast()).value(name.last().toString(), block)
} }
val items: Map<String, ItemDescriptor> get() = nodes + values val items: Map<String, ItemDescriptor> get() = nodes + values
@ -234,7 +227,6 @@ class ValueDescriptor(config: Config) : ItemDescriptor(config) {
override fun wrap(config: Config): ValueDescriptor = ValueDescriptor(config) override fun wrap(config: Config): ValueDescriptor = ValueDescriptor(config)
inline fun <reified E : Enum<E>> enum(name: String) = ValueDescriptor { inline fun <reified E : Enum<E>> enum(name: String) = ValueDescriptor {
this.name = name
type(ValueType.STRING) type(ValueType.STRING)
this.allowedValues = enumValues<E>().map { Value.of(it.name) } this.allowedValues = enumValues<E>().map { Value.of(it.name) }
} }