Coder Social home page Coder Social logo

petertrr / merge-data-class Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 125 KB

Kotlin Symbol Processor to construct data classes from incomplete sources while preserving nullability cheks

License: MIT License

Kotlin 100.00%
data-class kotlin ksp type-safety

merge-data-class's Introduction

merge-data-class KSP processor

This is a Kotlin Symbol Processor implementation that is intended to ease some tasks related to data classes by generating boilerplate code that helps to preserve type-safe nullability when working with incomplete data sources.

Sometimes there are cases when data comes from different sources, e.g. some fields are read from configuration file and others are set by user on the command line. After application startup those two sets of values sum up into a data class where all fields have to be present, i.e. they are non-nullable in terms of Kotlin type system. So, to construct such a class one might want to read all values manually, or create a special logic for iterating over class properties etc. This approach however leads to a lot of boilerplate.

Or there might be another approach - to make properties nullable, construct data class instances from all data sources and then merge them. However, this passes nullable properties down the whole application and introduces a huge number of unnecessary !! all over the code base.

merge-data-class serves as a pretty simple code generation solution for this case. Based on a data class it would generate a class with nullable properties and a merge method, which would return an instance of the original class while also performing nullability checks.

Consider the code below:

@BuildFromPartial
data class Foo(
    val field1: Type1,
    val field2: Type2?,
)

merge-data-class will generate the following code:

data class FooPartial(
    val field1: Type1?,
    val field2: Type2?,
) {
    fun merge(other: FooPartial): Foo {
        return Foo(
            field1 ?: other.field1,
            field2 ?: other.field2
        )
    }
}

Using it in a gradle project

This plugin can be used as any other KSP processor: apply the KSP plugin and add required dependencies:

plugins{
    kotlin("ksp") version "1.6.21"
}

## Using it in a KMP project
As described in [Kotlin docs](https://kotlinlang.org/docs/ksp-multiplatform.html), there are some differences
in using KSP in Multiplatform proejct over a single-platform project.
kotlin
plugins{
    id("com.google.devtools.ksp") version <ksp version>
}

kotlin {
    sourceSets {
        commonMain {
          implementation("io.github.petertrr.ksp:merge-data-class-annotations:0.1.0")
        }
    }
}

dependencies {
    "ksp"("io.github.petertrr.ksp:merge-data-class-annotations:0.1.0")
    "kspCommonMainMetadata"("io.github.petertrr.ksp:merge-data-class-ksp:0.1.0")
}
  
dependencies {
    compileOnly("io.github.petertrr:merge-data-class-annotations:0.1.0")
    ksp("io.github.petertrr:merge-data-class-ksp:0.1.0")
}

merge-data-class's People

Contributors

petertrr avatar

Stargazers

 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.