Playground project for Koin Koin Compiler - Sandbox

Overview

Koin Compiler - Sandbox

The goal of Koin compiler & Annotations project is to help declare Koin definition in a very fast and intuitive way, and generate all underlying Koin DSL.

Koin Annotation Processing 🚀

Koin code generation is mostly instant for Koin: just a few lines to generate. It is really easily readable/debuggable.

Even with ~~1000 of definitions, project compilation has almost no impact.

Here with such power from Ksp project, we can bring ease even more Kotlin dependency injection. We keep Koin API as it. We just avoid you to write definitions and modules.

Automatic definition binding

When tagging a component to be defined in Koin, we can easily declare all related supertypes directly:

@Single
class ElectricHeater : Heater 

would generate definition:

single { ElectricHeater() } bind Heater::class

Using generated content

The only thing to setup in particular is the org.koin.ksp.generated.* import as follow, to be able to use genrated extensions:

import org.koin.ksp.generated.*

fun main() {
    startKoin {
        printLogger()
        // ... generated features
    }
    // ...
}

Annotated definitions without modules

We need to use a default module with defaultModule() extension:

import org.koin.ksp.generated.*

fun main() {
    startKoin {
        printLogger()
        // generated default module
        defaultModule()
    }
    // ...
}

tag with @Single any components:

@Single
class CoffeeMaker(private val pump: Pump, private val heater: Heater)

@Single
class Thermosiphon(private val heater: Heater) : Pump

@Single
class ElectricHeater : Heater 

What is generated:

package org.koin.ksp.generated
// imports ...

fun KoinApplication.defaultModule() = modules(defaultModule)
val defaultModule = module {
    // definitions here ...
}

Class Module and functions as definitions

We want to use a CoffeeAppModule module class. The .module extension on CoffeeAppModule class will be generated:

import org.koin.ksp.generated.*

fun main() {
    startKoin {
        printLogger()
        modules(
        // generated .module extension on module class
          CoffeeAppModule().module
        )
    }
    // ...
}

Let's define a class module with annotations on functions:

@Module
class CoffeeAppModule {

  @Single
  fun heater() : Heater = ElectricHeater()

  @Single
  fun pump(heater: Heater) : Pump = Thermosiphon(heater)

  @Single
  fun coffeeMaker(heater: Heater, pump: Pump) = CoffeeMaker(pump, heater)
}

What is generated:

package org.koin.ksp.generated
// imports

val CoffeeAppModuleModule = module {
  // generated module instance
  val moduleInstance = org.koin.example.coffee.CoffeeAppModule()
  // definitions using functions here ...
}
val org.koin.example.coffee.CoffeeAppModule.module : org.koin.core.module.Module get() = CoffeeAppModuleModule

Scan all definitions into a Class Module

Rather than defining each component, we can allow a module to scan definitions for current package and sub packages:

import org.koin.ksp.generated.*

fun main() {
    startKoin {
        printLogger()
        modules(
        // generated .module extension on moduel class
          CoffeeAppModule().module
        )
    }
    // ...
}

Let's define a class module, use @ComponentScan to scan definitions for a given module, @Single on components:

@Module
@ComponentScan
class CoffeeAppModule

What is generated:

package org.koin.ksp.generated
// imports

val CoffeeAppModuleModule = module {
  val moduleInstance = org.koin.example.coffee.CoffeeAppModule()
  // definitions here ...
}
val org.koin.example.coffee.CoffeeAppModule.module : org.koin.core.module.Module get() = CoffeeAppModuleModule

We can also specify what package to scan in @ComponentScan value. Below we scan annotated components in org.koin.example.test package:

@Module
@ComponentScan("org.koin.example.test")
class CoffeeAppModule

Unmatched definitions fallback in default module

In case of using @ComponentScan, if any definition is tagged but not associated to a declared module, this definition will fallback into the defaultModule

import org.koin.ksp.generated.*

fun main() {
    startKoin {
        printLogger()
        modules(
          // generated default module
          defaultModule,
        // generated .module extension on module class
          CoffeeAppModule().module
        )
    }
    // ...
}

Class Module, mixing declarations

As with previous case:

import org.koin.ksp.generated.*

fun main() {
    startKoin {
        printLogger()
        modules(
        // generated .module extension on moduel class
          CoffeeAppModule().module
        )
    }
    // ...
}

We can combine @ComponentScan & annotated functions definition:

@Module
@ComponentScan
class CoffeeAppModule {

  @Single
  fun coffeeMaker(heater: Heater, pump: Pump) = CoffeeMaker(pump, heater)
}

We keep @Single annotations on needed components:

@Single
class CoffeeMaker(private val pump: Pump, private val heater: Heater)

@Single
class Thermosiphon(private val heater: Heater) : Pump

class ElectricHeater : Heater 

Generated content will add all definitions for module generation, like previous case.

Multiple Class Modules

Any class module tagged with @Module will be generated. Just import the module like follow:

@Module
@ComponentScan
class OtherModule

Just use it with the .module generated extension:

import org.koin.ksp.generated.*

startKoin {
  printLogger()
  modules(
    CoffeeAppModule().module,
    // new module here, with .module generated extension
    OtherModule().module
  )
}

TODO 🚧

Basic Definition Options: (In Progress)

  • Create at start

  • Qualifier (@Qualifier) => Type & Function

  • Generic for other keywords with factory (help for later Android)

  • Android Keywords

    • @ViewModel
    • @Fragment
    • @Worker

Parameter Injection (@Param)

  • Ctor
  • Fun

Property (@Property)

  • getProperty(key)

Scope Structure (@Scope)

  • @Scope on a type
  • @ScopedIn?
  • @ScopedIn for factory definitions? (visibility problem)
You might also like...
Build a compiler in Kotlin (based on the original tutorial by Jack Crenshaw)

Let's Build a Compiler Based on the original series "Let’s Build a Compiler!" by Jack Crenshaw. This is an adaptation of the original series to Kotlin

A Open GAL compiler based on OpenGAL 0.3.1

A Open GAL compiler based on OpenGAL 0.3.1

Lightweight compiler plugin intended for Kotlin/JVM library development and symbol visibility control.

Restrikt A Kotlin/JVM compiler plugin to restrict symbols access, from external project sources. This plugin offers two ways to hide symbols: An autom

A simple example of kotlim compiler plugin with FIR and IR.

A simple Kotlin compiler plugin example This Kotlin compiler plugin generates a top level class: public final class foo.bar.MyClass { fun foo(): S

A CLI tool to convert multi-module Jetpack Compose compiler metrics into beautiful HTML reports
A CLI tool to convert multi-module Jetpack Compose compiler metrics into beautiful HTML reports

A CLI tool to convert multi-module Jetpack Compose compiler metrics into beautiful HTML reports 1. What are Jetpack Compose compiler metrics? The Comp

Dependency Injection library for Compose Multiplatform, Koin wrapper.

🥥 Cokoin Injection library for Compose (Multiplatform and Jetpack), Koin wrapper. It uses @Composable functions to configure KoinContext and Scopes.

A minimal notes application in Jetpack Compose with MVVM architecture. Built with components like DataStore, Coroutines, ViewModel, LiveData, Room, Navigation-Compose, Coil, koin etc.
A minimal notes application in Jetpack Compose with MVVM architecture. Built with components like DataStore, Coroutines, ViewModel, LiveData, Room, Navigation-Compose, Coil, koin etc.

Paper - A Minimal Notes App A minimal notes application in Jetpack Compose with MVVM architecture. Built with components like DataStore, Coroutines, V

KmMScientists is a Kotlin multiplatform app with swift ui, jetpack compose, koin and realm
KmMScientists is a Kotlin multiplatform app with swift ui, jetpack compose, koin and realm

KmMScientists KmMScientists is a Kotlin multiplatform app built with swift ui, jetpack compose, koin and realm. Whats Shared? Local Data Persistence w

🍭 GithubSearchKMM - Github Repos Search - Android - iOS - Kotlin Multiplatform Mobile using Jetpack Compose, SwiftUI, FlowRedux, Coroutines Flow, Dagger Hilt, Koin Dependency Injection, shared KMP ViewModel, Clean Architecture
🍭 GithubSearchKMM - Github Repos Search - Android - iOS - Kotlin Multiplatform Mobile using Jetpack Compose, SwiftUI, FlowRedux, Coroutines Flow, Dagger Hilt, Koin Dependency Injection, shared KMP ViewModel, Clean Architecture

GithubSearchKMM Github Repos Search - Kotlin Multiplatform Mobile using Jetpack Compose, SwiftUI, FlowRedux, Coroutines Flow, Dagger Hilt, Koin Depend

Owner
insert-koin.io
All about Koin projects
insert-koin.io
A minecraft modification based on Fabric with Yarn Mappings, developed for the newest version of the sandbox game Minecraft.

JupiterClient A minecraft modification based on Fabric with Yarn Mappings, developed for the newest version of the sandbox game Minecraft. Building th

Cedric H. 1 Jun 27, 2022
This repository is part of a Uni-Project to write a complete Compiler for a subset of Java.

Compiler This repository is part of a Uni-Project to write a complete Compiler for a subset of Java. Features error recovery using context sensitive a

null 3 Jan 10, 2022
Clean Code and Reactive Programming PlayGround for Bangkit 2021

Clean Code and Reactive Programming PlayGround for Bangkit 2021 Hello! This repo contains the IntelliJ project that I use to present my talk, "Clean A

raditya gumay 3 May 16, 2021
Playground for learning Kotlin Multiplatform Mobile

This is a playground for learning KMP (KMM Plugin for android studio). Requirements Android Studio Canary 8 Architecture Thanks https://twitter.com/jo

Mitch Tabian 111 Dec 27, 2022
Small Kafka Playground to play around with Test Containers, and KotlinX Coroutines bindings while reading Kafka Definite Guide V2

KafkaPlayground Small playground where I'm playing around with Kafka in Kotlin and the Kafka SDK whilst reading the Kafka book Definite Guide from Con

Simon Vergauwen 34 Dec 30, 2022
Learning Playground - Kotlin Coroutines

Coroutines Kotlin Playground Coroutines Learning Playground Colaborator Very ope

Faisal Amir 2 Mar 12, 2022
An experimental tool for building console UI in Kotlin using the Jetpack Compose compiler/runtime

An experimental tool for building console UI in Kotlin using the Jetpack Compose compiler/runtime

Jake Wharton 1.4k Dec 28, 2022
A Kotlin compiler plugin that allows Java callers to pass in null for default parameters

kotlin-null-defaults (Compiler plugin) (Gradle Plugin) ( Currently pending approval) A Kotlin compiler plugin that allows Java callers to pass in null

Youssef Shoaib 7 Oct 14, 2022
An annotation and Kotlin compiler plugin for enforcing a when statement is exhaustive

An annotation and Kotlin compiler plugin for enforcing a when statement is exhaustive

Cash App 468 Jan 4, 2023
A composite Github Action to execute the Kotlin Script with compiler plugin and dependency caching!

Kotlin Script Github Action Kotlin can also be used as a scripting language, which is more safer, concise, and fun to write than bash or python. Githu

Suresh 9 Nov 28, 2022