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 Jan 5, 2025
1 parent 7137b36 commit d2f1c6d
Show file tree
Hide file tree
Showing 17 changed files with 276 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,27 @@ final class PartialTransformerDefinition[From, To, Overrides <: TransformerOverr
macro PartialTransformerDefinitionMacros
.withSealedSubtypeRenamedImpl[From, To, Overrides, Flags, FromSubtype, ToSubtype]

/** 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]

/** Use `f` instead of the primary constructor to construct the `To` value.
*
* Macro will read the names of Eta-expanded method's/lambda's parameters and try to match them with `From` getters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,27 @@ final class PartialTransformerInto[From, To, Overrides <: TransformerOverrides,
def withEnumCaseRenamed[FromSubtype, ToSubtype]: PartialTransformerInto[From, To, ? <: TransformerOverrides, Flags] =
macro PartialTransformerIntoMacros.withSealedSubtypeRenamedImpl[From, To, Overrides, Flags, FromSubtype, ToSubtype]

/** 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
): PartialTransformerInto[From, To, ? <: TransformerOverrides, Flags] =
macro PartialTransformerIntoMacros.withFallbackImpl[From, To, Overrides, Flags, FromFallback]

/** Use `f` instead of the primary constructor to construct the `To` value.
*
* Macro will read the names of Eta-expanded method's/lambda's parameters and try to match them with `From` getters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,27 @@ final class TransformerDefinition[From, To, Overrides <: TransformerOverrides, F
def withEnumCaseRenamed[FromSubtype, ToSubtype]: TransformerDefinition[From, To, ? <: TransformerOverrides, Flags] =
macro TransformerDefinitionMacros.withSealedSubtypeRenamedImpl[From, To, Overrides, Flags, FromSubtype, ToSubtype]

/** 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]

/** Use `f` instead of the primary constructor to construct the `To` value.
*
* Macro will read the names of Eta-expanded method's/lambda's parameters and try to match them with `From` getters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,25 @@ final class TransformerInto[From, To, Overrides <: TransformerOverrides, Flags <
def withEnumCaseRenamed[FromSubtype, ToSubtype]: TransformerInto[From, To, ? <: TransformerOverrides, Flags] =
macro TransformerIntoMacros.withSealedSubtypeRenamedImpl[From, To, Overrides, Flags, FromSubtype, ToSubtype]

/** 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]

/** Use `f` instead of the primary constructor to construct the `To` value.
*
* Macro will read the names of Eta-expanded method's/lambda's parameters and try to match them with `From` getters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,17 @@ class PartialTransformerDefinitionMacros(val c: whitebox.Context) extends utils.
]]
)

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]]

def withConstructorImpl[
From: WeakTypeTag,
To: WeakTypeTag,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,17 @@ class PartialTransformerIntoMacros(val c: whitebox.Context) extends utils.DslMac
.asInstanceOfExpr[PartialTransformerInto[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[PartialTransformerInto[From, To, Fallback[FromFallback, Path.Root, Overrides], Flags]]

def withConstructorToImpl[
From: WeakTypeTag,
To: WeakTypeTag,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ class TransformerDefinitionMacros(val c: whitebox.Context) extends utils.DslMacr
]]
)

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]]

def withConstructorImpl[
From: WeakTypeTag,
To: WeakTypeTag,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ class TransformerIntoMacros(val c: whitebox.Context) extends utils.DslMacroUtils
]]
)

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]]

def withConstructorImpl[
From: WeakTypeTag,
To: WeakTypeTag,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,27 @@ final class PartialTransformerDefinition[From, To, Overrides <: TransformerOverr
.withSealedSubtypeRenamedImpl[From, To, Overrides, Flags, FromSubtype, ToSubtype]('this)
}

/** 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) }

/** Use `f` instead of the primary constructor to construct the `To` value.
*
* Macro will read the names of Eta-expanded method's/lambda's parameters and try to match them with `From` getters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,27 @@ final class PartialTransformerInto[From, To, Overrides <: TransformerOverrides,
)
}

/** 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) }

/** Use `f` instead of the primary constructor to construct the `To` value.
*
* Macro will read the names of Eta-expanded method's/lambda's parameters and try to match them with `From` getters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,27 @@ final class TransformerDefinition[From, To, Overrides <: TransformerOverrides, 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) }

/** Use `f` instead of the primary constructor to construct the `To` value.
*
* Macro will read the names of Eta-expanded method's/lambda's parameters and try to match them with `From` getters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,27 @@ final class TransformerInto[From, To, Overrides <: TransformerOverrides, Flags <
: TransformerInto[From, To, ? <: TransformerOverrides, Flags] =
${ TransformerIntoMacros.withSealedSubtypeRenamedImpl[From, To, Overrides, Flags, FromSubtype, ToSubtype]('this) }

/** 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) }

/** Use `f` instead of the primary constructor to construct the `To` value.
*
* Macro will read the names of Eta-expanded method's/lambda's parameters and try to match them with `From` getters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,22 @@ object PartialTransformerDefinitionMacros {
]]
}

def withFallbackImpl[

Check warning on line 260 in chimney/src/main/scala-3/io/scalaland/chimney/internal/compiletime/dsl/PartialTransformerDefinitionMacros.scala

View check run for this annotation

Codecov / codecov/patch

chimney/src/main/scala-3/io/scalaland/chimney/internal/compiletime/dsl/PartialTransformerDefinitionMacros.scala#L260

Added line #L260 was not covered by tests
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

Check warning on line 271 in chimney/src/main/scala-3/io/scalaland/chimney/internal/compiletime/dsl/PartialTransformerDefinitionMacros.scala

View check run for this annotation

Codecov / codecov/patch

chimney/src/main/scala-3/io/scalaland/chimney/internal/compiletime/dsl/PartialTransformerDefinitionMacros.scala#L271

Added line #L271 was not covered by tests
.update($td, $fallback)
.asInstanceOf[PartialTransformerDefinition[From, To, Fallback[FromFallback, Path.Root, Overrides], Flags]]
}

def withConstructorImpl[
From: Type,
To: Type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,22 @@ object PartialTransformerIntoMacros {
]]
}

def withFallbackImpl[

Check warning on line 245 in chimney/src/main/scala-3/io/scalaland/chimney/internal/compiletime/dsl/PartialTransformerIntoMacros.scala

View check run for this annotation

Codecov / codecov/patch

chimney/src/main/scala-3/io/scalaland/chimney/internal/compiletime/dsl/PartialTransformerIntoMacros.scala#L245

Added line #L245 was not covered by tests
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)

Check warning on line 257 in chimney/src/main/scala-3/io/scalaland/chimney/internal/compiletime/dsl/PartialTransformerIntoMacros.scala

View check run for this annotation

Codecov / codecov/patch

chimney/src/main/scala-3/io/scalaland/chimney/internal/compiletime/dsl/PartialTransformerIntoMacros.scala#L256-L257

Added lines #L256 - L257 were not covered by tests
.asInstanceOf[PartialTransformerInto[From, To, Fallback[FromFallback, Path.Root, Overrides], Flags]]
}

def withConstructorImpl[
From: Type,
To: Type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,22 @@ object TransformerDefinitionMacros {
]]
}

def withFallbackImpl[

Check warning on line 154 in chimney/src/main/scala-3/io/scalaland/chimney/internal/compiletime/dsl/TransformerDefinitionMacros.scala

View check run for this annotation

Codecov / codecov/patch

chimney/src/main/scala-3/io/scalaland/chimney/internal/compiletime/dsl/TransformerDefinitionMacros.scala#L154

Added line #L154 was not covered by tests
From: Type,
To: Type,
Overrides <: TransformerOverrides: Type,
Flags <: TransformerFlags: Type,
FromFallback: Type
](
ti: Expr[TransformerDefinition[From, To, Overrides, Flags]],
fallback: Expr[FromFallback]
)(using Quotes): Expr[TransformerDefinition[From, To, ? <: TransformerOverrides, Flags]] =
'{
WithRuntimeDataStore
.update($ti, $fallback)

Check warning on line 166 in chimney/src/main/scala-3/io/scalaland/chimney/internal/compiletime/dsl/TransformerDefinitionMacros.scala

View check run for this annotation

Codecov / codecov/patch

chimney/src/main/scala-3/io/scalaland/chimney/internal/compiletime/dsl/TransformerDefinitionMacros.scala#L165-L166

Added lines #L165 - L166 were not covered by tests
.asInstanceOf[TransformerDefinition[From, To, Fallback[FromFallback, Path.Root, Overrides], Flags]]
}

def withConstructorImpl[
From: Type,
To: Type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,22 @@ object TransformerIntoMacros {
}
}(f)

def withFallbackImpl[

Check warning on line 172 in chimney/src/main/scala-3/io/scalaland/chimney/internal/compiletime/dsl/TransformerIntoMacros.scala

View check run for this annotation

Codecov / codecov/patch

chimney/src/main/scala-3/io/scalaland/chimney/internal/compiletime/dsl/TransformerIntoMacros.scala#L172

Added line #L172 was not covered by tests
From: Type,
To: Type,
Overrides <: TransformerOverrides: Type,
Flags <: TransformerFlags: Type,
FromFallback: Type
](
ti: Expr[TransformerInto[From, To, Overrides, Flags]],
fallback: Expr[FromFallback]
)(using Quotes): Expr[TransformerInto[From, To, ? <: TransformerOverrides, Flags]] =
'{
WithRuntimeDataStore
.update($ti, $fallback)

Check warning on line 184 in chimney/src/main/scala-3/io/scalaland/chimney/internal/compiletime/dsl/TransformerIntoMacros.scala

View check run for this annotation

Codecov / codecov/patch

chimney/src/main/scala-3/io/scalaland/chimney/internal/compiletime/dsl/TransformerIntoMacros.scala#L183-L184

Added lines #L183 - L184 were not covered by tests
.asInstanceOf[TransformerInto[From, To, Fallback[FromFallback, Path.Root, Overrides], Flags]]
}

def withConstructorToImpl[
From: Type,
To: Type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ object TransformerOverrides {
final class RenamedFrom[FromPath <: Path, ToPath <: Path, Tail <: Overrides] extends Overrides
// Computes a value from matched subtype, targeting another subtype
final class RenamedTo[FromPath <: Path, ToPath <: Path, Tail <: Overrides] extends Overrides
// Fallback value allowing merging several sources
final class Fallback[FromFallback, ToPath <: Path, Tail <: Overrides] extends Overrides
}

0 comments on commit d2f1c6d

Please sign in to comment.