Skip to content

MateriiApps/enumutil-kt

Repository files navigation

enumutil-kt Maven Central

A Kotlin KSP plugin for generating repetitive functions for enums.

Installation

plugins {
    id("com.google.devtools.ksp") version "1.9.20-1.0.14"
}

repositories {
    mavenCentral()
}

dependencies {
    implementation("io.github.materiiapps:enumutil:1.1.1")
    ksp("io.github.materiiapps:enumutil-ksp:1.1.1")
}

kotlin {
    sourceSets {
        getByName("main") {
            kotlin.srcDir("build/generated/ksp/main/kotlin")
        }
    }
}

Usages

@FromValue

Generate fromValue(...) extension methods for the target class. This matches the first enum parameter unless the field name is specified explicitly.

@FromValue
enum class OpCodes(val code: Int) {
    READY(1),
    DELETE(2),
    CREATE(3),
    DISCONNECT(4);

    // This is needed in order to have static extensions
    companion object
}

fun main() {
    val opCode = OpCodes.fromValue(1)
}