Skip to content

Commit

Permalink
Make sure that flag overrides are ignored when deciding if AnyVal rul…
Browse files Browse the repository at this point in the history
…e should be skipped
  • Loading branch information
MateuszKubuszok committed Nov 5, 2023
1 parent 5994f20 commit bcb1808
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,18 @@ private[compiletime] trait Configurations { this: Derivation =>
Type[SomeFrom] =:= Type[From] && Type[SomeTo] =:= Type[To]
}

def areOverridesEmptyForCurrent[From: Type, To: Type]: Boolean =
!instanceFlagOverridden && fieldOverrides.isEmpty && filterOverridesForCoproduct { (someFrom, someTo) =>
def areFlagOverridesEmptyForCurrent[From: Type, To: Type]: Boolean =
!instanceFlagOverridden

def areValueOverridesEmptyForCurrent[From: Type, To: Type]: Boolean =
fieldOverrides.isEmpty && filterOverridesForCoproduct { (someFrom, someTo) =>
import someFrom.Underlying as SomeFrom, someTo.Underlying as SomeTo
Type[SomeFrom] <:< Type[From] && Type[To] <:< Type[SomeTo]
}.isEmpty

def areOverridesEmptyForCurrent[From: Type, To: Type]: Boolean =
areFlagOverridesEmptyForCurrent[From, To] && areValueOverridesEmptyForCurrent[From, To]

def filterOverridesForField(nameFilter: String => Boolean): Map[String, RuntimeFieldOverride] =
fieldOverrides.view.collect {
case (FieldPath.CurrentField(fieldName), fieldOverride) if nameFilter(fieldName) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ private[compiletime] trait TransformTypeToValueClassRuleModule {
def expand[From, To](implicit ctx: TransformationContext[From, To]): DerivationResult[Rule.ExpansionResult[To]] =
Type[To] match {
case ValueClassType(to2) =>
if (ctx.config.areOverridesEmptyForCurrent[From, To]) {
if (ctx.config.areValueOverridesEmptyForCurrent[From, To]) {
import to2.{Underlying as InnerTo, value as valueTo}
transformToInnerToAndWrap[From, To, InnerTo](valueTo)
} else DerivationResult.attemptNextRuleBecause("Configuration has defined overrides")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import io.scalaland.chimney.internal.compiletime.DerivationResult
import io.scalaland.chimney.internal.compiletime.derivation.transformer.Derivation

private[compiletime] trait TransformValueClassToTypeRuleModule {
this: Derivation with TransformProductToProductRuleModule =>
this: Derivation & TransformProductToProductRuleModule =>

protected object TransformValueClassToTypeRule extends Rule("ValueClassToType") {

def expand[From, To](implicit ctx: TransformationContext[From, To]): DerivationResult[Rule.ExpansionResult[To]] =
Type[From] match {
case ValueClassType(from2) =>
if (ctx.config.areOverridesEmptyForCurrent[From, To]) {
if (ctx.config.areValueOverridesEmptyForCurrent[From, To]) {
import from2.{Underlying as InnerFrom, value as valueFrom}
unwrapAndTransform[From, To, InnerFrom](valueFrom)
} else DerivationResult.attemptNextRuleBecause("Configuration has defined overrides")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ private[compiletime] trait TransformValueClassToValueClassRuleModule { this: Der
def expand[From, To](implicit ctx: TransformationContext[From, To]): DerivationResult[Rule.ExpansionResult[To]] =
(Type[From], Type[To]) match {
case (ValueClassType(from2), ValueClassType(to2)) =>
if (ctx.config.areOverridesEmptyForCurrent[From, To]) {
if (ctx.config.areValueOverridesEmptyForCurrent[From, To]) {
import from2.{Underlying as InnerFrom, value as valueFrom}, to2.{Underlying as InnerTo, value as valueTo}
unwrapTransformAndWrapAgain[From, To, InnerFrom, InnerTo](valueFrom, valueTo)
} else DerivationResult.attemptNextRuleBecause("Configuration has defined overrides")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,6 @@ class PartialTransformerStdLibTypesSpec extends ChimneySpec {
test("transform between Iterables and Maps, using Total Transformer for inner type transformation") {
implicit val intPrinter: Transformer[Int, String] = _.toString

// Seq(1 -> 10, 2 -> 20).intoPartial[Map[String, String]].enableMacrosLogging.transform.asOption ==> Some(
// Map("1" -> "10", "2" -> "20")
// )
Seq(1 -> 10, 2 -> 20).transformIntoPartial[Map[String, String]].asOption ==> Some(Map("1" -> "10", "2" -> "20"))
ArrayBuffer(1 -> 10, 2 -> 20).transformIntoPartial[Map[Int, String]].asOption ==> Some(
Map(1 -> "10", 2 -> "20")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,20 @@ class PartialTransformerValueTypeSpec extends ChimneySpec {
.asOption
.get ==> NestedProduct(UserNameAlias("BATMAN"))
}

test("flags overrides aren't enough to skip on AnyVal rule application") {
implicit val transformer = new Transformer[String, Int] {
override def transform(src: String): Int = src.length
}

val batman = "Batman"
val abc = "abc"
UserName(batman).intoPartial[UserId].enableMethodAccessors.transform.asOption.get ==> UserId(batman.length)
UserWithUserName(UserName(abc))
.intoPartial[UserWithUserId]
.enableMethodAccessors
.transform
.asOption
.get ==> UserWithUserId(UserId(abc.length))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,17 @@ class TotalTransformerValueTypeSpec extends ChimneySpec {
.withFieldComputed(_.value.value, un => un.value.value.toUpperCase)
.transform ==> NestedProduct(UserNameAlias("BATMAN"))
}

test("flags overrides aren't enough to skip on AnyVal rule application") {
implicit val transformer = new Transformer[String, Int] {
override def transform(src: String): Int = src.length
}

val batman = "Batman"
val abc = "abc"
UserName(batman).into[UserId].enableMethodAccessors.transform ==> UserId(batman.length)
UserWithUserName(UserName(abc)).into[UserWithUserId].enableMethodAccessors.transform ==> UserWithUserId(
UserId(abc.length)
)
}
}

0 comments on commit bcb1808

Please sign in to comment.