tweak MetaDescriptor.kt

This commit is contained in:
Alexander Nozik 2021-08-07 10:42:43 +03:00
parent 5632487dca
commit d3c129526d

View File

@ -35,23 +35,31 @@ public class MetaDescriptorBuilder internal constructor() {
attributes.apply(block) attributes.apply(block)
} }
public fun item(name: Name, descriptor: MetaDescriptor, block: MetaDescriptorBuilder.() -> Unit) { public fun item(
when (name.length) { name: Name,
0 -> { 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
} }
1 -> children[name.first().body] = descriptor.toBuilder().apply(block)
else -> children.getOrPut(name.first().body) { else -> children.getOrPut(name.first().body) {
MetaDescriptorBuilder() MetaDescriptorBuilder()
}.item(name.cutFirst(), descriptor, block) }.item(name.cutFirst(), descriptor, block)
} }
} }
public fun item(name: Name, block: MetaDescriptorBuilder.() -> Unit) { public fun item(name: Name, block: MetaDescriptorBuilder.() -> Unit = {}): MetaDescriptorBuilder {
when (name.length) { return when (name.length) {
0 -> apply(block) 0 -> apply(block)
1 -> { 1 -> {
val target = MetaDescriptorBuilder().apply(block) val target = MetaDescriptorBuilder().apply(block)
children[name.first().body] = target children[name.first().body] = target
target
} }
else -> { else -> {
children.getOrPut(name.first().body) { MetaDescriptorBuilder() }.item(name.cutFirst(), block) children.getOrPut(name.first().body) { MetaDescriptorBuilder() }.item(name.cutFirst(), block)
@ -96,8 +104,8 @@ public fun MetaDescriptorBuilder.value(
name: Name, name: Name,
type: ValueType, type: ValueType,
vararg additionalTypes: ValueType, vararg additionalTypes: ValueType,
block: MetaDescriptorBuilder.() -> Unit block: MetaDescriptorBuilder.() -> Unit = {}
): Unit = item(name) { ): MetaDescriptorBuilder = item(name) {
type(type, *additionalTypes) type(type, *additionalTypes)
block() block()
} }
@ -106,13 +114,15 @@ public fun MetaDescriptorBuilder.value(
name: String, name: String,
type: ValueType, type: ValueType,
vararg additionalTypes: ValueType, vararg additionalTypes: ValueType,
block: MetaDescriptorBuilder.() -> Unit block: MetaDescriptorBuilder.() -> Unit = {}
): Unit = value(Name.parse(name), type, additionalTypes = additionalTypes, block) ): MetaDescriptorBuilder = value(Name.parse(name), type, additionalTypes = additionalTypes, block)
/** /**
* Create and configure child value descriptor * Create and configure child value descriptor
*/ */
public fun MetaDescriptorBuilder.node(name: Name, block: MetaDescriptorBuilder.() -> Unit): Unit = item(name) { public fun MetaDescriptorBuilder.node(
name: Name, block: MetaDescriptorBuilder.() -> Unit
): MetaDescriptorBuilder = item(name) {
valueRequirement = ValueRequirement.ABSENT valueRequirement = ValueRequirement.ABSENT
block() block()
} }
@ -129,7 +139,7 @@ public inline fun <reified E : Enum<E>> MetaDescriptorBuilder.enum(
key: Name, key: Name,
default: E?, default: E?,
crossinline modifier: MetaDescriptorBuilder.() -> Unit = {}, crossinline modifier: MetaDescriptorBuilder.() -> Unit = {},
): Unit = value(key, ValueType.STRING) { ): MetaDescriptorBuilder = value(key, ValueType.STRING) {
default?.let { default?.let {
this.default = default.asValue() this.default = default.asValue()
} }