If I have for example an entity:
data class SampleEntity(
val a: Int,
val b: Int,
val c: Int,
val d: Int){
fun update(form: SampleEntityUpdateForm): SampleEntity{
...
return this SampleEntity updated with form
}
}
and a form to update this entity:
data class SampleEntityUpdateForm(
val a: Int?,
val b: Int?,
val c: Int?,
val d: Int?
)
What is the best way to update the entity with this form (i.e. implementing SampleEntity.update(...)), leaving the properties that are null in SampleEntityUpdateForm as they were in the original entity, with minimum boiler plate.
So for example:
val sampleEntityInst = SampleEntity(1,2,3,4)
val sampleEntityUpdateForm(b = 20, d = 40)
val updatedSampleEntityInst = sampleEntityInst.update(sampleEntityUpdateForm)
where updatedSampleEntityInst will equal SampleEntity(1,20,3,40).
obviosly I could implement SampleEntity.update as:
fun update(form: SampleEntityUpdateForm) = SampleEntity(
a = form.a ?: this.a,
b = form.b ?: this.b,
c = form.c ?: this.c,
d = form.d ?: this.d,
)
But this has quite a bit of boiler plate/repetition. Is there something already made to allow doing this with less boiler plate/repetition?