lloydmeta / enumeratum

A type-safe, reflection-free, powerful enumeration implementation for Scala with exhaustive pattern match warnings and helpful integrations.
MIT License
1.19k stars 146 forks source link

Use optional StringEnumEntry field in case class with Quill in Scala #256

Open saeedzr opened 4 years ago

saeedzr commented 4 years ago

We're using enumeratum successfully when dealing with enum values, both with Circe and with Quill. We define something like this:

sealed abstract class TopicType(val value: String) extends StringEnumEntry
  object TopicType extends StringEnum[TopicType] with StringCirceEnum[TopicType] with StringQuillEnum[TopicType] {
    object Info extends TopicType("info")
    object Warning extends TopicType("warning")

    val values: immutable.IndexedSeq[TopicType] = findValues
  }

  // case class representing our MySQL payload table 
  case class Payload(id: String, topic: TopicType)

and use Payload when decoding / encoding mysql and json using Quill and Circe. With Quill and MySQL this works fine with the following table:

create table payload(
    id varchar(36) not null primary key,
    topic_type enum ('info', 'warning')  not null
)

However, I cannot for the life of me figure out how to make this work when using optional fields for our enumeratum type.

We want to be able to use the payload class like this:

   case class Payload(id: String, topic: Option[TopicType])

When trying to use an optional topic and read from our database using Quill with :

...
run {
   query[Payload].filter(payload => payload.id == lift(someId))
}

we get:

java.util.NoSuchElementException: null is not a member of ValueEnum (warning, info) (ValueEnum.scala:58)

Same thing happens if I do a leftJoin to a table that holds an Enum in it, e.g.:

case class Payload(id: String)
case class PayloadData(payloadId: String, topic: TopicType)
run {
        query[Payload]
          .leftJoin(query[PayloadData])
          .on((p, pd) => p.id == pd.payloadId)
      }

Could it be that we need to specify some custom decoder / encoder to handle options / null values?

lloydmeta commented 4 years ago

Hmm, I'm honestly not too sure about this one because I haven't used Quill with this lib myself. Can you try seeing if someone on the Quill side knows?