From 04c430d9cea690542bcc58a4aa450a4afc3d7b60 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Wed, 3 Apr 2019 14:00:45 +0300 Subject: [PATCH] Convenience functions --- .../kotlin/hep/dataforge/meta/Delegates.kt | 15 +++++++++------ .../commonMain/kotlin/hep/dataforge/names/Name.kt | 5 ++++- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Delegates.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Delegates.kt index 8cb9250e..ba3bd883 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Delegates.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Delegates.kt @@ -344,14 +344,17 @@ class MutableMorphDelegate, T : Configurable>( val meta: M, private val key: String? = null, private val converter: (Meta) -> T -) : - ReadWriteProperty { - override fun getValue(thisRef: Any?, property: KProperty<*>): T { - return converter(meta[key ?: property.name]?.node ?: EmptyMeta) +) : ReadWriteProperty { + 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 + } } } diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/names/Name.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/names/Name.kt index 4ce3c1e5..229598e4 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/names/Name.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/names/Name.kt @@ -116,4 +116,7 @@ fun Name.withIndex(index: String): Name { tokens.removeAt(tokens.size - 1) tokens.add(last) return Name(tokens) -} \ No newline at end of file +} + +operator fun Map.get(name: String) = get(name.toName()) +operator fun MutableMap.set(name: String, value: T) = set(name.toName(), value) \ No newline at end of file