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

Refactor TransformerConfiguration Paths to more explicitly trace what is matched on source-side and what on target-side #612

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -173,11 +173,6 @@ private[compiletime] trait Configurations { this: Derivation =>
path.segments.headOption.collect { case Segment.Matching(tpe) => tpe -> new Path(path.segments.tail) }
}

object AtSourceSubtype {
def unapply(path: Path): Option[(??, Path)] =
path.segments.headOption.collect { case Segment.SourceMatching(tpe) => tpe -> new Path(path.segments.tail) }
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This special-case hack is no longer needed

object AtItem {
def unapply(path: Path): Option[Path] =
path.segments.headOption.collect { case Segment.EveryItem => new Path(path.segments.tail) }
Expand Down Expand Up @@ -226,6 +221,31 @@ private[compiletime] trait Configurations { this: Derivation =>
}
}

sealed protected trait SidedPath extends scala.Product with Serializable {
def path: Path = this match {
case SourcePath(fromPath) => fromPath
case TargetPath(toPath) => toPath
}
def drop(droppedFrom: Path, droppedTo: Path)(implicit ctx: TransformationContext[?, ?]): Option[SidedPath] =
this match {
case SourcePath(fromPath) => fromPath.drop(droppedFrom).map(SourcePath(_))
case TargetPath(toPath) => toPath.drop(droppedTo).map(TargetPath(_))
}
}
object SidedPath {

def unapply(sidedPath: SidedPath): Some[Path] = sidedPath match {
case SourcePath(fromPath) => Some(fromPath)
case TargetPath(toPath) => Some(toPath)
}
}
final protected case class SourcePath(fromPath: Path) extends SidedPath {
override def toString: String = s"Source at $fromPath"
}
final protected case class TargetPath(toPath: Path) extends SidedPath {
override def toString: String = s"Target at $toPath"
}

sealed protected trait TransformerOverride extends scala.Product with Serializable
protected object TransformerOverride {
sealed trait ForField extends TransformerOverride
Expand Down Expand Up @@ -276,17 +296,16 @@ private[compiletime] trait Configurations { this: Derivation =>
/** Let us distinct if flags were modified only by implicit TransformerConfiguration or maybe also locally */
private val localFlagsOverridden: Boolean = false,
/** Stores all customizations provided by user */
private val runtimeOverrides: Vector[(Path, TransformerOverride)] = Vector.empty,
private val runtimeOverrides: Vector[(SidedPath, TransformerOverride)] = Vector.empty,
/** Let us prevent `implicit val foo = foo` but allow `implicit val foo = new Foo { def sth = foo }` */
private val preventImplicitSummoningForTypes: Option[(??, ??)] = None
) {

private lazy val runtimeOverridesForCurrent = runtimeOverrides.filter {
case (Path.AtField(_, Path.Root), _: TransformerOverride.ForField) => true
case (Path.AtSubtype(_, Path.Root), _: TransformerOverride.ForSubtype) => true
case (Path.AtSourceSubtype(_, Path.Root), _: TransformerOverride.ForSubtype) => true
case (Path.Root, _: TransformerOverride.ForConstructor) => true
case _ => false
case (SidedPath(Path.AtField(_, Path.Root)), _: TransformerOverride.ForField) => true
case (SidedPath(Path.AtSubtype(_, Path.Root)), _: TransformerOverride.ForSubtype) => true
case (SidedPath(Path.Root), _: TransformerOverride.ForConstructor) => true
case _ => false
}

def allowFromToImplicitSummoning: TransformerConfiguration =
Expand All @@ -304,8 +323,8 @@ private[compiletime] trait Configurations { this: Derivation =>
def areLocalFlagsEmpty: Boolean =
!localFlagsOverridden

def addTransformerOverride(path: Path, runtimeOverride: TransformerOverride): TransformerConfiguration =
copy(runtimeOverrides = runtimeOverrides :+ (path -> runtimeOverride))
def addTransformerOverride(sidedPath: SidedPath, runtimeOverride: TransformerOverride): TransformerConfiguration =
copy(runtimeOverrides = runtimeOverrides :+ (sidedPath -> runtimeOverride))
def areOverridesEmpty: Boolean =
runtimeOverrides.isEmpty
def areLocalFlagsAndOverridesEmpty: Boolean =
Expand All @@ -314,69 +333,74 @@ private[compiletime] trait Configurations { this: Derivation =>
def filterCurrentOverridesForField(nameFilter: String => Boolean): Map[String, TransformerOverride.ForField] =
ListMap.from(
runtimeOverridesForCurrent.collect {
case (Path.AtField(name, _), runtimeFieldOverride: TransformerOverride.ForField) if nameFilter(name) =>
case (TargetPath(Path.AtField(name, _)), runtimeFieldOverride: TransformerOverride.ForField)
if nameFilter(name) =>
name -> runtimeFieldOverride
}
)
def filterCurrentOverridesForSubtype(
sourceTypeFilter: ?? => Boolean,
@scala.annotation.unused targetTypeFilter: ?? => Boolean
): Map[Option[??], TransformerOverride.ForSubtype] = ListMap.from(
sourceTypeFilter: ?? => Boolean
): Map[??, TransformerOverride.ForSubtype] = ListMap.from(
runtimeOverridesForCurrent.collect {
case (Path.AtSourceSubtype(tpe, _), runtimeCoproductOverride: TransformerOverride.ForSubtype)
case (SourcePath(Path.AtSubtype(tpe, _)), runtimeCoproductOverride: TransformerOverride.ForSubtype)
if sourceTypeFilter(tpe) =>
Some(tpe) -> runtimeCoproductOverride
tpe -> runtimeCoproductOverride
}
)
def filterCurrentOverridesForSome: Set[TransformerOverride.ForField] = ListSet.from(
runtimeOverrides.collect {
case (Path.AtSubtype(tpe, path), runtimeFieldOverride: TransformerOverride.ForField)
case (TargetPath(Path.AtSubtype(tpe, path)), runtimeFieldOverride: TransformerOverride.ForField)
if path == Path.Root && tpe.Underlying <:< Type[Some[Any]] =>
runtimeFieldOverride
}
)
def filterCurrentOverridesForLeft: Set[TransformerOverride.ForField] = ListSet.from(
runtimeOverrides.collect {
case (Path.AtSubtype(tpe, path), runtimeFieldOverride: TransformerOverride.ForField)
case (TargetPath(Path.AtSubtype(tpe, path)), runtimeFieldOverride: TransformerOverride.ForField)
if path == Path.Root && tpe.Underlying <:< Type[Left[Any, Any]] =>
runtimeFieldOverride
}
)
def filterCurrentOverridesForRight: Set[TransformerOverride.ForField] = ListSet.from(
runtimeOverrides.collect {
case (Path.AtSubtype(tpe, path), runtimeFieldOverride: TransformerOverride.ForField)
case (TargetPath(Path.AtSubtype(tpe, path)), runtimeFieldOverride: TransformerOverride.ForField)
if path == Path.Root && tpe.Underlying <:< Type[Right[Any, Any]] =>
runtimeFieldOverride
}
)
def filterCurrentOverridesForEveryItem: Set[TransformerOverride.ForField] = ListSet.from(
runtimeOverrides.collect {
case (Path.AtItem(path), runtimeFieldOverride: TransformerOverride.ForField) if path == Path.Root =>
case (TargetPath(Path.AtItem(path)), runtimeFieldOverride: TransformerOverride.ForField) if path == Path.Root =>
runtimeFieldOverride
}
)
def filterCurrentOverridesForEveryMapKey: Set[TransformerOverride.ForField] = ListSet.from(
runtimeOverrides.collect {
case (Path.AtMapKey(path), runtimeFieldOverride: TransformerOverride.ForField) if path == Path.Root =>
case (TargetPath(Path.AtMapKey(path)), runtimeFieldOverride: TransformerOverride.ForField)
if path == Path.Root =>
runtimeFieldOverride
}
)
def filterCurrentOverridesForEveryMapValue: Set[TransformerOverride.ForField] = ListSet.from(
runtimeOverrides.collect {
case (Path.AtMapValue(path), runtimeFieldOverride: TransformerOverride.ForField) if path == Path.Root =>
case (TargetPath(Path.AtMapValue(path)), runtimeFieldOverride: TransformerOverride.ForField)
if path == Path.Root =>
runtimeFieldOverride
}
)
def currentOverrideForConstructor: Option[TransformerOverride.ForConstructor] =
runtimeOverridesForCurrent.collectFirst {
case (_, runtimeConstructorOverride: TransformerOverride.ForConstructor) => runtimeConstructorOverride
case (TargetPath(_), runtimeConstructorOverride: TransformerOverride.ForConstructor) =>
runtimeConstructorOverride
}

def prepareForRecursiveCall(toPath: Path)(implicit ctx: TransformationContext[?, ?]): TransformerConfiguration =
def prepareForRecursiveCall(fromPath: Path, toPath: Path)(implicit
ctx: TransformationContext[?, ?]
): TransformerConfiguration =
copy(
localFlagsOverridden = false,
runtimeOverrides = for {
(path, runtimeOverride) <- runtimeOverrides
(sidedPath, runtimeOverride) <- runtimeOverrides
alwaysDropOnRoot = runtimeOverride match {
// Fields are always matched with "_.fieldName" Path while subtypes are always matched with
// "_ match { case _: Tpe => }" so "_" Paths are useless in their case while they might get in way of
Expand All @@ -385,9 +409,9 @@ private[compiletime] trait Configurations { this: Derivation =>
// Constructor is always matched at "_" Path, and dropped only when going inward
case _: TransformerOverride.ForConstructor => false
}
newPath <- path.drop(toPath).to(Vector)
if !(newPath == Path.Root && alwaysDropOnRoot)
} yield newPath -> runtimeOverride,
newSidePath <- sidedPath.drop(fromPath, toPath).to(Vector)
if !(newSidePath.path == Path.Root && alwaysDropOnRoot)
} yield newSidePath -> runtimeOverride,
preventImplicitSummoningForTypes = None
)

Expand Down Expand Up @@ -502,70 +526,70 @@ private[compiletime] trait Configurations { this: Derivation =>
import toPath.Underlying as ToPath, cfg.Underlying as Tail2
extractTransformerConfig[Tail2](1 + runtimeDataIdx, runtimeDataStore)
.addTransformerOverride(
extractPath[ToPath],
TargetPath(extractPath[ToPath]),
TransformerOverride.Const(runtimeDataStore(runtimeDataIdx))
)
case ChimneyType.TransformerOverrides.ConstPartial(toPath, cfg) =>
import toPath.Underlying as ToPath, cfg.Underlying as Tail2
extractTransformerConfig[Tail2](1 + runtimeDataIdx, runtimeDataStore)
.addTransformerOverride(
extractPath[ToPath],
TargetPath(extractPath[ToPath]),
TransformerOverride.ConstPartial(runtimeDataStore(runtimeDataIdx))
)
case ChimneyType.TransformerOverrides.Computed(toPath, cfg) =>
import toPath.Underlying as ToPath, cfg.Underlying as Tail2
extractTransformerConfig[Tail2](1 + runtimeDataIdx, runtimeDataStore)
.addTransformerOverride(
extractPath[ToPath],
TargetPath(extractPath[ToPath]),
TransformerOverride.Computed(runtimeDataStore(runtimeDataIdx))
)
case ChimneyType.TransformerOverrides.ComputedPartial(toPath, cfg) =>
import toPath.Underlying as ToPath, cfg.Underlying as Tail2
extractTransformerConfig[Tail2](1 + runtimeDataIdx, runtimeDataStore)
.addTransformerOverride(
extractPath[ToPath],
TargetPath(extractPath[ToPath]),
TransformerOverride.ComputedPartial(runtimeDataStore(runtimeDataIdx))
)
case ChimneyType.TransformerOverrides.CaseComputed(toPath, cfg) =>
import toPath.Underlying as ToPath, cfg.Underlying as Tail2
extractTransformerConfig[Tail2](1 + runtimeDataIdx, runtimeDataStore)
.addTransformerOverride(
extractPath[ToPath],
SourcePath(extractPath[ToPath]),
TransformerOverride.CaseComputed(runtimeDataStore(runtimeDataIdx))
)
case ChimneyType.TransformerOverrides.CaseComputedPartial(toPath, cfg) =>
import toPath.Underlying as ToPath, cfg.Underlying as Tail2
extractTransformerConfig[Tail2](1 + runtimeDataIdx, runtimeDataStore)
.addTransformerOverride(
extractPath[ToPath],
SourcePath(extractPath[ToPath]),
TransformerOverride.CaseComputedPartial(runtimeDataStore(runtimeDataIdx))
)
case ChimneyType.TransformerOverrides.Constructor(args, toPath, cfg) =>
import args.Underlying as Args, toPath.Underlying as ToPath, cfg.Underlying as Tail2
extractTransformerConfig[Tail2](1 + runtimeDataIdx, runtimeDataStore)
.addTransformerOverride(
extractPath[ToPath],
TargetPath(extractPath[ToPath]),
TransformerOverride.Constructor(runtimeDataStore(runtimeDataIdx), extractArgumentLists[Args])
)
case ChimneyType.TransformerOverrides.ConstructorPartial(args, toPath, cfg) =>
import args.Underlying as Args, toPath.Underlying as ToPath, cfg.Underlying as Tail2
extractTransformerConfig[Tail2](1 + runtimeDataIdx, runtimeDataStore)
.addTransformerOverride(
extractPath[ToPath],
TargetPath(extractPath[ToPath]),
TransformerOverride.ConstructorPartial(runtimeDataStore(runtimeDataIdx), extractArgumentLists[Args])
)
case ChimneyType.TransformerOverrides.RenamedFrom(fromPath, toPath, cfg) =>
import fromPath.Underlying as FromPath, toPath.Underlying as ToPath, cfg.Underlying as Tail2
extractTransformerConfig[Tail2](runtimeDataIdx, runtimeDataStore)
.addTransformerOverride(
extractPath[ToPath],
TargetPath(extractPath[ToPath]),
TransformerOverride.RenamedFrom(extractPath[FromPath])
)
case ChimneyType.TransformerOverrides.RenamedTo(fromPath, toPath, cfg) =>
import fromPath.Underlying as FromPath, toPath.Underlying as ToPath, cfg.Underlying as Tail2
extractTransformerConfig[Tail2](runtimeDataIdx, runtimeDataStore)
.addTransformerOverride(
extractPath[FromPath],
SourcePath(extractPath[FromPath]),
TransformerOverride.RenamedTo(extractPath[ToPath])
)
// $COVERAGE-OFF$should never happen unless someone mess around with type-level representation
Expand All @@ -585,7 +609,7 @@ private[compiletime] trait Configurations { this: Derivation =>
extractPath[PathType2].matching[Subtype]
case ChimneyType.Path.SourceMatching(init, sourceSubtype) =>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to leave this in the DSL runtime (bincompat), so we might as well still parse it.

import init.Underlying as PathType2, sourceSubtype.Underlying as SourceSubtype
extractPath[PathType2].sourceMatching[SourceSubtype]
extractPath[PathType2].matching[SourceSubtype]
case ChimneyType.Path.EveryItem(init) =>
import init.Underlying as PathType2
extractPath[PathType2].everyItem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,21 @@ private[compiletime] trait Derivation
/** Intended use case: recursive derivation within rules */
final protected def deriveRecursiveTransformationExpr[NewFrom: Type, NewTo: Type](
newSrc: Expr[NewFrom],
followFrom: Path,
followTo: Path
)(implicit ctx: TransformationContext[?, ?]): DerivationResult[TransformationExpr[NewTo]] =
deriveRecursiveTransformationExprUpdatingRules[NewFrom, NewTo](newSrc, followTo)(identity)
deriveRecursiveTransformationExprUpdatingRules[NewFrom, NewTo](newSrc, followFrom, followTo)(identity)

/** Intended use case: recursive derivation within rules which should remove some rules from consideration */
final protected def deriveRecursiveTransformationExprUpdatingRules[NewFrom: Type, NewTo: Type](
newSrc: Expr[NewFrom],
followFrom: Path,
followTo: Path
)(
updateRules: List[Rule] => List[Rule]
)(implicit ctx: TransformationContext[?, ?]): DerivationResult[TransformationExpr[NewTo]] = {
val newCtx: TransformationContext[NewFrom, NewTo] =
ctx.updateFromTo[NewFrom, NewTo](newSrc).updateConfig(_.prepareForRecursiveCall(followTo))
ctx.updateFromTo[NewFrom, NewTo](newSrc).updateConfig(_.prepareForRecursiveCall(followFrom, followTo))
deriveTransformationResultExprUpdatingRules(updateRules)(newCtx)
.logSuccess {
case TransformationExpr.TotalExpr(expr) => s"Derived recursively total expression ${Expr.prettyPrint(expr)}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ private[compiletime] trait TransformEitherToEitherRuleModule {
useOverrideIfPresentOr("matchingLeft", ctx.config.filterCurrentOverridesForLeft) {
deriveRecursiveTransformationExpr[FromL, ToL](
ctx.src.upcastToExprOf[Left[FromL, FromR]].value,
Path.Root.matching[Left[FromL, FromR]],
Path.Root.matching[Left[ToL, ToR]]
)
}
Expand All @@ -46,6 +47,7 @@ private[compiletime] trait TransformEitherToEitherRuleModule {
useOverrideIfPresentOr("matchingRight", ctx.config.filterCurrentOverridesForRight) {
deriveRecursiveTransformationExpr[FromR, ToR](
ctx.src.upcastToExprOf[Right[FromL, FromR]].value,
Path.Root.matching[Right[FromL, FromR]],
Path.Root.matching[Right[ToL, ToR]]
)
}
Expand All @@ -62,15 +64,23 @@ private[compiletime] trait TransformEitherToEitherRuleModule {
.promise[FromL](ExprPromise.NameGenerationStrategy.FromPrefix("left"))
.traverse { (leftExpr: Expr[FromL]) =>
useOverrideIfPresentOr("matchingLeft", ctx.config.filterCurrentOverridesForLeft) {
deriveRecursiveTransformationExpr[FromL, ToL](leftExpr, Path.Root.matching[Left[ToL, ToR]])
deriveRecursiveTransformationExpr[FromL, ToL](
leftExpr,
Path.Root.matching[Either[FromL, FromR]],
Path.Root.matching[Left[ToL, ToR]]
)
}
}

val toRightResult = ExprPromise
.promise[FromR](ExprPromise.NameGenerationStrategy.FromPrefix("right"))
.traverse { (rightExpr: Expr[FromR]) =>
useOverrideIfPresentOr("matchingRight", ctx.config.filterCurrentOverridesForRight) {
deriveRecursiveTransformationExpr[FromR, ToR](rightExpr, Path.Root.matching[Right[ToL, ToR]])
deriveRecursiveTransformationExpr[FromR, ToR](
rightExpr,
Path.Root.matching[Either[FromL, FromR]],
Path.Root.matching[Right[ToL, ToR]]
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ private[compiletime] trait TransformIterableToIterableRuleModule {
.promise[FromK](ExprPromise.NameGenerationStrategy.FromPrefix("key"))
.traverse { key =>
useOverrideIfPresentOr("everyMapKey", ctx.config.filterCurrentOverridesForEveryMapKey) {
deriveRecursiveTransformationExpr[FromK, ToK](key, Path.Root.everyMapKey)
deriveRecursiveTransformationExpr[FromK, ToK](key, Path.Root.everyMapKey, Path.Root.everyMapKey)
}.map(_.ensurePartial -> key)
}
val toValueResult = ExprPromise
.promise[FromV](ExprPromise.NameGenerationStrategy.FromPrefix("value"))
.traverse { value =>
useOverrideIfPresentOr("everyMapValue", ctx.config.filterCurrentOverridesForEveryMapValue) {
deriveRecursiveTransformationExpr[FromV, ToV](value, Path.Root.everyMapValue)
deriveRecursiveTransformationExpr[FromV, ToV](value, Path.Root.everyMapValue, Path.Root.everyMapValue)
}.map(_.ensurePartial)
}

Expand Down Expand Up @@ -119,7 +119,7 @@ private[compiletime] trait TransformIterableToIterableRuleModule {
.promise[InnerFrom](ExprPromise.NameGenerationStrategy.FromExpr(ctx.src))
.traverse { (newFromSrc: Expr[InnerFrom]) =>
useOverrideIfPresentOr("everyItem", ctx.config.filterCurrentOverridesForEveryItem) {
deriveRecursiveTransformationExpr[InnerFrom, InnerTo](newFromSrc, Path.Root.everyItem)
deriveRecursiveTransformationExpr[InnerFrom, InnerTo](newFromSrc, Path.Root.everyItem, Path.Root.everyItem)
}
}
.flatMap { (to2P: ExprPromise[InnerFrom, TransformationExpr[InnerTo]]) =>
Expand Down
Loading
Loading