Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: enable UUID field overrides with patch #687

Merged
merged 3 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ private[compiletime] trait TransformProductToProductRuleModule { this: Derivatio
def expand[From, To](implicit ctx: TransformationContext[From, To]): DerivationResult[Rule.ExpansionResult[To]] =
// From is checked after To, because extraction always succeeds
(Type[To], Type[From]) match {
case (l, r) if r <:< l && ctx.config.areLocalFlagsAndOverridesEmpty =>
DerivationResult.attemptNextRuleBecause(
s"Type ${Type.prettyPrint[From]} <:< ${Type.prettyPrint[To]}. Falling back to TransformSubtypesRule"
hughsimpson marked this conversation as resolved.
Show resolved Hide resolved
)
case (HasCustomConstructor(constructorOverride), Product.Extraction(fromExtractors)) =>
mapOverridesAndExtractorsToConstructorArguments[From, To](fromExtractors, constructorOverride)
case (Product.Constructor(parameters, constructor), Product.Extraction(fromExtractors)) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,30 @@ class PatcherProductSpec extends ChimneySpec {
foo.using(bar).patch ==> Foo(10, "", 10.0)
}

test(
"""patch an product type object with a product type patch object containing a non-self-transformable pojo"""
) {
object UUIDish {
def randomUUID(): UUIDish = {
val ng = scala.util.Random
new UUIDish(ng.nextLong(), ng.nextLong())
}
}
class UUIDish(mostSigBits: Long, leastSigBits: Long) {
override def toString: String = s"$mostSigBits::$leastSigBits"
}
case class Foo(id: Int, externalReference: UUIDish)
case class Bar(externalReference: UUIDish)

val uuid1 = UUIDish.randomUUID()
val uuid2 = UUIDish.randomUUID()
val foo = Foo(42, uuid1)
val bar = Bar(uuid2)

foo.using(bar).patch ==> Foo(42, uuid2)
foo.patchUsing(bar) ==> Foo(42, uuid2)
}

test(
"patch a product with non-Option field with a product type patch object with all Option fields, applying value from Some and leaving original value in None"
) {
Expand Down