Simplify the processing of sealed class/interface

Overview

shiirudo

Generates DSL to simplify processing branching by when expressions in sealed class/interface.

Setup

Refer to the KSP quickstart guide to make KSP available for your project.

Installation

Repository is now Jitpack:

repositories {
   maven { url "https://jitpack.io" }
}

Check the latest-version

implementation "com.tkhskt:shiirudo:[latest-version]"
ksp "com.tkhskt:shiirudo:[latest-version]"

Usage

Annotate the target sealed class/interface with the Shiirudo annotation.

@Shiirudo
sealed interface Event {
    object ShowDialog : Event
    object CloseDialog : Event
    object ShowToast : Event
}

When the project is built, shiirudo methods corresponding to subclasses of the annotated class are generated.

The shiirudo method can be used as follows.

fun handleEvent(event: Event) {
    event.shiirudo()
        .isShowDialog {

        }
        .isCloseDialog {

        }
        .isElse {

        }.execute()
}

// or

fun handleEvent(event: Event) {
    eventShiirudo {
        event
    }.isShowDialog {

    }.isCloseDialog {

    }.isElse {

    }.execute()
}

// or

fun handleEvent(event: Event) {
    event.shiirudo {
        isShowDialog {

        }
        isCloseDialog {

        }
        isElse {

        }
    }
}

Use with Flow

You can use shiirudo when you collect Flow as follows.

private suspend fun Flow<Event>.collectEvent(
    handler: EventShiirudoBuilder.() -> Unit,
) {
    collect {
        it.shiirudo(handler)
    }
}

fun handleEvent(eventFlow: Flow<Event>) {
    eventFlow.collectEvent {
        isShowDialog {

        }
        isCloseDialog {

        }
    }
}

Name Resolution

The code generated from the nested sealed class/interface and its subclasses is name resolved as follows.

class SampleViewModel : ViewModel() {

    @Shiirudo
    sealed interface Event {
        interface Dialog {
            object Show : Event
            object Close : Event
        }
        object ShowToast : Event
    }
}

fun handleEvent(event: SampleViewModel.Event) {
    event.shiirudo {
        isDialogShow {

        }
        isDialogClose {

        }
        isShowToast {

        }
    }
}

Examples

See the sample project.

License

MIT

You might also like...
Explore-KiiT-App - An app to simplify the complicated website navigation and keep track of Attendance
Explore-KiiT-App - An app to simplify the complicated website navigation and keep track of Attendance

KiiT Explore App "An app to simplify the complicated website navigation and keep

An interactive command line interface to a transactional key value store
An interactive command line interface to a transactional key value store

Transactional Key-Value Store An interactive command line interface to a transactional key value store. Commands: SET key value // store the value

A Certificate Authority with RESTful interface and WEB UI
A Certificate Authority with RESTful interface and WEB UI

minica A Certificate Authority with RESTful interface and WEB UI Home page View CA detail View Cert Detail With RESTful interface, you can manage cert

Test tv - A marketing company needs a simple pluggable application with a RESTful interface

Demo project A marketing company needs a simple pluggable application with a RES

Discord bot interface for Spring Initializr
Discord bot interface for Spring Initializr

Spring Initializr interface over Discord. Initialize your new Spring Boot project right in the app.

Kotlin & Java class for gRPC client

jvm-minter-grpc-class Kotlin & Java class for gRPC client Use @TODO Разное Добавление git subtree add --prefix node-grpc-gateway https://github.com/Mi

Kotlin compiler plugin that allows class delegation to be dynamic like property delegations

kotlin-dynamic-delegation Kotlin compiler plugin that allows class delegation to be dynamic like property delegations. The plugin is working in progre

A deep learning based mobile application for the multi-class classification of pneumonia into three categories via Chest X-rays

PneumoniaClassifier A deep learning based mobile application for the multi-class classification of pneumonia into three categories via Chest X-rays. W

Releases(0.1.1)
Owner
KeitaTakahashi
Android
KeitaTakahashi
Kotlin extension function provides a facility to "add" methods to class without inheriting a class or using any type of design pattern

What is Kotlin Extension Function ? Kotlin extension function provides a facility to "add" methods to class without inheriting a class or using any ty

mohsen 21 Dec 3, 2022
Clean MVVM with eliminating the usage of context from view models by introducing hilt for DI and sealed classes for displaying Errors in views using shared flows (one time event), and Stateflow for data

Clean ViewModel with Sealed Classes Following are the purposes of this repo Showing how you can remove the need of context in ViewModels. I. By using

Kashif Mehmood 22 Oct 26, 2022
🎲 Kotlin Symbol Processor to auto-generate extensive sealed classes and interfaces for Android and Kotlin.

SealedX ?? Kotlin Symbol Processor to auto-generate extensive sealed classes and interfaces for Android and Kotlin. Why SealedX? SealedX generates ext

Jaewoong Eum 236 Nov 30, 2022
A Kotlin Symbol Processor to list sealed object instances.

Sealed Object Instances A Kotlin Symbol Processor to list sealed object instances. Usage Let's say you have a similar structure of sealed classes (or

Simon Marquis 17 Nov 17, 2022
Exploring Kotlin Symbol Processing - KSP. This is just an experiment.

KSP example Exploring Kotlin Symbol Processing - KSP. This is just an experiment. Project contains 2 modules Processing Example Processing module is t

Merab Tato Kutalia 12 Aug 23, 2022
Kotlin Symbol Processing (KSP) sample project

Kotlin Symbol Processing (KSP) Sample Project Sample annotation processor created with Kotlin Symbol Processing (KSP) API. The repository supplements

Pavlo Stavytskyi 33 Dec 23, 2022
Mocking for Kotlin/Native and Kotlin Multiplatform using the Kotlin Symbol Processing API (KSP)

Mockative Mocking for Kotlin/Native and Kotlin Multiplatform using the Kotlin Symbol Processing API (KSP). Installation Mockative uses KSP to generate

Mockative 121 Dec 26, 2022
Image Processing Engine with GUI

Image Processing Engine with GUI Imperial College London Department of Computing Third Year Software Engineer Group Project Supervisor: Dr. Pancham Sh

null 1 Jan 14, 2022
This repository demonstrates how Kotlin can simplify Spring Boot configuration properties file mapping

Kotlin spring-boot nested config props This repository demonstrates how Kotlin can simplify Spring Boot configuration properties file mapping @Constru

Maksim Kostromin 1 Oct 11, 2021
Utility library dedicated for functional & non-functional codebases to simplify modelling of success and failure responses for the JVM languages 🔀

Expressible Utility library, part of the panda-lang SDK, dedicated for functional codebases that require enhanced response handling. Express yourself

Panda 28 Nov 14, 2022