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

Commit b2a1b6f

Browse files
committed
Improve objectMatcher
1 parent a88246a commit b2a1b6f

File tree

5 files changed

+30
-8
lines changed

5 files changed

+30
-8
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ object ObjectMatcher extends LowPriorityObjectMatcher {
1414
ObjectMatcher[U].isSameObject(f(left), f(right))
1515

1616
def byValue[K, V: ObjectMatcher]: ObjectMatcher[(K, V)] = ObjectMatcher.by[(K, V), V](_._2)
17+
def byValue[K, V, U: ObjectMatcher](f: V => U): ObjectMatcher[(K, V)] =
18+
ObjectMatcher.by[(K, V), V](_._2)(ObjectMatcher.by[V, U](f))
1719

1820
implicit def optionMatcher[T: ObjectMatcher]: ObjectMatcher[Option[T]] = (left: Option[T], right: Option[T]) => {
1921
(left, right) match {

docs-sources/usage/replacing.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,14 @@ compare(Person(23, 60), Person(23, 62))
2727
```
2828

2929
In fact, replacement is so powerful that ignoring is implemented as a replacement
30-
with the `Diff.ignore` instance.
30+
with the `Diff.ignore` instance.
31+
32+
You can use the same mechanism to set particular object matcher for given nested collection in the hierarchy.
33+
Depending, whether it is list, set or map a respective method needs to be called:
34+
```scala mdoc:silent
35+
case class Organization(peopleList: List[Person], peopleSet: Set[Person], peopleMap: Map[String, Person])
36+
implicit val diffOrg: Derived[Diff[Organization]] = Diff.derived[Organization]
37+
.modify(_.peopleList).withListMatcher[Person](ObjectMatcher.byValue(_.age))
38+
.modify(_.peopleSet).withSetMatcher[Person](ObjectMatcher.by(_.age))
39+
.modify(_.peopleMap).withMapMatcher[String,Person](ObjectMatcher.byValue(_.age))
40+
```

docs-sources/usage/sequences.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ import com.softwaremill.diffx._
5454
import com.softwaremill.diffx.generic.auto._
5555
case class Person(id: String, name: String)
5656

57-
implicit val personMatcher: ObjectMatcher[(Int, Person)] =
58-
ObjectMatcher.byValue(ObjectMatcher.by(_.id))
57+
implicit val personMatcher: ObjectMatcher[(Int, Person)] = ObjectMatcher.byValue(_.id)
5958
val bob = Person("1","Bob")
6059
val alice = Person("2","Alice")
6160
```
6261
```scala mdoc
6362
compare(List(bob, alice), List(alice, bob))
6463
```
6564

66-
*Note: `ObjectMatcher` can be also passed explicitly, either upon creation or during modification*
65+
*Note: `ObjectMatcher` can be also passed explicitly, either upon creation or during modification*
66+
*See [replacing](replacing.md) for details.*

generated-docs/out/usage/replacing.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,14 @@ compare(Person(23, 60), Person(23, 62))
2828
```
2929

3030
In fact, replacement is so powerful that ignoring is implemented as a replacement
31-
with the `Diff.ignore` instance.
31+
with the `Diff.ignore` instance.
32+
33+
You can use the same mechanism to set particular object matcher for given nested collection in the hierarchy.
34+
Depending, whether it is list, set or map a respective method needs to be called:
35+
```scala
36+
case class Organization(peopleList: List[Person], peopleSet: Set[Person], peopleMap: Map[String, Person])
37+
implicit val diffOrg: Derived[Diff[Organization]] = Diff.derived[Organization]
38+
.modify(_.peopleList).withListMatcher[Person](ObjectMatcher.byValue(_.age))
39+
.modify(_.peopleSet).withSetMatcher[Person](ObjectMatcher.by(_.age))
40+
.modify(_.peopleMap).withMapMatcher[String,Person](ObjectMatcher.byValue(_.age))
41+
```

generated-docs/out/usage/sequences.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ import com.softwaremill.diffx._
6767
import com.softwaremill.diffx.generic.auto._
6868
case class Person(id: String, name: String)
6969

70-
implicit val personMatcher: ObjectMatcher[(Int, Person)] =
71-
ObjectMatcher.byValue(ObjectMatcher.by(_.id))
70+
implicit val personMatcher: ObjectMatcher[(Int, Person)] = ObjectMatcher.byValue(_.id)
7271
val bob = Person("1","Bob")
7372
val alice = Person("2","Alice")
7473
```
@@ -79,4 +78,5 @@ compare(List(bob, alice), List(alice, bob))
7978
// )
8079
```
8180

82-
*Note: `ObjectMatcher` can be also passed explicitly, either upon creation or during modification*
81+
*Note: `ObjectMatcher` can be also passed explicitly, either upon creation or during modification*
82+
*See [replacing](replacing.md) for details.*

0 commit comments

Comments
 (0)