Use optional StringEnumEntry field in case class with Quill in Scala

480 Views Asked by At

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 and nullable MySQL types.

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

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

and with a SQL table like this (topic_type can be null):

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

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)

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

0

There are 0 best solutions below