/
Derivation.scala
43 lines (39 loc) · 1.87 KB
/
Derivation.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package io.scalaland.chimney.internal.compiletime.derivation.transformer
import io.scalaland.chimney.internal.compiletime.{datatypes, ChimneyDefinitions, DerivationResult}
private[compiletime] trait Derivation
extends ChimneyDefinitions
with Configurations
with Contexts
with ImplicitSummoning
with ResultOps
with datatypes.IterableOrArrays
with datatypes.ProductTypes
with datatypes.SealedHierarchies
with datatypes.ValueClasses
with rules.TransformationRules {
/** Intended use case: starting recursive derivation from Gateway */
final protected def deriveTransformationResultExpr[From, To](implicit
ctx: TransformationContext[From, To]
): DerivationResult[TransformationExpr[To]] =
DerivationResult.namedScope(
ctx.fold(_ => s"Deriving Total Transformer expression from ${Type.prettyPrint[From]} to ${Type.prettyPrint[To]}")(
_ => s"Deriving Partial Transformer expression from ${Type.prettyPrint[From]} to ${Type.prettyPrint[To]}"
)
) {
Rule.expandRules[From, To](rulesAvailableForPlatform)
}
/** Intended use case: recursive derivation within rules */
final protected def deriveRecursiveTransformationExpr[NewFrom: Type, NewTo: Type](
newSrc: Expr[NewFrom]
)(implicit ctx: TransformationContext[?, ?]): DerivationResult[TransformationExpr[NewTo]] = {
val newCtx: TransformationContext[NewFrom, NewTo] = ctx.updateFromTo[NewFrom, NewTo](newSrc).updateConfig {
_.prepareForRecursiveCall
}
deriveTransformationResultExpr(newCtx)
.logSuccess {
case TransformationExpr.TotalExpr(expr) => s"Derived recursively total expression ${Expr.prettyPrint(expr)}"
case TransformationExpr.PartialExpr(expr) => s"Derived recursively partial expression ${Expr.prettyPrint(expr)}"
}
.logFailure(errors => s"Errors at recursive derivation: ${errors.prettyPrint}")
}
}