Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to reuse apiName function for serializable enum classes #412

Open
PattaFeuFeu opened this issue Dec 28, 2023 · 2 comments
Open

Try to reuse apiName function for serializable enum classes #412

PattaFeuFeu opened this issue Dec 28, 2023 · 2 comments
Labels
code quality Everything related to code quality help wanted Extra attention is needed

Comments

@PattaFeuFeu
Copy link
Collaborator

At some point, we introduced the apiName function on some enum classes where the serializable name that is expected by the Mastodon API contains characters we cannot recreate in enum class names (such as posting:default:visibility) and thus cannot easily get the name of those classes by just getting its name.toLowercase().

The current approach is as such:

@Serializable
enum class NotificationType {
    […]
    @SerialName("update")
    UPDATE;

    @OptIn(ExperimentalSerializationApi::class)
    val apiName: String get() = serializer().descriptor.getElementName(ordinal)
}

That approach works well but we need to add those exact lines for every enum class where we want that apiName function.

When implementing a solution for this ticket it would be good if we could reuse the same e.g. extension function so that all enum classes (or at least those enum classes that have the @Serializable annotation and thus a serializer() to begin with) automatically.

@PattaFeuFeu PattaFeuFeu added the code quality Everything related to code quality label Dec 28, 2023
@PattaFeuFeu PattaFeuFeu added the help wanted Extra attention is needed label Dec 28, 2023
@G10xy
Copy link
Contributor

G10xy commented Jan 3, 2024

I would suggest creating an interface that all your serializable enums implement.
For example:

interface ApiNamed {
    val apiName: String
}

@Serializable
enum class NotificationType : ApiNamed {
    @SerialName("update")
    UPDATE;

    @OptIn(ExperimentalSerializationApi::class)
    override val apiName: String
        get() = serializer().descriptor.getElementName(ordinal)
}

Otherwise, I think it is possible to create extension function for all enum classes that have the @Serializable annotation, something like that: https://discuss.kotlinlang.org/t/extension-function-for-enums-of-the-same-shape/17262

@PattaFeuFeu
Copy link
Collaborator Author

@G10xy An interface wouldn’t really solve our problem of repeating ourselves constantly, unfortunately.

An extension function was what I had in mind. I just don’t know yet if it’s possible to create one only for enum classes that also have the @Serializable annotation but without using reflection. 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code quality Everything related to code quality help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants