zio-config icon indicating copy to clipboard operation
zio-config copied to clipboard

no way to derive a string descriptor for sealed trait/case object "enum"

Open vlushn opened this issue 2 years ago • 2 comments

A typical representation of enums (in Scala 2) is the following:

sealed trait Status
case object Succeeded extends Status
case object Failed extends Status
case object Unknown extends Status

case class State(status: Status, a: String)

Currently, with the magnolia auto-derivation, this have the following representation (using JSON syntax for simplicity):

{
  "status": {
    "Unknown": {} 
  },
  "a": "foobar"
}

There needs to be a way to derive the descriptor such that the Status enum can be represented by a string.

vlushn avatar Oct 14 '22 09:10 vlushn

I am not sure about this behaviour

The following works for me:

import zio.config._, typesafe._, magnolia._

import zio._
import zio.Console._

object MyApp extends ZIOAppDefault {

  sealed trait Status
  case object Succeeded extends Status
  case object Failed extends Status
  case object Unknown extends Status

  case class State(status: Status, a: String)

  val str =
    s"""
     "status" : "Unknown",
      "a"     : "foo"
   
   """

  val myAppLogic = {


    for {

      str <- read(descriptor[State] from ConfigSource.fromHoconString(str))
      _ <- ZIO.succeed(println(str))
    } yield ()
  }

  def run = myAppLogic

}

afsalthaj avatar Nov 06 '22 13:11 afsalthaj

Try this in scastie, and see how it goes: https://scastie.scala-lang.org/afsalthaj/JjQZyUNHQSar9OCQN2VJzQ/1

afsalthaj avatar Nov 06 '22 13:11 afsalthaj