question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to `modify` nested case class

See original GitHub issue

Given 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:open
  • Created a year ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
ghostbuster91commented, Oct 25, 2022

We clearly miss some test cases for those. Thanks I will look into that.

0reactions
ghostbuster91commented, Nov 6, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found