Skip to content

Commit

Permalink
Fix propageting parent diff modificatino to its children
Browse files Browse the repository at this point in the history
This closes #418
  • Loading branch information
ghostbuster91 committed Oct 25, 2022
1 parent 58091b7 commit 1ea299f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,16 @@ trait DiffMagnoliaDerivation {
val lType = ctx.split(left)(a => a)
val rType = ctx.split(right)(a => a)
if (lType == rType) {
lType.typeclass(
val leftTypeClass = lType.typeclass
val contextPath = ModifyPath.Subtype(lType.typeName.owner, lType.typeName.short)
val modifyFromOverride = context
.getOverride(contextPath)
.map(_.asInstanceOf[leftTypeClass.type => leftTypeClass.type])
.getOrElse(identity[leftTypeClass.type] _)
modifyFromOverride(leftTypeClass)(
lType.cast(left),
lType.cast(right),
context.getNextStep(ModifyPath.Subtype(lType.typeName.owner, lType.typeName.short)).merge(context)
context.getNextStep(contextPath).merge(context)
)
} else {
DiffResultValue(lType.typeName.full, rType.typeName.full)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,16 @@ trait DiffMagnoliaDerivation extends Derivation[Diff] {
val lType = ctx.choose(left)(a => a)
val rType = ctx.choose(right)(a => a)
if (lType.typeInfo == rType.typeInfo) {
lType.typeclass(
val leftTypeClass = lType.typeclass
val contextPath = ModifyPath.Subtype(lType.typeInfo.owner, lType.typeInfo.short)
val modifyFromOverride = context
.getOverride(contextPath)
.map(_.asInstanceOf[leftTypeClass.type => leftTypeClass.type])
.getOrElse(identity[leftTypeClass.type] _)
modifyFromOverride(leftTypeClass)(
lType.cast(left),
lType.cast(right),
context.getNextStep(ModifyPath.Subtype(lType.typeInfo.owner, lType.typeInfo.short)).merge(context)
context.getNextStep(contextPath).merge(context)
)
} else {
DiffResultValue(lType.typeInfo.full, rType.typeInfo.full)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ case class DiffContext(

private def treeOverride[T](nextPath: ModifyPath, tree: Tree[T]) = {
tree match {
case Tree.Leaf(v) => Some(v)
case Tree.Leaf(v) => None
case Tree.Node(tries) => getOverrideFromNode(nextPath, tries)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.scalatest.matchers.should.Matchers

import java.time.Instant
import java.util.UUID
import scala.collection.immutable.ListMap

class DiffModifyIntegrationTest extends AnyFlatSpec with Matchers with AutoDerivation {
val instant: Instant = Instant.now()
Expand Down Expand Up @@ -293,4 +294,30 @@ class DiffModifyIntegrationTest extends AnyFlatSpec with Matchers with AutoDeriv
val d2 = Diff[Family].modify(_.second.name).ignore.modify(_.first).ignore
compare(f1, f2)(d2).isIdentical shouldBe true
}

it should "allow to set custom diff to a nested case class field" in {
case class Address(house: Int, street: String)
case class Person(name: String, address: Address)

val add = Diff.summon[Address]
val d = Diff
.summon[Person]
.modify(_.address)
.setTo(add)

val a1 = Address(123, "Robin St.")
val a2 = Address(456, "Robin St.")
val p1 = Person("Mason", a1)
val p2 = Person("Mason", a2)
d(p1, p2) shouldBe DiffResultObject(
"Person",
ListMap(
"name" -> IdenticalValue("Mason"),
"address" -> DiffResultObject(
"Address",
ListMap("house" -> DiffResultValue(123, 456), "street" -> IdenticalValue("Robin St."))
)
)
)
}
}

0 comments on commit 1ea299f

Please sign in to comment.