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

View File

@ -117,3 +117,6 @@ fun Name.withIndex(index: String): Name {
tokens.add(last) tokens.add(last)
return Name(tokens) 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)