softwaremill / diffx

Pretty diffs for scala case classes
Apache License 2.0
342 stars 30 forks source link

Ignore structure inside Option field #443

Open gregorath opened 1 year ago

gregorath commented 1 year ago

Hello, I was trying to compare two case classes with nested optional structures but it seems that it doesn't work.

For example the following

import com.softwaremill.diffx.Diff.Typeclass
import com.softwaremill.diffx.*

case class Test(id: Int)
case class TestWrap(id: Test)
case class TestWrapWrap(id: Option[TestWrap])

val t1 = TestWrapWrap(Option(TestWrap(Test(1))))
val t2 = TestWrapWrap(Option(TestWrap(Test(2))))

given Typeclass[Test] = Diff.derived
given Typeclass[TestWrap] = Diff.derived
given Typeclass[TestWrapWrap] = Diff.derived

val diff = Diff[TestWrapWrap].ignore(_.id.each.id)
val diffResult = diff(t1, t2)

should pass but actually fails with:

TestWrapWrap(
                id: TestWrap(
                    id: Test(
                        id: 1 -> 2)))

if we set

val diff = Diff[TestWrapWrap].ignore(_.id)

then it ignores the optional id and it returns that they are identical