Getting started with patchers

Chimney supports case class patching. It is a bit different type of transformation when you hold an object of some type, but want to modify only subset of fields.

Consider the following example.

case class Email(address: String) extends AnyVal
case class Phone(number: Long) extends AnyVal

case class User(id: Int, email: Email, phone: Phone)
case class UserUpdateForm(email: String, phone: Long)

Let’s assume you want to apply update form to existing object of type User.

val user = User(10, Email(""), Phone(1234567890L))
val updateForm = UserUpdateForm("", 123123123L)

// User(10, Email(""), Phone(123123123L))

Notice that when using patchers, we rely on standard transformers’ derivation rules. In this case we used value classes in the User model, but plain values in update form. Chimney was able to derive transformers for each patched field, so it was able to successfully derive a patcher.