This complex machine is for allowing us to keep our default values for the config ADT, but still validate the configs properly during decoding.
This complex machine is for allowing us to keep our default values for the config ADT, but still validate the
configs properly during decoding. Circe (at least the version available to us given Scala 2.11 constraint) silently
drops decoding errors when there's a default value, and just uses the default instead. This derivation does
something different:
- Derive the shapeless record type OR
that corresponds to the case class A
, but with all values wrapped in Option
.
- Derive the circe decoder for OR
, which allows values to be unspecified but does not drop validation errors.
- Put the optionalized record together with the defaults from the case class constructor, and do another round of
validation where a None
is replaced by the default (if it exists) or a validation error (if there is no default).
- Within the validated structure, go back to the original record type R
that corresponds to the case class and
use the shapeless LabelledGeneric to translate it to a validated case class instance.