mr0xf00 / lensgenerator

Annotation based lens generation for Kotlin classes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Lens Generation for Kotlin

An annotation processor that generates lens for Kotlin classes.

Lens make updating immutable state very easy by allowing you to write :

newState = state.setValue(AppState.users[action.index].address.street, action.newStreet)

instead of

val newAddress = state.users[action.index].address.copy(street = action.newStreet)
val newUser = state.users[action.index].copy(address = newAddress)
val newUsers = state.users.toMutableList().apply { this[action.index] = newUser }
newState = state.copy(users = newUsers)

Getting Started

1. Download

plugins {
    id("com.google.devtools.ksp") version "1.7.10-1.0.6"
}
//...
dependecies {
    implementation("io.github.mr0xf00:lens-generator:0.1.0")
    ksp("io.github.mr0xf00:lens-generator:0.1.0")
    //for KMP prefer
    //add("ksp<Target>", "io.github.mr0xf00:lens-generator:0.1.0") 
}

KMP is supported but only jvm and js targets are available for now.

2. Annotate your classes/interfaces

Apply @GenerateLens your classes.

Only data classes and sealed classes/interfaces are supported for now.

3. Use the generated lens

val newInstance = MyClass.propertyName.nestedProperty.set(instance, value)
val value = MyClass.propertyName.get(instance)

See sample app for example usage as well as SealedTest.kt for sealed hierarchies and GenericTest.kt for generic classes.

Collections

The sample app uses PersistentList from kotlinx.collections.immutable.

See Lens.kt for an example on how to write your own lens to support immutable collection modification.

About

Annotation based lens generation for Kotlin classes

License:Apache License 2.0


Languages

Language:Kotlin 100.0%