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

Commit 043a1c5

Browse files
committed
Fix eachLeft/eachRight
1 parent 9cb8b04 commit 043a1c5

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,13 @@ object ModifyMacro {
9797
case q"($arg) => $pathBody " => collectPathElements(pathBody, Nil)
9898
case _ => c.abort(c.enclosingPosition, s"$ShapeInfo, got: ${path.tree}")
9999
}
100-
101100
c.Expr[List[String]](
102-
q"(${pathEls.collect { case TermPathElement(c) =>
103-
c.decodedName.toString
101+
q"(${pathEls.collect {
102+
case TermPathElement(c) => c.decodedName.toString
103+
case FunctorPathElement(_, method, _ @_*) if method.decodedName.toString == "eachLeft" =>
104+
method.decodedName.toString
105+
case FunctorPathElement(_, method, _ @_*) if method.decodedName.toString == "eachRight" =>
106+
method.decodedName.toString
104107
}})"
105108
)
106109
}

core/src/main/scala/com/softwaremill/diffx/instances/DiffForEither.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ private[diffx] class DiffForEither[L, R](ld: Diff[L], rd: Diff[R]) extends Diff[
99
context: DiffContext
1010
): DiffResult = {
1111
(left, right) match {
12-
case (Left(v1), Left(v2)) => ld.apply(v1, v2, context)
13-
case (Right(v1), Right(v2)) => rd.apply(v1, v2, context)
12+
case (Left(v1), Left(v2)) => ld.apply(v1, v2, context.getNextStep("eachLeft"))
13+
case (Right(v1), Right(v2)) => rd.apply(v1, v2, context.getNextStep("eachRight"))
1414
case (v1, v2) => DiffResultValue(v1, v2)
1515
}
1616
}

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,35 @@ class DiffModifyIntegrationTest extends AnyFlatSpec with Matchers {
4747
compare(o1, o2) shouldBe Identical(Organization(List(p1, p2)))
4848
}
4949

50+
it should "ignore only on right" in {
51+
case class Wrapper(e: Either[Person, Person])
52+
val e1 = Wrapper(Right(p1))
53+
val e2 = Wrapper(Right(p1.copy(name = p1.name + "_modified")))
54+
55+
implicit val wrapperDiff: Diff[Wrapper] = Derived[Diff[Wrapper]].ignore(_.e.eachRight.name)
56+
57+
compare(e1, e2) shouldBe Identical(e1)
58+
59+
val e3 = Wrapper(Left(p1))
60+
val e4 = Wrapper(Left(p1.copy(name = p1.name + "_modified")))
61+
62+
compare(e3, e4) should not be an[Identical[_]]
63+
}
64+
65+
it should "ignore only on left" in {
66+
case class Wrapper(e: Either[Person, Person])
67+
val e1 = Wrapper(Right(p1))
68+
val e2 = Wrapper(Right(p1.copy(name = p1.name + "_modified")))
69+
70+
implicit val wrapperDiff: Diff[Wrapper] = Derived[Diff[Wrapper]].ignore(_.e.eachLeft.name)
71+
72+
compare(e1, e2) should not be an[Identical[_]]
73+
val e3 = Wrapper(Left(p1))
74+
val e4 = Wrapper(Left(p1.copy(name = p1.name + "_modified")))
75+
76+
compare(e3, e4) shouldBe an[Identical[_]]
77+
}
78+
5079
it should "match map entries by values" in {
5180
implicit val lookupDiff: Diff[MyLookup] = Derived[Diff[MyLookup]]
5281
.modify(_.map)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ class ModifyMacroTest extends AnyFlatSpec with Matchers {
1414
}
1515

1616
it should "ignore fields in product wrapped with either" in {
17-
ModifyMacro.modifiedFromPath[Either[Person, Person], String](_.eachRight.name) shouldBe List("name")
18-
ModifyMacro.modifiedFromPath[Either[Person, Person], String](_.eachLeft.name) shouldBe List("name")
17+
ModifyMacro.modifiedFromPath[Either[Person, Person], String](_.eachRight.name) shouldBe List("eachRight", "name")
18+
ModifyMacro.modifiedFromPath[Either[Person, Person], String](_.eachLeft.name) shouldBe List("eachLeft", "name")
1919
}
2020

2121
it should "ignore fields in product wrapped with option" in {

0 commit comments

Comments
 (0)