Skip to content

SimonMarquis/SealedObjectInstances

Repository files navigation

Sealed Object Instances Status

A Kotlin Symbol Processor to list sealed object instances.

Usage

Let's say you have a similar structure of sealed classes (or interfaces):

// FeatureFlag.kt
sealed class FeatureFlag {
    abstract val isEnabled: Boolean
}
// Debug.kt
sealed class Debug(override val isEnabled: Boolean = false) : FeatureFlag() {
    data object Logs : Debug(true)
    data object Traces : Debug()
    data object StrictMode : Debug()
}
// UI.kt
sealed class UI(override val isEnabled: Boolean = false) : FeatureFlag() {
    data object Animations : UI()
    data object Framerate : UI()
}

And you want to automatically list all FeatureFlags, then you have at least 3 options:

  • 🙅 Manually keep a list of objects somewhere, but this is cumbersome and error-prone.
  • 🤷 Use Kotlin reflection to browse the class declaration, with the following extension FeatureFlag::class.reflectSealedObjectInstances(), but this can become an expensive operation if you have a lot of objects and a deeply nested structure.
  • 🙆 Use Kotlin Symbol Processing (KSP) to create the list of objects at compile time.

This is exactly what this project does. You simply need to add the SealedObjectInstances annotation on the sealed class:

@SealedObjectInstances
sealed class FeatureFlag { /*...*/ }

And then you'll have access to the sealedObjectInstances() extension on the corresponding type:

val flags: Set<FeatureFlag> = FeatureFlag::class.sealedObjectInstances()

Note

You can annotate the companion object to access a simpler extension function (no more ::class prefix).

sealed class FeatureFlag {
    @SealedObjectInstances companion object;
    /*...*/
}

val flags: Set<FeatureFlag> = FeatureFlag.sealedObjectInstances()

Setup

In the module's build script, apply the com.google.devtools.ksp plugin with the matching Kotlin version: Maven Central

plugins {
    id("com.google.devtools.ksp") version "<version>"
}

Then add the library dependencies: Maven Central Sonatype Nexus (Snapshots)

dependencies {
    implementation("fr.smarquis.sealed:sealed-object-instances:<latest-version>")
    ksp("fr.smarquis.sealed:sealed-object-instances:<latest-version>")
}
🐙 GitHub Packages

GitHub release (latest SemVer)

[!NOTE]
You'll need to create a personal access token (PAT) with the read:packages permission to be able to download from this repository. https://docs.github.com/en/packages/learn-github-packages

repositories {
    maven {
        url = uri("https://maven.pkg.github.com/SimonMarquis/SealedObjectInstances")
        credentials {
            username = System.getenv("GITHUB_USERNAME")
            password = System.getenv("GITHUB_PACKAGES_READ_TOKEN")
        }
    }
}

dependencies {
    implementation("fr.smarquis.sealed:sealed-object-instances:<latest-version>")
    ksp("fr.smarquis.sealed:sealed-object-instances:<latest-version>")
}
🚀 JitPack.io

JitPack.io

repositories {
    maven {
        url = uri("https://jitpack.io")
    }
}

dependencies {
    implementation("com.github.SimonMarquis:SealedObjectInstances:<latest-version>")
    ksp("com.github.SimonMarquis:SealedObjectInstances:<latest-version>")
}

Make IDE aware of generated code

By default, IntelliJ IDEA or other IDEs don't know about the generated code. So it will mark references to generated symbols unresolvable. To make an IDE be able to reason about the generated symbols, mark the following paths as generated source roots:

  • Android project
    androidComponents.beforeVariants {
      kotlin.sourceSets.register(it.name) {
          kotlin.srcDir(file("$buildDir/generated/ksp/${it.name}/kotlin"))
      }
    }
  • Kotlin JVM project
    kotlin {
        sourceSets.main {
            kotlin.srcDir("build/generated/ksp/main/kotlin")
        }
        sourceSets.test {
            kotlin.srcDir("build/generated/ksp/test/kotlin")
        }
    }

Configuration

SealedObjectInstances annotation can be configured to produce different outputs, and is also repeatable:

@SealedObjectInstances
@SealedObjectInstances(name = "values", rawType = Array)
sealed class FeatureFlag { /*...*/ }

This code will produce these two extensions:

// The default extension
fun KClass<FeatureFlag>.sealedObjectInstances(): Set<FeatureFlag>
// The custom extension with different name and raw type
fun KClass<FeatureFlag>.values(): Array<FeatureFlag>

Known issues

  • Having multiple sealed classes/interfaces with the same name in the same package is currently not supported, and the KSP will fail. But this can be avoided by providing a custom generated fileName on the SealedObjectInstances annotation.
  • KT-8970: Using the extension from a static context (e.g. companion object), will return null values. A simple solution is to delegate to a lazy initializer: val values by lazy(MySealedClass::class::sealedObjectInstances).
  • Does not support non-standard (alphanumeric) class names that needs backticks like:
    sealed class `A-B-C`
  • google/ksp#1651: KSP does not run on common multiplatform code, requiring an extra manual "bridge" expect/actual method.