Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix propagating parent diff modificatino to its children #419

Merged
merged 1 commit into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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."))
)
)
)
}
}