I have several enums where there companion object code is identical to one another.
How can I generate these companion objects where the difference between them is just the enum name?
enum Variants:
case A, B, C, D, ...
enum Groups:
case I, II, III, IV, ...
/** Same for Groups and all other enums **/
object Variants:
import zio.json.*
/* ZIO */
given JsonCodec[Variants] = JsonCodec[Variants](
JsonEncoder[String].contramap[Variants](_.toString),
JsonDecoder[String].mapOrFail(name =>
Try { Variants.valueOf(name) }.toEither.left.map(_.getMessage)
)
)
/* Tapir */
import sttp.tapir.*
given Codec[String, Variants, CodecFormat.TextPlain] =
Codec.derivedEnumeration[String, Variants].defaultStringBased
/* Quill */
import io.getquill.MappedEncoding
given MappedEncoding[Variants, String](_.toString)
given MappedEncoding[String, Variants](Variants.valueOf)