Mutator for Immutable Object

Overview

Mutator

Why Mutator?

As the immutable object becomes the default pattern of functional programming language, such as Value Object in Java, and Data Class in Kotlin, even the immutable pattern has a lot of advantages, modifying properties of deeply nested immutable objects is still not very convenient, for example:

data class Person(
    val name: String,
    val address: Address
)

data class Address(
    val nation: Nation,
    val state: String,
    val street: String
)

data class Nation(
    val name: String,
    val code: String
)

If we want to modify the Person.address.nation.code, we have to write code like this:

val person = Person(...)
// ...
val clone = person.copy(
    address = person.address.copy(
        nation = person.address.nation.copy(
            code = "New Code"
        )       
    )
)

Getting Started

  1. Add dependency into build.gradle or build.gradle.kts

    dependencies {
        implementation("io.johnsonlee:mutator:1.2.0")
    }
  2. Call the extension function mutate(...)

    val clone = person.mutate(Person::address, Address::nation, Nation::code, "New Code")

    The code above is equivalent to the following

    val clone = person.copy(
       address = person.address.copy(
           nation = person.address.nation.copy(
               code = "New Code"
           )       
       )
    )
You might also like...
TensorFlow Lite Object Detection Android Demo
TensorFlow Lite Object Detection Android Demo

GSoC Project 2021 - TensorFlow Description This repository contains the project where I contributed to the TensorFlow Team during GSoC in the year 202

Animation samples with motion layout and object animator 🦹🏻‍♀️
Animation samples with motion layout and object animator 🦹🏻‍♀️

💕 Animations 💕 Animation samples with motion layout and object animator 🦹🏻‍♀️ Car Animation with Object Animator 👀 🎀 I've created this project b

Kotlin Object Notation - Lightweight DSL to build fluid JSON trees

Kotlin Object Notation Lightweight kotlin MPP DSL for building JSON trees Setup Just drop the dependency in your commonMain sourceSet kotlin { sourc

Its a small example with Custom Employee object is sent from one Fragment to another.
Its a small example with Custom Employee object is sent from one Fragment to another.

Kotlin-SafeArgs-Custom-Object Its a small example with Custom Employee object is sent from one Fragment to another. Dependencies used // Navigation

Kotlin Android object for global applicationContext

ContextProvider Kotlin Android object for global applicationContext Usage In your Aplication class class YourApp : Application() { override fun o

Object declarations Board game basics Game

Object-declarations-Board-game-basics-Game You want to create a game and you decided to store the playing field in a PlayingField singleton. Inside th

EVMapper: Simple object mapper for Kotlin

EVMapper - Simple object mapper for Kotlin Simple mapping data class AData(val p1: Int, val p2: Int) data class BData(val p1: Int, val p2: Int) val a

Object-streams - An experimental library for dealing with legacy code

Object Streams Super experimental! no guarantees to the public API surface. At t

Based on Android + NodeMCU platform to achieve object detection and tracking, AI capabilities come from Google MLKit.
Based on Android + NodeMCU platform to achieve object detection and tracking, AI capabilities come from Google MLKit.

Based on Android + NodeMCU platform to achieve object detection and tracking, AI capabilities come from Google MLKit.

Pdf Extractor - Covert array list of object to pdf table
Pdf Extractor - Covert array list of object to pdf table

Pdf Extractor - Covert array list of object to pdf table

Demonstration of Object Pool Design Pattern using Kotlin language and Coroutine
Demonstration of Object Pool Design Pattern using Kotlin language and Coroutine

Object Pool Design Pattern with Kotlin Demonstration of Thread Safe Object Pool Design Pattern using Kotlin language and Coroutine. Abstract The objec

📭 Extension to Ktor’s routing system to add object oriented routing and much more. 💜

📭 Ktor Routing Extensions Extension to Ktor’s routing system to add object-oriented routing and much more. 💜 Why? This extension library was created

ShapeShift️ - A Kotlin library for intelligent object mapping and conversion between objects
ShapeShift️ - A Kotlin library for intelligent object mapping and conversion between objects

ShapeShift️ A Kotlin library for intelligent object mapping and conversion between objects. Documentation Installation Maven Gradle Groovy DSL Kotlin

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

A thought experiment on architecture, object-oriented programming, and composability.

Journal3 There's barely anything special about the features that Journal3 is offering, it's literally yet another journaling application. What is spec

A modular object storage framework for Kotlin multiplatform projects.

ObjectStore A modular object storage framework for Kotlin multiplatform projects. Usage ObjectStore provides a simple key/value storage interface whic

Releases(v1.0.0)
Owner
Johnson Lee
Full Stack Engineer, focus on Android application architecture, mobile infrastructure, my personal blog 👉https://johnsonlee.io
Johnson Lee
Demonstration of Object Pool Design Pattern using Kotlin language and Coroutine

Object Pool Design Pattern with Kotlin Demonstration of Thread Safe Object Pool Design Pattern using Kotlin language and Coroutine. Abstract The objec

Enes Kayıklık 7 Apr 12, 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
Kotlin Symbol Processor library to create Mutable and Immutable variants of objects.

implier Kotlin Symbol Processor plugin to create Mutable and Immutable variants of objects. Examples @ImmutableImpl @MutableImpl public interface Samp

Vadim Yaroschuk 14 Nov 8, 2022
Simplify mutating "immutable" state models

Mutekt (Pronunciation: /mjuːˈteɪt/, 'k' is silent) "Simplify mutating "immutable" state models" Generates mutable models from immutable model definiti

Shreyas Patil 179 Nov 30, 2022
Little utilities for more pleasant immutable data in Kotlin

What can KopyKat do? Mutable copy Nested mutation Nested collections Mapping copyMap copy for sealed hierarchies copy from supertypes copy for type al

KopyKat 193 Dec 19, 2022
Object-relational mapping for Android

RushOrm Object-relational mapping for Android RushOrm replaces the need for SQL by mapping java classes to SQL tables. What is the aim? The aim is to

Stuart Campbell 172 Nov 11, 2022
Helper object for injecting typeface into various text views of android.

TypefaceHelper Helper object for injecting typeface into various text views of android. Overview We can use various custom typefaces asset for any tex

Drivemode, Inc. 105 Nov 25, 2022
An Android project containing image recognition and object detection models.

An Android project containing image recognition and object detection models. Users can input images into the deep learning model by taking photos, opening photo albums, and real-time previews on the Android side. After the calculation on the Android side is completed, the model will output the prediction result and show it to the user.

null 7 Nov 27, 2022
This app using Mlkit along with the TensorFlow Lite model for object detection,

I built this app using Mlkit along with the TensorFlow Lite model for object detection, Arcore is used to place anchors to the detected objects. It's a good blend of Machine learning and Augmented reality to visualise ML information in a much better way than regular bounding boxes

Kashif Mehmood 18 Nov 10, 2022
This repo contains examples of all the functionalities of sceneform, Face filter , Object Placement etc

ARCore-Projects This repo uses sceneform 1.17.1 to demonstrate the usage of different features of arcore such as, Augmented Faces Augmented images AR

Kashif Mehmood 21 Dec 4, 2022