Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default values usage require explicit enabling (changed default setting) #227

Merged
merged 4 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ sbt makeSite

HTML Documentation should be generated at `target/sphinx/html/index.html`.

Alternatively use Docker:

```bash
docker run --rm -v ./docs:/docs sphinxdoc/sphinx:3.2.1 bash -c "pip install sphinx-rtd-theme && make html"
```

## Thanks

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ trait FlagsDsl[UpdateFlag[_ <: TransformerFlags], Flags <: TransformerFlags] {

/** Enable fallback to default case class values in `To` type.
*
* @see [[https://scalalandio.github.io/chimney/transformers/default-values.html#disabling-default-values-in-generated-transformer]] for more details
* By default in such case derivation will fail. By enabling this flag, derivation will fallback to default value.
*
* @see [[https://scalalandio.github.io/chimney/transformers/default-values.html#enabling-default-values-in-generated-transformer]] for more details
*/
def enableDefaultValues: UpdateFlag[Enable[DefaultValues, Flags]] =
enableFlag[DefaultValues]

/** Fail derivation if `From` type is missing field even if `To` has default value for it.
*
* By default in such case derivation will fallback to default values.
*
* @see [[https://scalalandio.github.io/chimney/transformers/default-values.html#disabling-default-values-in-generated-transformer]] for more details
* @see [[https://scalalandio.github.io/chimney/transformers/default-values.html#enabling-default-values-in-generated-transformer]] for more details
*/
def disableDefaultValues: UpdateFlag[Disable[DefaultValues, Flags]] =
disableFlag[DefaultValues]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ trait TransformerConfigSupport extends MacroUtils {

case class TransformerFlags(
methodAccessors: Boolean = false,
processDefaultValues: Boolean = true,
processDefaultValues: Boolean = false,
beanSetters: Boolean = false,
beanGetters: Boolean = false,
optionDefaultsToNone: Boolean = false,
Expand Down
8 changes: 6 additions & 2 deletions chimney/src/test/scala/io/scalaland/chimney/DslSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,11 @@ object DslSpec extends TestSuite {
}

"not use None as default when other default value is set" - {
SomeFoo("foo").into[Foobar2].transform ==> Foobar2("foo", Some(42))
SomeFoo("foo").into[Foobar2].enableOptionDefaultsToNone.transform ==> Foobar2("foo", Some(42))
SomeFoo("foo").into[Foobar2].enableDefaultValues.transform ==> Foobar2("foo", Some(42))
SomeFoo("foo").into[Foobar2].enableDefaultValues.enableOptionDefaultsToNone.transform ==> Foobar2(
"foo",
Some(42)
)
}

"not compile if default value is missing and no .enableOptionDefaultsToNone" - {
Expand Down Expand Up @@ -199,6 +202,7 @@ object DslSpec extends TestSuite {
case class Baahr(x: Int, y: Bar)

"use default parameter value" - {
implicit val cfg = TransformerConfiguration.default.enableDefaultValues

"field does not exists - the source" - {
Foo(10).transformInto[Bar] ==> Bar(10, 30L)
Expand Down
52 changes: 23 additions & 29 deletions docs/source/transformers/default-values.rst
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
Default values support
======================

Chimney respects case classes' default values as a possible target
Chimney allows case classes' default values as a possible target
field value source.

Automatic value provision
-------------------------
Enabling default values in generated transformer
------------------------------------------------

Field's default value is automatically used as a target value when constructing
target object.
.. warning::

Prior to version 0.7.0 fallback to default values was automatically enabled
and required explicit disabling.

Field's default value can be enabled as a target value when constructing
target object. The support for them has to be explicitly enabled to avoid
accidents.

.. code-block:: scala

Expand All @@ -17,42 +23,30 @@ target object.

val stevie = Catterpillar(5, "Steve")

val steve = stevie.transformInto[Butterfly]
//val steve = stevie.transformInto[Butterfly] // fails with
// error: Chimney can't derive transformation from Catterpillar to Butterfly
//
// Butterfly
// wingsColor: String - no field named wingsColor in source type Catterpillar
//
// Consult https://scalalandio.github.io/chimney for usage examples.
//
// .transform
// ^
val steve = stevie.into[Butterfly].enableDefaultValues.transform
// Butterfly(5, "Steve", "purple")

Providing the value manually has always a priority over the default.

.. code-block:: scala

val steve = stevie.into[Butterfly]
.enableDefaultValues
.withFieldConst(_.wingsColor, "yellow")
.transform
// Butterfly(5, "Steve", "yellow")


Disabling default values in generated transformer
-------------------------------------------------

Using ``.disableDefaultValues`` operation it's possible to disable
lookup for default values and require them always to be passed explicitly.

.. code-block:: scala

val steve = stevie
.into[Butterfly]
.disableDefaultValues
.transform
// error: Chimney can't derive transformation from Catterpillar to Butterfly
//
// Butterfly
// wingsColor: String - no field named wingsColor in source type Catterpillar
//
// Consult https://scalalandio.github.io/chimney for usage examples.
//
// .transform
// ^


Default values for ``Option`` fields
------------------------------------

Expand Down