How to use enumerationByName, but with different s...
# exposed
m
How to use enumerationByName, but with different stored values compared to in-code names? Since my existing DB has lowercase values, but I want PascalCase for my in-code enum names. For example, this is what I have now:
Copy code
enum class StatusAffiliate{
    deleted,
    active,
}
As you can see, the enum names are in lowercase for compatibility with current DB values. But I want it more like this if there’s a way:
Copy code
enum class StatusAffiliate{
    @SerialName("deleted") Deleted,
    @SerialName("active") Active,
}
1
o
It looks like
enumerationByName
doesn't support such an option, it just takes the names of the enum values as keys of the map (
enumConstants by lazy { klass.java.enumConstants!!.associateBy { it.name } }
- the part of
EnumerationNameColumnType
class) I'd say you have to use
customEnumeration
for your case with the current api. It could write something like this at the first glance:
Copy code
enum class StatusAffiliate(val tableName: String) {
        Deleted("deleted"),
        Active("active"),
    }

    object TestCustomEnumTable : IntIdTable() {
        val value = customEnumeration(
            "value", "status_affiliate",
            fromDb = { value ->
                when (value) {
                    is String -> StatusAffiliate.entries.find { it.tableName == value } ?: error("Enum value does not exists")
                    else -> error("The value from DB is not string")
                }
            },
            toDb = { it.tableName}
        )
    }