Default values support

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

Automatic value provision

Field’s default value is automatically used as a target value when constructing target object.

case class Catterpillar(size: Int, name: String)
case class Butterfly(size: Int, name: String, wingsColor: String = "purple")

val stevie = Catterpillar(5, "Steve")

val steve = stevie.transformInto[Butterfly]
// Butterfly(5, "Steve", "purple")

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

val steve = stevie.into[Butterfly]
  .withFieldConst(_.wingsColor, "yellow")
// 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.

val steve = stevie
// error: Chimney can't derive transformation from Catterpillar to Butterfly
// Butterfly
//   wingsColor: String - no field named wingsColor in source type Catterpillar
// Consult for usage examples.
//            .transform
//            ^

Default values for Option fields

In case you have added an optional field to a type, wanting to write migration from old data, usually you set new optional type to None.

case class Foo(a: Int, b: String)
case class FooV2(a: Int, b: String, newField: Option[Double])

Usual approach would be to use .withFieldConst to set new field value or give newField field a default value.

Foo(5, "test")
  .withFieldConst(_.newField, None)
// FooV2(5, "test", None)

At some scale this may turn out to be cumbersome. Therefore, it’s possible to handle such Option field values for which we can’t find counterpart in source data type as None by default. You just need to enable this behavior by using .enableOptionDefaultsToNone operation.

Foo(5, "test")
// FooV2(5, "test", None)

Default values for Unit fields

Having a target case class type that contains a field of type Unit, Chimney is able to automatically fill with unit value (()).

case class Foo(x: Int, y: String)
case class Bar(x: Int, y: String, z: Unit)

Foo(10, "test").transformInto[Bar]
// Foo(10, test, ())