Skip to content

Commit

Permalink
Keep trace of intermediate src values and their Paths, for future wit…
Browse files Browse the repository at this point in the history
…hFieldRenamed improvements
  • Loading branch information
MateuszKubuszok committed Oct 2, 2024
1 parent c327e2b commit 08794da
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ private[compiletime] trait Configurations { this: Derivation =>
def everyMapKey: Path = new Path(segments :+ EveryMapKey)
def everyMapValue: Path = new Path(segments :+ EveryMapValue)

def concat(path: Path): Path = new Path(segments ++ path.segments)

@scala.annotation.tailrec
def drop(prefix: Path)(implicit ctx: TransformationContext[?, ?]): Option[Path] = (prefix, this) match {
case (Root, result) => Some(result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,35 @@ private[compiletime] trait Contexts { this: Derivation =>
val From: Type[From]
val To: Type[To]

val srcJournal: Vector[(Path, ExistentialExpr)]

/** When using nested paths (_.foo.bar.baz) and recursive derivation this is the original, "top-level" value */
val originalSrc: ExistentialExpr
val originalSrc: ExistentialExpr = srcJournal.head._2
val config: TransformerConfiguration
val derivationStartedAt: java.time.Instant

type Target
val Target: Type[Target]

def updateFromTo[NewFrom: Type, NewTo: Type](newSrc: Expr[NewFrom]): TransformationContext[NewFrom, NewTo] =
def updateFromTo[NewFrom: Type, NewTo: Type](
newSrc: Expr[NewFrom],
followFrom: Path = Path.Root,
followTo: Path = Path.Root
): TransformationContext[NewFrom, NewTo] =
fold[TransformationContext[NewFrom, NewTo]] { (ctx: TransformationContext.ForTotal[From, To]) =>
TransformationContext.ForTotal[NewFrom, NewTo](src = newSrc)(
From = Type[NewFrom],
To = Type[NewTo],
originalSrc = ctx.originalSrc,
config = ctx.config,
srcJournal = ctx.srcJournal :+ (ctx.srcJournal.last._1.concat(followFrom) -> newSrc.as_??),
config = ctx.config.prepareForRecursiveCall(followFrom, followTo)(ctx),
ctx.derivationStartedAt
)
} { (ctx: TransformationContext.ForPartial[From, To]) =>
TransformationContext.ForPartial[NewFrom, NewTo](src = newSrc, failFast = ctx.failFast)(
From = Type[NewFrom],
To = Type[NewTo],
originalSrc = ctx.originalSrc,
config = ctx.config,
srcJournal = ctx.srcJournal :+ (ctx.srcJournal.last._1.concat(followFrom) -> newSrc.as_??),
config = ctx.config.prepareForRecursiveCall(followFrom, followTo)(ctx),
ctx.derivationStartedAt
)
}
Expand All @@ -43,15 +49,15 @@ private[compiletime] trait Contexts { this: Derivation =>
TransformationContext.ForTotal[From, To](src = ctx.src)(
From = ctx.From,
To = ctx.To,
originalSrc = ctx.originalSrc,
srcJournal = ctx.srcJournal,
config = update(ctx.config),
derivationStartedAt = ctx.derivationStartedAt
)
} { (ctx: TransformationContext.ForPartial[From, To]) =>
TransformationContext.ForPartial[From, To](src = ctx.src, failFast = ctx.failFast)(
From = ctx.From,
To = ctx.To,
originalSrc = ctx.originalSrc,
srcJournal = ctx.srcJournal,
config = update(ctx.config),
derivationStartedAt = ctx.derivationStartedAt
)
Expand All @@ -78,7 +84,7 @@ private[compiletime] trait Contexts { this: Derivation =>
final case class ForTotal[From, To](src: Expr[From])(
val From: Type[From],
val To: Type[To],
val originalSrc: ExistentialExpr,
val srcJournal: Vector[(Path, ExistentialExpr)],
val config: TransformerConfiguration,
val derivationStartedAt: java.time.Instant
) extends TransformationContext[From, To] {
Expand All @@ -105,7 +111,7 @@ private[compiletime] trait Contexts { this: Derivation =>
ForTotal(src = src)(
From = Type[From],
To = Type[To],
originalSrc = src.as_??,
srcJournal = Vector(Path.Root -> src.as_??),
config = config.preventImplicitSummoningFor[From, To],
derivationStartedAt = java.time.Instant.now()
)
Expand All @@ -114,7 +120,7 @@ private[compiletime] trait Contexts { this: Derivation =>
final case class ForPartial[From, To](src: Expr[From], failFast: Expr[Boolean])(
val From: Type[From],
val To: Type[To],
val originalSrc: ExistentialExpr,
val srcJournal: Vector[(Path, ExistentialExpr)],
val config: TransformerConfiguration,
val derivationStartedAt: java.time.Instant
) extends TransformationContext[From, To] {
Expand Down Expand Up @@ -142,7 +148,7 @@ private[compiletime] trait Contexts { this: Derivation =>
): ForPartial[From, To] = ForPartial(src = src, failFast = failFast)(
From = Type[From],
To = Type[To],
originalSrc = src.as_??,
srcJournal = Vector(Path.Root -> src.as_??),
config = config.preventImplicitSummoningFor[From, To],
derivationStartedAt = java.time.Instant.now()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ private[compiletime] trait Derivation
updateRules: List[Rule] => List[Rule] = identity
)(implicit ctx: TransformationContext[?, ?]): DerivationResult[TransformationExpr[NewTo]] = {
val newCtx: TransformationContext[NewFrom, NewTo] =
ctx
.updateFromTo[NewFrom, NewTo](newSrc)
.updateConfig(_.prepareForRecursiveCall(followFrom, followTo))
ctx.updateFromTo[NewFrom, NewTo](newSrc, followFrom, followTo)
deriveTransformationResultExprUpdatingRules(updateRules)(newCtx)
.logSuccess {
case TransformationExpr.TotalExpr(expr) => s"Derived recursively total expression ${Expr.prettyPrint(expr)}"
Expand Down

0 comments on commit 08794da

Please sign in to comment.