scalalandio / chimney

Scala library for boilerplate-free, type-safe data transformations
https://chimney.readthedocs.io
Apache License 2.0
1.16k stars 91 forks source link

Patching from another instance of the same case class #57

Open rpiaggio opened 6 years ago

rpiaggio commented 6 years ago

Thank you for this great library! It's incredibly useful together with Diode+React.

Here's a suggestion, which I think naturally falls within the domain of chimney: Being able to patch a case class instance from another instance, specifying the fields to patch. It can be seen as nicer syntax for .copy(...) specifying override fields from the other instance.

For example:

user.patchFrom(anotherUser, _.email, _.phone, _.address)

which would be equivalent to:

user.copy(email = anotherUser.email, phone = anotherUser.phone, address = anotherUser.address)

Thank you!

krzemin commented 6 years ago

Sounds good 👍

rpiaggio commented 6 years ago

Thank you @krzemin. Upon further thought, the patcher could be abstracted away for reusability (or reusability of the code that applies it):

val patcher = PatcherFor[User](_.email, _.phone, _.address)
user.patchFrom(patcher)(anotherUser)

(Might make sense to curry the application too).

MateuszKubuszok commented 4 months ago

Depends on #538 which in turns depends on #115.