From d47ea849d13634e4df079dca6caf3697015c51bd Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Thu, 16 Aug 2018 10:23:14 +0200 Subject: Main dependencies upgrade (Kotlin 1.2.60, coroutines 0.24.0, serialization 0.6.1, frontend plugin 0.0.37, jooby 1.5.0) --- src/main/kotlin/pl/treksoft/kvision/form/Form.kt | 28 +++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) (limited to 'src/main/kotlin/pl/treksoft') diff --git a/src/main/kotlin/pl/treksoft/kvision/form/Form.kt b/src/main/kotlin/pl/treksoft/kvision/form/Form.kt index 18acdf9f..b1e08606 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/Form.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/Form.kt @@ -41,6 +41,24 @@ internal data class FieldParams( val validator: ((F) -> Boolean?)? = null ) +/** + * A wrapper for a Map with a custom containsKey method implementation. + * Used with kotlinx.serialization Mapper. + */ +private class FormMapWrapper(private val map: Map) : Map { + override fun equals(other: Any?): Boolean = map == other + override fun hashCode(): Int = map.hashCode() + override fun toString(): String = map.toString() + override val size: Int get() = map.size + override fun isEmpty(): Boolean = map.isEmpty() + override fun containsKey(key: String): Boolean = if (key.indexOf('.') != -1) map.containsKey(key) else !(map.containsKey("$key.time") || map.containsKey("$key.size")) + override fun containsValue(value: @UnsafeVariance V): Boolean = map.containsValue(value) + override fun get(key: String): V? = map[key] + override val keys: Set get() = map.keys + override val values: Collection get() = map.values + override val entries: Set> get() = map.entries +} + /** * The form definition class. Can be used directly or indirectly inside a [FormPanel]. * @@ -67,9 +85,9 @@ class Form(private val panel: FormPanel? = null, private val seriali } is List<*> -> { @Suppress("UNCHECKED_CAST") - (entry.value as? List)?.let { - listOf(entry.key to entry.value, "${entry.key}.size" to it.size) + - it.mapIndexed { index, kFile -> + (entry.value as? List)?.let { list -> + listOf(entry.key to entry.value, "${entry.key}.size" to list.size) + + list.mapIndexed { index, kFile -> listOf( "${entry.key}.${index + 1}.name" to kFile.name, "${entry.key}.${index + 1}.size" to kFile.size, @@ -80,8 +98,8 @@ class Form(private val panel: FormPanel? = null, private val seriali } else -> listOf(entry.key to entry.value) } - }.toMap().withDefault { null } - val mapper = Mapper.InNullableMapper(map) + }.toMap() + val mapper = Mapper.InNullableMapper(FormMapWrapper(map)) mapper.read(serializer) } } -- cgit