Git Product home page Git Product logo

kopybuilder's Introduction

KopyBuilder

This is a compiler plugin that generates a builder class for Kotlin's data class. You can get and put properties from the builder, similar to a Map, and finally build a new instance.

Usage

Gradle Setup

Currently, it supports Kotlin versions 1.8 and 1.9.

plugins {
    id("io.github.windedge.kopybuilder") version "$version"
}

Code Generation

For example, consider the following data class:

@KopyBuilder
data class User(
    val name: String,
    val email: String?
)

It will generate code like this:

public class UserBuilder: io.github.windedge.copybuilder.CopyBuilder<User> {
    override fun `get`(key: String): Any? {
        //...
    }

    override fun put(key: String, `value`: Any?) {
        //...
    }

    override fun build(): User = User(name = ..., email = ... )
}

fun User.toCopyBuilder(): CopyBuilder<User> = ...
fun User.copyBuild(initialize: CopyBuilder<User>.() -> Unit): User { /*...*/ }

You can use it as follows:

val user = User(...)
val builder = user.toCopyBuilder()
builder.apply {
    put("name", ...)
    put("email", ...)
}
val newUser = builder.build()

// Or build with copyBuild directly
val newUser = user.copyBuild {
    put("name", ...)
}

You can even use it in a reflection way, making it possible to cooperate with 3rd party libraries:

import io.github.windedge.copybuilder.CopyBuilderHost

if (CopyBuilderHost::class.isInstance(user)) {
    @Suppress("CAST_NEVER_SUCCEEDS")
    val host = user as CopyBuilderHost<User>
    val newUser = host.copyBuild {
        put("name", "Max")
    }
}

License

This project is licensed under the MIT License. Please refer to the LICENSE file for details.

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.