Giter Site home page Giter Site logo

lensgenerator's Introduction

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.

lensgenerator's People

Contributors

mr0xf00 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.