Hi, I have a question. Is there support for enums ...
# exposed
h
Hi, I have a question. Is there support for enums with KotlinX Serialization? Or planned support for the future?
For some context: I have a table on my postgresql with an enum column, and I'm actually declaring the field like this:
Copy code
val status = enumerationByName("status", 50, SubscriptionStatus::class)
And my enum is this one:
Copy code
@Serializable
enum class SubscriptionStatus {
    @SerialName("active")
    Active,

    @SerialName("canceled")
    Canceled,

    @SerialName("expired")
    Expired,

    @SerialName("pending")
    Pending
}
The result query uses the enum's name as the method implies, but it would be nice to have an option to support @SerialName. I couldn't find it on the docs
n
You can use either .transform on the column and store string under the hood, or use EnumCodec with similar thing as transform (in case you wish to store enum specifically) Then you can use reflection if you want, but I don't see the problem to just do an in-and-out method to convert it back and forth
exposed obviously is not locked on serialization thus don't support it i also don't think it's planned
h
I see, thank you very much for the ideas! I ended up making two functions at each enum that manually maps them. and using a customEnumeration column to keep everything centralized and fast
Copy code
// on enums 
fun toDBEnum(value: SubscriptionStatus): String {...}
fun fromDBEnum(value: String): SubscriptionStatus {...}

// on table object
val status: Column<SubscriptionStatus> = customEnumeration(
    name = "status",
    fromDb = { value -> SubscriptionStatus.fromDBEnum(value as String) },
    toDb = { PGEnum("subscription_status", SubscriptionStatus.toDBEnum(it)) }
)