Unable to `modify` nested case class
See original GitHub issueGiven the following simple example:
package diffxsandbox
import com.softwaremill.diffx.generic.auto._
import com.softwaremill.diffx.Diff
case class Address(house: Int, street: String)
case class Person(name: String, address: Address) {
def diff(other: Person) = {
val add = Diff.summon[Address]
val d =
Diff.summon[Person]
.modify(_.address)
.setTo(add)
d.apply(this, other)
}
}
object diffxdemo extends App {
val a1 = Address(123, "Robin St.")
val a2 = Address(456, "Robin St.")
val p1 = Person("Mason", a1)
val p2 = Person("Mason", a2)
println {
p1.diff(p2).isIdentical
}
}
we are receiving the following stack trace
error] java.lang.ClassCastException: class java.lang.Integer cannot be cast to class scala.Product (java.lang.Integer is in module java.base of loader 'bootstrap'; scala.Product is in unnamed module of loader sbt.internal.ScalaLibraryClassLoader @616527ba)
[error] at magnolia1.ReadOnlyParam$$anon$3.dereference(interface.scala:213)
[error] at com.softwaremill.diffx.generic.DiffMagnoliaDerivation.$anonfun$join$3(DiffMagnoliaDerivation.scala:19)
[error] at scala.collection.immutable.ArraySeq.map(ArraySeq.scala:75)
[error] at scala.collection.immutable.ArraySeq.map(ArraySeq.scala:35)
[error] at com.softwaremill.diffx.generic.DiffMagnoliaDerivation.$anonfun$join$2(DiffMagnoliaDerivation.scala:14)
[error] at com.softwaremill.diffx.DiffxSupport.nullGuard(DiffxSupport.scala:20)
[error] at com.softwaremill.diffx.DiffxSupport.nullGuard$(DiffxSupport.scala:14)
<elided...>
Does diffx
support modification of nested case class parameters? If I instead provide Diff.useEquals[Address]
instead of the automically derived instance, the example runs successfully.
Issue Analytics
- State:
- Created a year ago
- Comments:9 (1 by maintainers)
Top Results From Across the Web
How to modify this nested case classes with "Seq" fields?
I'm looking for a way to improve it, and can't find one. Even tried Monocle, but can't apply it to this problem. Is...
Read more >Quicklens: modify deeply nested case class fields
TL;DR: Quicklens: modify deeply nested fields in case classes, e.g.: modify(person)(_.address.street.name). using(_. toUpperCase) .
Read more >Avoid nesting case classes · Issue #4 - GitHub
It is tempting, but you should almost never define nested case classes inside another object/class because it messes with serialization.
Read more >circe/circe - Gitter
I managed to fix it. The lesson was: make sure all the nested case class decoders are in scope.
Read more >Difference Between Class and Case Class in Scala - Baeldung
There's another limitation we need to know when designing our code with case classes: a case class can't inherit from another case class....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
We clearly miss some test cases for those. Thanks I will look into that.
I released another fix as 0.8.2 It should solve your problem but that is not a complete solution to that issue as there are still some edge cases where it will not work. I have an idea how to fix it completely but I need to rethink how to approach it.