Fixed handling unknown enum values

This commit is contained in:
dankito 2024-09-09 00:13:51 +02:00
parent d25802f3bb
commit cd8e0a53ad
1 changed files with 12 additions and 3 deletions

View File

@ -8,6 +8,7 @@ import net.codinux.banking.dataaccess.entities.AccountTransactionEntity
import net.codinux.banking.dataaccess.entities.BankAccountEntity
import net.codinux.banking.dataaccess.entities.UserAccountEntity
import net.codinux.banking.ui.model.AccountTransactionViewModel
import net.codinux.log.logger
import kotlin.enums.EnumEntries
import kotlin.js.JsName
import kotlin.jvm.JvmName
@ -22,6 +23,7 @@ open class SqliteBankingRepository(
private val accountTransactionQueries = database.accountTransactionQueries
private val log by logger()
override fun getAllUserAccounts(): List<UserAccountEntity> {
val bankAccounts = getAllBankAccounts().groupBy { it.userAccountId }
@ -254,14 +256,21 @@ open class SqliteBankingRepository(
private fun <E : Enum<E>> mapEnum(enum: Enum<E>): String = enum.name
private fun <E : Enum<E>> mapToEnum(enumName: String, values: EnumEntries<E>): E =
values.first { it.name == enumName }
private fun <E : Enum<E>> mapToEnum(enumName: String, values: EnumEntries<E>): E? {
val mapped = values.firstOrNull { it.name == enumName }
if (mapped == null) {
log.warn("Could not map '$enumName' to Enum ${values.first()::class}")
}
return mapped
}
private fun <E : Enum<E>> mapEnumCollectionToString(enums: Collection<E>): String =
enums.joinToString(",") { it.name }
private fun <E : Enum<E>> mapEnumSet(enumsString: String, values: EnumEntries<E>): Set<E> =
enumsString.split(',').filterNot { it.isBlank() }.map { mapToEnum(it, values) }.toSet()
enumsString.split(',').filterNot { it.isBlank() }.mapNotNull { mapToEnum(it, values) }.toSet()
@JvmName("mapIntNullable")
@JsName("mapIntNullable")