chimney icon indicating copy to clipboard operation
chimney copied to clipboard

Provide fallback value for a particular type

Open MateuszKubuszok opened this issue 1 year ago • 0 comments

In some cases there is one value which could be used as a default value for a certain type, but:

  • we would like to avoid passing it manually into every field which uses it
  • we would like to avoid enabling default values (because we want to fill that value but not other default values)

An examples are UnknownFieldSet values appearing in every protobuf which has preserve_unknown_fields = true. We might not want to enable default values for this field, and accidentally also fill another field which we would prefer to fail until we filled it ourselves.

Config-based approach

We could provide a solution in form:

domain.into[Protobuf]
  .withFieldTypeDefault(UnknownFieldSet())
  .transform

Implementation would require:

  • adding a flag in runtime.TransformerCfg
  • parsing it by macros config
  • using it in ProductToProductRule
  • testing

"Hardcore" version would be implementing that in TransformerConfiguration, and kicking off runtime overrides shared across whole scope with implicits.

Implicit-based approach

Alternatively, we might consider creating

package io.scalaland.chimney.integrations

trait DefaultValue[A] {
  def provide(): A
}

since #505 already paved a way for providing integrations through implicits.

MateuszKubuszok avatar Oct 18 '23 11:10 MateuszKubuszok