diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main/kotlin/pl/treksoft/kvision/form/Form.kt | 28 |
1 files changed, 23 insertions, 5 deletions
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 @@ -42,6 +42,24 @@ internal data class FieldParams<in F : FormControl>( ) /** + * A wrapper for a Map with a custom containsKey method implementation. + * Used with kotlinx.serialization Mapper. + */ +private class FormMapWrapper<out V>(private val map: Map<String, V>) : Map<String, V> { + 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<String> get() = map.keys + override val values: Collection<V> get() = map.values + override val entries: Set<Map.Entry<String, V>> get() = map.entries +} + +/** * The form definition class. Can be used directly or indirectly inside a [FormPanel]. * * @constructor Creates a form with a given modelFactory function @@ -67,9 +85,9 @@ class Form<K : Any>(private val panel: FormPanel<K>? = null, private val seriali } is List<*> -> { @Suppress("UNCHECKED_CAST") - (entry.value as? List<KFile>)?.let { - listOf(entry.key to entry.value, "${entry.key}.size" to it.size) + - it.mapIndexed { index, kFile -> + (entry.value as? List<KFile>)?.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<K : Any>(private val panel: FormPanel<K>? = 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) } } |