Skip to content

Commit

Permalink
Add withFallback DSL method, DSL macros and TransformerOverride type
Browse files Browse the repository at this point in the history
  • Loading branch information
MateuszKubuszok committed Oct 3, 2024
1 parent 08794da commit a08ca86
Show file tree
Hide file tree
Showing 17 changed files with 284 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,27 @@ final class PartialTransformerDefinition[From, To, Overrides <: TransformerOverr
): PartialTransformerDefinition[From, To, ? <: TransformerOverrides, Flags] =
macro PartialTransformerDefinitionMacros.withConstructorEitherImpl[From, To, Overrides, Flags]

/** To use `fallback` when the source of type `From` is missing fields.
*
* Fallbacks can be stacked - then they will be tried in the order in which they were added.
*
* @see
* TODO
*
* @tparam FromFallback
* type of the fallback value which would be checked for fields when the `From` value would be missing
* @param fallback
* fallback value which would be checked for fields when the `From` value would be missing
* @return
* [[io.scalaland.chimney.dsl.PartialTransformerDefinition]]
*
* @since TODO
*/
def withFallback[FromFallback](
fallback: FromFallback
): PartialTransformerDefinition[From, To, ? <: TransformerOverrides, Flags] =
macro PartialTransformerDefinitionMacros.withFallbackImpl[From, To, Overrides, Flags, FromFallback]

/** Build Partial Transformer using current configuration.
*
* It runs macro that tries to derive instance of `PartialTransformer[From, To]`. When transformation can't be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,27 @@ final class PartialTransformerInto[From, To, Overrides <: TransformerOverrides,
): PartialTransformerInto[From, To, ? <: TransformerOverrides, Flags] =
macro PartialTransformerIntoMacros.withConstructorEitherImpl[From, To, Overrides, Flags]

/** To use `fallback` when the source of type `From` is missing fields.
*
* Fallbacks can be stacked - then they will be tried in the order in which they were added.
*
* @see
* TODO
*
* @tparam FromFallback
* type of the fallback value which would be checked for fields when the `From` value would be missing
* @param fallback
* fallback value which would be checked for fields when the `From` value would be missing
* @return
* [[io.scalaland.chimney.dsl.TransformerOverrides]]
*
* @since TODO
*/
def withFallback[FromFallback](
fallback: FromFallback
): TransformerOverrides[From, To, ? <: TransformerOverrides, Flags] =
macro PartialTransformerIntoMacros.withFallbackImpl[From, To, Overrides, Flags, FromFallback]

/** Apply configured partial transformation in-place.
*
* It runs macro that tries to derive instance of `PartialTransformer[From, To]` and immediately apply it to captured
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,27 @@ final class TransformerDefinition[From, To, Overrides <: TransformerOverrides, F
)(implicit ev: IsFunction.Of[Ctor, To]): TransformerDefinition[From, To, ? <: TransformerOverrides, Flags] =
macro TransformerDefinitionMacros.withConstructorImpl[From, To, Overrides, Flags]

/** To use `fallback` when the source of type `From` is missing fields.
*
* Fallbacks can be stacked - then they will be tried in the order in which they were added.
*
* @see
* TODO
*
* @tparam FromFallback
* type of the fallback value which would be checked for fields when the `From` value would be missing
* @param fallback
* fallback value which would be checked for fields when the `From` value would be missing
* @return
* [[io.scalaland.chimney.dsl.TransformerDefinition]]
*
* @since TODO
*/
def withFallback[FromFallback](
fallback: FromFallback
): TransformerDefinition[From, To, ? <: TransformerOverrides, Flags] =
macro TransformerDefinitionMacros.withFallbackImpl[From, To, Overrides, Flags, FromFallback]

/** Build Transformer using current configuration.
*
* It runs macro that tries to derive instance of `Transformer[From, To]`. When transformation can't be derived, it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,25 @@ final class TransformerInto[From, To, Overrides <: TransformerOverrides, Flags <
): TransformerInto[From, To, ? <: TransformerOverrides, Flags] =
macro TransformerIntoMacros.withConstructorImpl[From, To, Overrides, Flags]

/** To use `fallback` when the source of type `From` is missing fields.
*
* Fallbacks can be stacked - then they will be tried in the order in which they were added.
*
* @see
* TODO
*
* @tparam FromFallback
* type of the fallback value which would be checked for fields when the `From` value would be missing
* @param fallback
* fallback value which would be checked for fields when the `From` value would be missing
* @return
* [[io.scalaland.chimney.dsl.TransformerInto]]
*
* @since TODO
*/
def withFallback[FromFallback](fallback: FromFallback): TransformerInto[From, To, ? <: TransformerOverrides, Flags] =
macro TransformerIntoMacros.withFallbackImpl[From, To, Overrides, Flags, FromFallback]

/** Apply configured transformation in-place.
*
* It runs macro that tries to derive instance of `Transformer[From, To]` and immediately apply it to captured
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,15 @@ class PartialTransformerDefinitionMacros(val c: whitebox.Context) extends utils.
.addOverride(q"_root_.io.scalaland.chimney.internal.runtime.FunctionEitherToResult.lift($f)")
.asInstanceOfExpr[PartialTransformerDefinition[From, To, ConstructorPartial[Args, Path.Root, Overrides], Flags]]
}.applyFromBody(f)

def withFallbackImpl[
From: WeakTypeTag,
To: WeakTypeTag,
Overrides <: TransformerOverrides: WeakTypeTag,
Flags <: TransformerFlags: WeakTypeTag,
FromFallback: WeakTypeTag
](fallback: Tree): Tree =
c.prefix.tree
.addOverride(fallback)
.asInstanceOfExpr[PartialTransformerDefinition[From, To, Fallback[FromFallback, Path.Root, Overrides], Flags]]
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,15 @@ class PartialTransformerIntoMacros(val c: whitebox.Context) extends utils.DslMac
.addOverride(q"_root_.io.scalaland.chimney.internal.runtime.FunctionEitherToResult.lift($f)")
.asInstanceOfExpr[PartialTransformerInto[From, To, ConstructorPartial[Args, Path.Root, Overrides], Flags]]
}.applyFromBody(f)

def withFallbackImpl[
From: WeakTypeTag,
To: WeakTypeTag,
Overrides <: TransformerOverrides: WeakTypeTag,
Flags <: TransformerFlags: WeakTypeTag,
FromFallback: WeakTypeTag
](fallback: Tree): Tree =
c.prefix.tree
.addOverride(fallback)
.asInstanceOfExpr[PartialTransformerInto[From, To, Fallback[FromFallback, Path.Root, Overrides], Flags]]
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,15 @@ class TransformerDefinitionMacros(val c: whitebox.Context) extends utils.DslMacr
.addOverride(f)
.asInstanceOfExpr[TransformerDefinition[From, To, Constructor[Args, Path.Root, Overrides], Flags]]
}.applyFromBody(f)

def withFallbackImpl[
From: WeakTypeTag,
To: WeakTypeTag,
Overrides <: TransformerOverrides: WeakTypeTag,
Flags <: TransformerFlags: WeakTypeTag,
FromFallback: WeakTypeTag
](fallback: Tree): Tree =
c.prefix.tree
.addOverride(fallback)
.asInstanceOfExpr[TransformerDefinition[From, To, Fallback[FromFallback, Path.Root, Overrides], Flags]]
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,15 @@ class TransformerIntoMacros(val c: whitebox.Context) extends utils.DslMacroUtils
.addOverride(f)
.asInstanceOfExpr[TransformerInto[From, To, Constructor[Args, Path.Root, Overrides], Flags]]
}.applyFromBody(f)

def withFallbackImpl[
From: WeakTypeTag,
To: WeakTypeTag,
Overrides <: TransformerOverrides: WeakTypeTag,
Flags <: TransformerFlags: WeakTypeTag,
FromFallback: WeakTypeTag
](fallback: Tree): Tree =
c.prefix.tree
.addOverride(fallback)
.asInstanceOfExpr[TransformerInto[From, To, Fallback[FromFallback, Path.Root, Overrides], Flags]]
}
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,27 @@ final class PartialTransformerDefinition[From, To, Overrides <: TransformerOverr
): PartialTransformerDefinition[From, To, ? <: TransformerOverrides, Flags] =
${ PartialTransformerDefinitionMacros.withConstructorEitherImpl('this, 'f) }

/** To use `fallback` when the source of type `From` is missing fields.
*
* Fallbacks can be stacked - then they will be tried in the order in which they were added.
*
* @see
* TODO
*
* @tparam FromFallback
* type of the fallback value which would be checked for fields when the `From` value would be missing
* @param fallback
* fallback value which would be checked for fields when the `From` value would be missing
* @return
* [[io.scalaland.chimney.dsl.PartialTransformerDefinition]]
*
* @since TODO
*/
transparent inline def withFallback[FromFallback](
inline fallback: FromFallback
): PartialTransformerDefinition[From, To, ? <: TransformerOverrides, Flags] =
${ PartialTransformerDefinitionMacros.withFallbackImpl('this, 'fallback) }

/** Build Partial Transformer using current configuration.
*
* It runs macro that tries to derive instance of `PartialTransformer[From, To]`. When transformation can't be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,27 @@ final class PartialTransformerInto[From, To, Overrides <: TransformerOverrides,
)(using IsFunction.Of[Ctor, Either[String, To]]): PartialTransformerInto[From, To, ? <: TransformerOverrides, Flags] =
${ PartialTransformerIntoMacros.withConstructorEitherImpl('this, 'f) }

/** To use `fallback` when the source of type `From` is missing fields.
*
* Fallbacks can be stacked - then they will be tried in the order in which they were added.
*
* @see
* TODO
*
* @tparam FromFallback
* type of the fallback value which would be checked for fields when the `From` value would be missing
* @param fallback
* fallback value which would be checked for fields when the `From` value would be missing
* @return
* [[io.scalaland.chimney.dsl.PartialTransformerInto]]
*
* @since TODO
*/
transparent inline def withFallback[FromFallback](
inline fallback: FromFallback
): PartialTransformerInto[From, To, ? <: TransformerOverrides, Flags] =
${ PartialTransformerIntoMacros.withFallbackImpl('this, 'fallback) }

/** Apply configured partial transformation in-place.
*
* It runs macro that tries to derive instance of `PartialTransformer[From, To]` and immediately apply it to captured
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,27 @@ final class TransformerDefinition[From, To, Overrides <: TransformerOverrides, F
)(using IsFunction.Of[Ctor, To]): TransformerDefinition[From, To, ? <: TransformerOverrides, Flags] =
${ TransformerDefinitionMacros.withConstructorImpl('this, 'f) }

/** To use `fallback` when the source of type `From` is missing fields.
*
* Fallbacks can be stacked - then they will be tried in the order in which they were added.
*
* @see
* TODO
*
* @tparam FromFallback
* type of the fallback value which would be checked for fields when the `From` value would be missing
* @param fallback
* fallback value which would be checked for fields when the `From` value would be missing
* @return
* [[io.scalaland.chimney.dsl.TransformerDefinition]]
*
* @since TODO
*/
transparent inline def withFallback[FromFallback](
inline fallback: FromFallback
): TransformerDefinition[From, To, ? <: TransformerOverrides, Flags] =
${ TransformerDefinitionMacros.withFallbackImpl('this, 'fallback) }

/** Build Transformer using current configuration.
*
* It runs macro that tries to derive instance of `Transformer[From, To]`. When transformation can't be derived, it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,27 @@ final class TransformerInto[From, To, Overrides <: TransformerOverrides, Flags <
)(using IsFunction.Of[Ctor, To]): TransformerInto[From, To, ? <: TransformerOverrides, Flags] =
${ TransformerIntoMacros.withConstructorImpl('this, 'f) }

/** To use `fallback` when the source of type `From` is missing fields.
*
* Fallbacks can be stacked - then they will be tried in the order in which they were added.
*
* @see
* TODO
*
* @tparam FromFallback
* type of the fallback value which would be checked for fields when the `From` value would be missing
* @param fallback
* fallback value which would be checked for fields when the `From` value would be missing
* @return
* [[io.scalaland.chimney.dsl.TransformerInto]]
*
* @since TODO
*/
transparent inline def withFallback[FromFallback](
inline fallback: FromFallback
): TransformerInto[From, To, ? <: TransformerOverrides, Flags] =
${ TransformerIntoMacros.withFallbackImpl('this, 'fallback) }

/** Apply configured transformation in-place.
*
* It runs macro that tries to derive instance of `Transformer[From, To]` and immediately apply it to captured
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,15 @@ object PartialTransformerDefinitionMacros {
Flags <: TransformerFlags: Type,
Ctor: Type
](
ti: Expr[PartialTransformerDefinition[From, To, Overrides, Flags]],
td: Expr[PartialTransformerDefinition[From, To, Overrides, Flags]],
f: Expr[Ctor]
)(using Quotes): Expr[PartialTransformerDefinition[From, To, ? <: TransformerOverrides, Flags]] =
DslMacroUtils().applyConstructorType {
[args <: ArgumentLists] =>
(_: Type[args]) ?=>
'{
WithRuntimeDataStore
.update($ti, $f)
.update($td, $f)
.asInstanceOf[PartialTransformerDefinition[From, To, Constructor[args, Path.Root, Overrides], Flags]]
}
}(f)
Expand All @@ -229,15 +229,15 @@ object PartialTransformerDefinitionMacros {
Flags <: TransformerFlags: Type,
Ctor: Type
](
ti: Expr[PartialTransformerDefinition[From, To, Overrides, Flags]],
td: Expr[PartialTransformerDefinition[From, To, Overrides, Flags]],
f: Expr[Ctor]
)(using Quotes): Expr[PartialTransformerDefinition[From, To, ? <: TransformerOverrides, Flags]] =
DslMacroUtils().applyConstructorType {
[args <: ArgumentLists] =>
(_: Type[args]) ?=>
'{
WithRuntimeDataStore
.update($ti, $f)
.update($td, $f)
.asInstanceOf[PartialTransformerDefinition[
From,
To,
Expand All @@ -254,7 +254,7 @@ object PartialTransformerDefinitionMacros {
Flags <: TransformerFlags: Type,
Ctor: Type
](
ti: Expr[PartialTransformerDefinition[From, To, Overrides, Flags]],
td: Expr[PartialTransformerDefinition[From, To, Overrides, Flags]],
f: Expr[Ctor]
)(using Quotes): Expr[PartialTransformerDefinition[From, To, ? <: TransformerOverrides, Flags]] =
DslMacroUtils().applyConstructorType {
Expand All @@ -263,7 +263,7 @@ object PartialTransformerDefinitionMacros {
'{
WithRuntimeDataStore
.update(
$ti,
$td,
FunctionEitherToResult.lift[Ctor, Any]($f)(
${ Expr.summon[FunctionEitherToResult[Ctor]].get }.asInstanceOf[FunctionEitherToResult.Aux[Ctor, Any]]
)
Expand All @@ -277,6 +277,22 @@ object PartialTransformerDefinitionMacros {
}
}(f)

def withFallbackImpl[
From: Type,
To: Type,
Overrides <: TransformerOverrides: Type,
Flags <: TransformerFlags: Type,
FromFallback: Type
](
td: Expr[PartialTransformerDefinition[From, To, Overrides, Flags]],
fallback: Expr[FromFallback]
)(using Quotes): Expr[PartialTransformerDefinition[From, To, ? <: TransformerOverrides, Flags]] =
'{
WithRuntimeDataStore
.update($td, $fallback)
.asInstanceOf[PartialTransformerDefinition[From, To, Fallback[FromFallback, Path.Root, Overrides], Flags]]
}

def buildTransformer[
From: Type,
To: Type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,20 @@ object PartialTransformerIntoMacros {
.asInstanceOf[PartialTransformerInto[From, To, ConstructorPartial[args, Path.Root, Overrides], Flags]]
}
}(f)

def withFallbackImpl[
From: Type,
To: Type,
Overrides <: TransformerOverrides: Type,
Flags <: TransformerFlags: Type,
FromFallback: Type
](
ti: Expr[PartialTransformerInto[From, To, Overrides, Flags]],
fallback: Expr[FromFallback]
)(using Quotes): Expr[PartialTransformerInto[From, To, ? <: TransformerOverrides, Flags]] =
'{
WithRuntimeDataStore
.update($ti, $fallback)
.asInstanceOf[PartialTransformerInto[From, To, Fallback[FromFallback, Path.Root, Overrides], Flags]]
}
}
Loading

0 comments on commit a08ca86

Please sign in to comment.