Skip to content
This repository was archived by the owner on Jan 24, 2025. It is now read-only.

Commit 7c5f410

Browse files
Merge pull request #419 from softwaremill/fix/i418
Fix propagating parent diff modificatino to its children
2 parents 58091b7 + 58b8e0e commit 7c5f410

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

core/src/main/scala-2/com/softwaremill/diffx/generic/DiffMagnoliaDerivation.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,16 @@ trait DiffMagnoliaDerivation {
3030
val lType = ctx.split(left)(a => a)
3131
val rType = ctx.split(right)(a => a)
3232
if (lType == rType) {
33-
lType.typeclass(
33+
val leftTypeClass = lType.typeclass
34+
val contextPath = ModifyPath.Subtype(lType.typeName.owner, lType.typeName.short)
35+
val modifyFromOverride = context
36+
.getOverride(contextPath)
37+
.map(_.asInstanceOf[leftTypeClass.type => leftTypeClass.type])
38+
.getOrElse(identity[leftTypeClass.type] _)
39+
modifyFromOverride(leftTypeClass)(
3440
lType.cast(left),
3541
lType.cast(right),
36-
context.getNextStep(ModifyPath.Subtype(lType.typeName.owner, lType.typeName.short)).merge(context)
42+
context.getNextStep(contextPath).merge(context)
3743
)
3844
} else {
3945
DiffResultValue(lType.typeName.full, rType.typeName.full)

core/src/main/scala-3/com/softwaremill/diffx/generic/DiffMagnoliaDerivation.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,16 @@ trait DiffMagnoliaDerivation extends Derivation[Diff] {
5050
val lType = ctx.choose(left)(a => a)
5151
val rType = ctx.choose(right)(a => a)
5252
if (lType.typeInfo == rType.typeInfo) {
53-
lType.typeclass(
53+
val leftTypeClass = lType.typeclass
54+
val contextPath = ModifyPath.Subtype(lType.typeInfo.owner, lType.typeInfo.short)
55+
val modifyFromOverride = context
56+
.getOverride(contextPath)
57+
.map(_.asInstanceOf[leftTypeClass.type => leftTypeClass.type])
58+
.getOrElse(identity[leftTypeClass.type] _)
59+
modifyFromOverride(leftTypeClass)(
5460
lType.cast(left),
5561
lType.cast(right),
56-
context.getNextStep(ModifyPath.Subtype(lType.typeInfo.owner, lType.typeInfo.short)).merge(context)
62+
context.getNextStep(contextPath).merge(context)
5763
)
5864
} else {
5965
DiffResultValue(lType.typeInfo.full, rType.typeInfo.full)

core/src/main/scala/com/softwaremill/diffx/DiffContext.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ case class DiffContext(
2222

2323
private def treeOverride[T](nextPath: ModifyPath, tree: Tree[T]) = {
2424
tree match {
25-
case Tree.Leaf(v) => Some(v)
25+
case Tree.Leaf(v) => None
2626
case Tree.Node(tries) => getOverrideFromNode(nextPath, tries)
2727
}
2828
}

core/src/test/scala/com/softwaremill/diffx/test/DiffModifyIntegrationTest.scala

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import org.scalatest.matchers.should.Matchers
77

88
import java.time.Instant
99
import java.util.UUID
10+
import scala.collection.immutable.ListMap
1011

1112
class DiffModifyIntegrationTest extends AnyFlatSpec with Matchers with AutoDerivation {
1213
val instant: Instant = Instant.now()
@@ -293,4 +294,30 @@ class DiffModifyIntegrationTest extends AnyFlatSpec with Matchers with AutoDeriv
293294
val d2 = Diff[Family].modify(_.second.name).ignore.modify(_.first).ignore
294295
compare(f1, f2)(d2).isIdentical shouldBe true
295296
}
297+
298+
it should "allow to set custom diff to a nested case class field" in {
299+
case class Address(house: Int, street: String)
300+
case class Person(name: String, address: Address)
301+
302+
val add = Diff.summon[Address]
303+
val d = Diff
304+
.summon[Person]
305+
.modify(_.address)
306+
.setTo(add)
307+
308+
val a1 = Address(123, "Robin St.")
309+
val a2 = Address(456, "Robin St.")
310+
val p1 = Person("Mason", a1)
311+
val p2 = Person("Mason", a2)
312+
d(p1, p2) shouldBe DiffResultObject(
313+
"Person",
314+
ListMap(
315+
"name" -> IdenticalValue("Mason"),
316+
"address" -> DiffResultObject(
317+
"Address",
318+
ListMap("house" -> DiffResultValue(123, 456), "street" -> IdenticalValue("Robin St."))
319+
)
320+
)
321+
)
322+
}
296323
}

0 commit comments

Comments
 (0)