-
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TransformTypeConstraintRule and TransformImplicitConversionRule
- Loading branch information
1 parent
a24bab8
commit ef67255
Showing
7 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...rnal/compiletime/derivation/transformer/rules/TransformImplicitConversionRuleModule.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package io.scalaland.chimney.internal.compiletime.derivation.transformer.rules | ||
|
||
import io.scalaland.chimney.internal.compiletime.DerivationResult | ||
import io.scalaland.chimney.internal.compiletime.derivation.transformer.Derivation | ||
|
||
private[compiletime] trait TransformImplicitConversionRuleModule { this: Derivation => | ||
|
||
import Type.Implicits.* | ||
|
||
protected object TransformImplicitConversionRule extends Rule("ImplicitConversion") { | ||
|
||
def expand[From, To](implicit ctx: TransformationContext[From, To]): DerivationResult[Rule.ExpansionResult[To]] = | ||
if (ctx.config.flags.implicitConversions) { | ||
Expr.summonImplicit[From => To] match { | ||
case Some(ev) => transformWithConversion[From, To](ev) | ||
case None => DerivationResult.attemptNextRule | ||
} | ||
} else DerivationResult.attemptNextRuleBecause("Implicit conversions are disabled") | ||
|
||
private def transformWithConversion[From, To](ev: Expr[From => To])(implicit | ||
ctx: TransformationContext[From, To] | ||
): DerivationResult[Rule.ExpansionResult[To]] = | ||
// We're constructing: | ||
// '{ ${ ev }.apply(${ src }) } | ||
DerivationResult.expandedTotal(ev.apply(ctx.src)) | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...imney/internal/compiletime/derivation/transformer/rules/TransformTypeConstraintRule.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package io.scalaland.chimney.internal.compiletime.derivation.transformer.rules | ||
|
||
import io.scalaland.chimney.internal.compiletime.DerivationResult | ||
import io.scalaland.chimney.internal.compiletime.derivation.transformer.Derivation | ||
|
||
private[compiletime] trait TransformTypeConstraintRuleModule { this: Derivation => | ||
|
||
import Type.Implicits.* | ||
|
||
protected object TransformTypeConstraintRule extends Rule("TypeConstraint") { | ||
|
||
def expand[From, To](implicit ctx: TransformationContext[From, To]): DerivationResult[Rule.ExpansionResult[To]] = | ||
if (ctx.config.flags.typeConstraintEvidence) { | ||
Expr.summonImplicit[From <:< To] match { | ||
case Some(ev) => transformWithEvidence[From, To](ev) | ||
case None => DerivationResult.attemptNextRule | ||
} | ||
} else DerivationResult.attemptNextRuleBecause("<:< evidence is disabled") | ||
|
||
private def transformWithEvidence[From, To](ev: Expr[From <:< To])(implicit | ||
ctx: TransformationContext[From, To] | ||
): DerivationResult[Rule.ExpansionResult[To]] = | ||
// We're constructing: | ||
// '{ ${ ev }.apply(${ src }) } | ||
DerivationResult.expandedTotal(ev.upcastToExprOf[From => To].apply(ctx.src)) | ||
} | ||
} |