How can I add a Configuration Description manually for an Algebraic Data Type with ZIO Conf.
In the examples I found an example on how to handle ADTs with Magnolia.
Is this also possible when adding manually a Description of the Configuration?
Here an example:
sealed trait Dance
final case class A(any: Person) extends Dance
final case class B(body: Height) extends Dance
final case class Person(name: String, age: Option[Int])
final case class Height(height: Long)
With Magnolia:
val danceConfig = description[Dance]
Manually:
val danceConfig = ???
It is verbose as you would expect. But there are different ways of doing it, and it's a matter of preference.
We tried to be bit more verbose than required in both these options for better understanding
Option 1:
A bit convoluted during the write side, but it is all type driven.
Option 2
You would already note, during the write part (second arg to xmapEither) we make sure it is the correct type. Example: In
aConfigAsDance
, it is unsafe to assume it can only be A and do anasInstanceOf
.With
xmapeither
we are able to write safe and pure code and we followed it.In future, zio-config will come up with some helper functions to deal with Either. This is because ZIO-Config philosophy is to provide as less magical interface to the user as possible, while you can still use zio-config-magnolia to shorten them to just one line, which is
Good to have this example back in zio-config if you are interested. Thanks a lot for this question, and hope the answer is helpful.