Convenience functions

This commit is contained in:
Alexander Nozik 2019-04-03 14:00:45 +03:00
parent 9355eefb65
commit 04c430d9ce
2 changed files with 13 additions and 7 deletions

View File

@ -344,14 +344,17 @@ class MutableMorphDelegate<M : MutableMetaNode<M>, T : Configurable>(
val meta: M,
private val key: String? = null,
private val converter: (Meta) -> T
) :
ReadWriteProperty<Any?, T> {
override fun getValue(thisRef: Any?, property: KProperty<*>): T {
return converter(meta[key ?: property.name]?.node ?: EmptyMeta)
) : ReadWriteProperty<Any?, T?> {
override fun getValue(thisRef: Any?, property: KProperty<*>): T? {
return meta[key ?: property.name]?.node?.let(converter)
}
override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
meta[key ?: property.name] = value.config
override fun setValue(thisRef: Any?, property: KProperty<*>, value: T?) {
if (value == null) {
meta.remove(key ?: property.name)
} else {
meta[key ?: property.name] = value.config
}
}
}

View File

@ -116,4 +116,7 @@ fun Name.withIndex(index: String): Name {
tokens.removeAt(tokens.size - 1)
tokens.add(last)
return Name(tokens)
}
}
operator fun <T> Map<Name, T>.get(name: String) = get(name.toName())
operator fun <T> MutableMap<Name, T>.set(name: String, value: T) = set(name.toName(), value)