Skip to content

Commit

Permalink
RuntimeDataStore is no longer hanrdocded in macros internals, now it'…
Browse files Browse the repository at this point in the history
…s an implementation detail in DSL/Gateway/config parsing
  • Loading branch information
MateuszKubuszok committed Jun 7, 2024
1 parent e00692d commit e0d3a52
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ private[compiletime] trait Derivation
val context = TransformationContext.ForTotal
.create[From, To](
src,
TransformerConfiguration(),
ChimneyExpr.RuntimeDataStore.empty
TransformerConfiguration()
)
.updateConfig(_.allowFromToImplicitSummoning)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.scalaland.chimney.internal.compiletime.derivation.transformer

import io.scalaland.chimney.dsl.ImplicitTransformerPreference
import io.scalaland.chimney.dsl.{ImplicitTransformerPreference, TransformerDefinitionCommons}
import io.scalaland.chimney.dsl as dsls
import io.scalaland.chimney.internal.runtime

Expand Down Expand Up @@ -185,21 +185,33 @@ private[compiletime] trait Configurations { this: Derivation =>
sealed trait ForSubtype extends TransformerOverride
sealed trait ForConstructor extends TransformerOverride

final case class Const(runtimeDataIdx: Int) extends ForField
final case class ConstPartial(runtimeDataIdx: Int) extends ForField
final case class Const(runtimeData: Expr[Any]) extends ForField {
override def toString: String = s"Const(${Expr.prettyPrint(runtimeData)})"
}
final case class ConstPartial(runtimeData: Expr[Any]) extends ForField {
override def toString: String = s"ConstPartial(${Expr.prettyPrint(runtimeData)})"
}

final case class Computed(runtimeDataIdx: Int) extends ForField // with ForSubtype
final case class ComputedPartial(runtimeDataIdx: Int) extends ForField // with ForSubtype
final case class Computed(runtimeData: Expr[Any]) extends ForField {
override def toString: String = s"Computed(${Expr.prettyPrint(runtimeData)})"
}
final case class ComputedPartial(runtimeData: Expr[Any]) extends ForField {
override def toString: String = s"ComputedPartial(${Expr.prettyPrint(runtimeData)})"
}

final case class CaseComputed(runtimeDataIdx: Int) extends ForSubtype
final case class CaseComputedPartial(runtimeDataIdx: Int) extends ForSubtype
final case class CaseComputed(runtimeData: Expr[Any]) extends ForSubtype {
override def toString: String = s"CaseComputed(${Expr.prettyPrint(runtimeData)})"
}
final case class CaseComputedPartial(runtimeData: Expr[Any]) extends ForSubtype {
override def toString: String = s"CaseComputedPartial(${Expr.prettyPrint(runtimeData)})"
}

type Args = List[ListMap[String, ??]]
final case class Constructor(runtimeDataIdx: Int, args: Args) extends ForConstructor {
override def toString: String = s"Constructor($runtimeDataIdx, ${printArgs(args)})"
final case class Constructor(runtimeData: Expr[Any], args: Args) extends ForConstructor {
override def toString: String = s"Constructor(${Expr.prettyPrint(runtimeData)}, ${printArgs(args)})"
}
final case class ConstructorPartial(runtimeDataIdx: Int, args: Args) extends ForConstructor {
override def toString: String = s"ConstructorPartial($runtimeDataIdx, ${printArgs(args)})"
final case class ConstructorPartial(runtimeData: Expr[Any], args: Args) extends ForConstructor {
override def toString: String = s"ConstructorPartial(${Expr.prettyPrint(runtimeData)}, ${printArgs(args)})"
}

final case class RenamedFrom(sourcePath: Path) extends ForField
Expand Down Expand Up @@ -288,11 +300,6 @@ private[compiletime] trait Configurations { this: Derivation =>
}
newPath <- path.drop(toPath).to(Vector)
if !(newPath == Path.Root && alwaysDropOnRoot)
// newPathWorkaround <- newPath match {
// case Path.AtSubtype(_, tail) => Vector(newPath, tail)
// case _ => Vector(newPath)
// }
// } yield newPathWorkaround -> runtimeOverride,
} yield newPath -> runtimeOverride,
preventImplicitSummoningForTypes = None
)
Expand All @@ -318,10 +325,10 @@ private[compiletime] trait Configurations { this: Derivation =>
Tail <: runtime.TransformerOverrides: Type,
InstanceFlags <: runtime.TransformerFlags: Type,
ImplicitScopeFlags <: runtime.TransformerFlags: Type
]: TransformerConfiguration = {
](runtimeDataStore: Expr[TransformerDefinitionCommons.RuntimeDataStore]): TransformerConfiguration = {
val implicitScopeFlags = extractTransformerFlags[ImplicitScopeFlags](TransformerFlags())
val allFlags = extractTransformerFlags[InstanceFlags](implicitScopeFlags)
val cfg = extractTransformerConfig[Tail](runtimeDataIdx = 0).copy(flags = allFlags)
val cfg = extractTransformerConfig[Tail](runtimeDataIdx = 0, runtimeDataStore).copy(flags = allFlags)
if (Type[InstanceFlags] =:= ChimneyType.TransformerFlags.Default) cfg else cfg.setLocalFlagsOverriden
}

Expand Down Expand Up @@ -394,68 +401,69 @@ private[compiletime] trait Configurations { this: Derivation =>
}

private def extractTransformerConfig[Tail <: runtime.TransformerOverrides: Type](
runtimeDataIdx: Int
runtimeDataIdx: Int,
runtimeDataStore: Expr[TransformerDefinitionCommons.RuntimeDataStore]
): TransformerConfiguration = Type[Tail] match {
case empty if empty =:= ChimneyType.TransformerOverrides.Empty => TransformerConfiguration()
case ChimneyType.TransformerOverrides.Const(toPath, cfg) =>
import toPath.Underlying as ToPath, cfg.Underlying as Tail2
extractTransformerConfig[Tail2](1 + runtimeDataIdx)
extractTransformerConfig[Tail2](1 + runtimeDataIdx, runtimeDataStore)
.addTransformerOverride(
extractPath[ToPath],
TransformerOverride.Const(runtimeDataIdx)
TransformerOverride.Const(runtimeDataStore(runtimeDataIdx))
)
case ChimneyType.TransformerOverrides.ConstPartial(toPath, cfg) =>
import toPath.Underlying as ToPath, cfg.Underlying as Tail2
extractTransformerConfig[Tail2](1 + runtimeDataIdx)
extractTransformerConfig[Tail2](1 + runtimeDataIdx, runtimeDataStore)
.addTransformerOverride(
extractPath[ToPath],
TransformerOverride.ConstPartial(runtimeDataIdx)
TransformerOverride.ConstPartial(runtimeDataStore(runtimeDataIdx))
)
case ChimneyType.TransformerOverrides.Computed(toPath, cfg) =>
import toPath.Underlying as ToPath, cfg.Underlying as Tail2
extractTransformerConfig[Tail2](1 + runtimeDataIdx)
extractTransformerConfig[Tail2](1 + runtimeDataIdx, runtimeDataStore)
.addTransformerOverride(
extractPath[ToPath],
TransformerOverride.Computed(runtimeDataIdx)
TransformerOverride.Computed(runtimeDataStore(runtimeDataIdx))
)
case ChimneyType.TransformerOverrides.ComputedPartial(toPath, cfg) =>
import toPath.Underlying as ToPath, cfg.Underlying as Tail2
extractTransformerConfig[Tail2](1 + runtimeDataIdx)
extractTransformerConfig[Tail2](1 + runtimeDataIdx, runtimeDataStore)
.addTransformerOverride(
extractPath[ToPath],
TransformerOverride.ComputedPartial(runtimeDataIdx)
TransformerOverride.ComputedPartial(runtimeDataStore(runtimeDataIdx))
)
case ChimneyType.TransformerOverrides.CaseComputed(toPath, cfg) =>
import toPath.Underlying as ToPath, cfg.Underlying as Tail2
extractTransformerConfig[Tail2](1 + runtimeDataIdx)
extractTransformerConfig[Tail2](1 + runtimeDataIdx, runtimeDataStore)
.addTransformerOverride(
extractPath[ToPath],
TransformerOverride.CaseComputed(runtimeDataIdx)
TransformerOverride.CaseComputed(runtimeDataStore(runtimeDataIdx))
)
case ChimneyType.TransformerOverrides.CaseComputedPartial(toPath, cfg) =>
import toPath.Underlying as ToPath, cfg.Underlying as Tail2
extractTransformerConfig[Tail2](1 + runtimeDataIdx)
extractTransformerConfig[Tail2](1 + runtimeDataIdx, runtimeDataStore)
.addTransformerOverride(
extractPath[ToPath],
TransformerOverride.CaseComputedPartial(runtimeDataIdx)
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)
extractTransformerConfig[Tail2](1 + runtimeDataIdx, runtimeDataStore)
.addTransformerOverride(
extractPath[ToPath],
TransformerOverride.Constructor(runtimeDataIdx, extractArgumentLists[Args])
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)
extractTransformerConfig[Tail2](1 + runtimeDataIdx, runtimeDataStore)
.addTransformerOverride(
extractPath[ToPath],
TransformerOverride.ConstructorPartial(runtimeDataIdx, extractArgumentLists[Args])
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)
extractTransformerConfig[Tail2](runtimeDataIdx, runtimeDataStore)
.addTransformerOverride(
extractPath[ToPath],
TransformerOverride.RenamedFrom(extractPath[FromPath])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.scalaland.chimney.internal.compiletime.derivation.transformer

import io.scalaland.chimney.dsl.TransformerDefinitionCommons
import io.scalaland.chimney.partial

private[compiletime] trait Contexts { this: Derivation =>
Expand All @@ -14,7 +13,6 @@ private[compiletime] trait Contexts { this: Derivation =>

/** When using nested paths (_.foo.bar.baz) and recursive derivation this is the original, "top-level" value */
val originalSrc: ExistentialExpr
val runtimeDataStore: Expr[TransformerDefinitionCommons.RuntimeDataStore]
val config: TransformerConfiguration
val derivationStartedAt: java.time.Instant

Expand All @@ -27,7 +25,6 @@ private[compiletime] trait Contexts { this: Derivation =>
From = Type[NewFrom],
To = Type[NewTo],
originalSrc = ctx.originalSrc,
runtimeDataStore = ctx.runtimeDataStore,
config = ctx.config,
ctx.derivationStartedAt
)
Expand All @@ -36,7 +33,6 @@ private[compiletime] trait Contexts { this: Derivation =>
From = Type[NewFrom],
To = Type[NewTo],
originalSrc = ctx.originalSrc,
runtimeDataStore = ctx.runtimeDataStore,
config = ctx.config,
ctx.derivationStartedAt
)
Expand All @@ -48,7 +44,6 @@ private[compiletime] trait Contexts { this: Derivation =>
From = ctx.From,
To = ctx.To,
originalSrc = ctx.originalSrc,
runtimeDataStore = ctx.runtimeDataStore,
config = update(ctx.config),
derivationStartedAt = ctx.derivationStartedAt
)
Expand All @@ -57,7 +52,6 @@ private[compiletime] trait Contexts { this: Derivation =>
From = ctx.From,
To = ctx.To,
originalSrc = ctx.originalSrc,
runtimeDataStore = ctx.runtimeDataStore,
config = update(ctx.config),
derivationStartedAt = ctx.derivationStartedAt
)
Expand Down Expand Up @@ -85,7 +79,6 @@ private[compiletime] trait Contexts { this: Derivation =>
val From: Type[From],
val To: Type[To],
val originalSrc: ExistentialExpr,
val runtimeDataStore: Expr[TransformerDefinitionCommons.RuntimeDataStore],
val config: TransformerConfiguration,
val derivationStartedAt: java.time.Instant
) extends TransformationContext[From, To] {
Expand All @@ -107,14 +100,12 @@ private[compiletime] trait Contexts { this: Derivation =>

def create[From: Type, To: Type](
src: Expr[From],
config: TransformerConfiguration,
runtimeDataStore: Expr[TransformerDefinitionCommons.RuntimeDataStore]
config: TransformerConfiguration
): ForTotal[From, To] =
ForTotal(src = src)(
From = Type[From],
To = Type[To],
originalSrc = src.as_??,
runtimeDataStore = runtimeDataStore,
config = config.preventImplicitSummoningFor[From, To],
derivationStartedAt = java.time.Instant.now()
)
Expand All @@ -124,7 +115,6 @@ private[compiletime] trait Contexts { this: Derivation =>
val From: Type[From],
val To: Type[To],
val originalSrc: ExistentialExpr,
val runtimeDataStore: Expr[TransformerDefinitionCommons.RuntimeDataStore],
val config: TransformerConfiguration,
val derivationStartedAt: java.time.Instant
) extends TransformationContext[From, To] {
Expand All @@ -148,13 +138,11 @@ private[compiletime] trait Contexts { this: Derivation =>
def create[From: Type, To: Type](
src: Expr[From],
failFast: Expr[Boolean],
config: TransformerConfiguration,
runtimeDataStore: Expr[TransformerDefinitionCommons.RuntimeDataStore]
config: TransformerConfiguration
): ForPartial[From, To] = ForPartial(src = src, failFast = failFast)(
From = Type[From],
To = Type[To],
originalSrc = src.as_??,
runtimeDataStore = runtimeDataStore,
config = config.preventImplicitSummoningFor[From, To],
derivationStartedAt = java.time.Instant.now()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ private[compiletime] trait Gateway extends GatewayCommons { this: Derivation =>
val context = TransformationContext.ForTotal
.create[From, To](
src,
TransformerConfigurations.readTransformerConfiguration[Tail, InstanceFlags, ImplicitScopeFlags],
runtimeDataStore
TransformerConfigurations.readTransformerConfiguration[Tail, InstanceFlags, ImplicitScopeFlags](
runtimeDataStore
)
)
.updateConfig(_.allowFromToImplicitSummoning)

Expand All @@ -55,8 +56,9 @@ private[compiletime] trait Gateway extends GatewayCommons { this: Derivation =>
val context = TransformationContext.ForTotal
.create[From, To](
src,
TransformerConfigurations.readTransformerConfiguration[Tail, InstanceFlags, ImplicitScopeFlags],
runtimeDataStore
TransformerConfigurations.readTransformerConfiguration[Tail, InstanceFlags, ImplicitScopeFlags](
runtimeDataStore
)
)

await(enableLoggingIfFlagEnabled(deriveFinalTransformationResultExpr(context), context))
Expand Down Expand Up @@ -85,8 +87,9 @@ private[compiletime] trait Gateway extends GatewayCommons { this: Derivation =>
.create[From, To](
src,
failFast,
TransformerConfigurations.readTransformerConfiguration[Tail, InstanceFlags, ImplicitScopeFlags],
runtimeDataStore
TransformerConfigurations.readTransformerConfiguration[Tail, InstanceFlags, ImplicitScopeFlags](
runtimeDataStore
)
)
.updateConfig(_.allowFromToImplicitSummoning)

Expand Down Expand Up @@ -114,8 +117,9 @@ private[compiletime] trait Gateway extends GatewayCommons { this: Derivation =>
.create[From, To](
src,
failFast,
TransformerConfigurations.readTransformerConfiguration[Tail, InstanceFlags, ImplicitScopeFlags],
runtimeDataStore
TransformerConfigurations.readTransformerConfiguration[Tail, InstanceFlags, ImplicitScopeFlags](
runtimeDataStore
)
)

await(enableLoggingIfFlagEnabled(deriveFinalTransformationResultExpr(context), context))
Expand Down
Loading

0 comments on commit e0d3a52

Please sign in to comment.