Skip to content

Commit

Permalink
Fix rename edge cases broken by 1.6.0 refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
MateuszKubuszok committed Jan 29, 2025
1 parent 3e7a40f commit 96510a7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ private[compiletime] trait Configurations { this: Derivation =>
// checking if there might be some relevant overrides for current/nested values
case _: TransformerOverride.ForField | _: TransformerOverride.ForSubtype =>
val newSidedPath = sidedPath.drop(fromPath, toPath).view.filterNot(_.path == Path.Root)
newSidedPath.map(_ -> runtimeOverride)
newSidedPath.map(_ -> runtimeOverride).filterNot(pathCannotBeUsedButBlocksRuleForEmptyOverrides)
// Fallbacks are always matched at "_" Path, and dropped _manually_ only when going inward,
// because we have to update their inner value
case f: TransformerOverride.ForFallback =>
Expand All @@ -607,6 +607,12 @@ private[compiletime] trait Configurations { this: Derivation =>
preventImplicitSummoningForTypes = None
)

// I haven't found a more "principled" way to achieve this, that wouldn't break half the tests
private lazy val pathCannotBeUsedButBlocksRuleForEmptyOverrides: ((SidedPath, TransformerOverride)) => Boolean = {
case (TargetPath(Path.AtSubtype(_, Path.Root)), _: TransformerOverride.Renamed) => true
case _ => false
}

override def toString: String = {
val runtimeOverridesString =
runtimeOverrides.map { case (path, runtimeOverride) => s"$path -> $runtimeOverride" }.mkString(", ")
Expand Down
15 changes: 15 additions & 0 deletions chimney/src/test/scala/io/scalaland/chimney/IssuesSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -827,4 +827,19 @@ class IssuesSpec extends ChimneySpec {
Foo(1, "value").transformIntoPartial[Bar].asOption.get ==> Bar(20, "value")
Foo(1, "value").patchUsing(Baz(30)) ==> Foo(30, "patched")
}

test("fix issue #683") {
import Issue683.*

(Proto.Foo(10): Proto)
.intoPartial[Domain]
.withSealedSubtypeRenamed[Proto.Foo, Domain.Foo1]
.transform
.asOption ==> Some(Domain.Foo1(10))
(Proto.Empty: Proto)
.intoPartial[Domain]
.withSealedSubtypeRenamed[Proto.Foo, Domain.Foo1]
.transform
.asOption ==> None
}
}
16 changes: 16 additions & 0 deletions chimney/src/test/scala/io/scalaland/chimney/fixtures/Issues.scala
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,19 @@ object Issue479 {
case class Impl(value: String) extends Target
}
}

object Issue683 {
sealed trait Proto extends Product with Serializable
object Proto {
case class Foo(a: Int) extends Proto
case object Empty extends Proto
}

sealed trait Domain extends Product with Serializable
object Domain {
case class Foo1(a: Int) extends Domain
}

implicit def handleEmpty[To]: PartialTransformer[Proto.Empty.type, To] =
(_: Proto.Empty.type, _: Boolean) => partial.Result.fromEmpty[To]
}

0 comments on commit 96510a7

Please sign in to comment.