All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.zeal18.zio.mongodb.bson.codecs.EnumCodec.scala Maven / Gradle / Ivy

package io.github.zeal18.zio.mongodb.bson.codecs

import org.bson.BsonWriter
import org.bson.BsonReader
import org.bson.BsonSerializationException
import org.bson.BsonInvalidOperationException
import org.bson.codecs.DecoderContext
import org.bson.codecs.EncoderContext
import io.github.zeal18.zio.mongodb.bson.codecs.error.BsonError

/** A codec for simple coproducts
  *
  * A simple coproduct is a sealed trait or an enum with all object members
  * or a Scala 3 parameterized enum.
  *
  * @param fullName full name of the type for better error reporting
  * @param nameByValue function to get a name by a coproduct case
  * @param valueByName function to get a coproduct case by its name
  */
case class EnumCodec[A](
  fullName: String,
  nameByValue: A => String,
  valueByName: String => Option[A],
) extends Codec[A]:
  override def encode(writer: BsonWriter, value: A, encoderContext: EncoderContext): Unit =
    try
      val shortName = nameByValue(value)
      writer.writeString(shortName)
    catch
      case e: BsonSerializationException =>
        throw BsonError.CodecError(
          fullName,
          BsonError.SerializationError(e),
        ) // scalafix:ok
      case e: BsonInvalidOperationException =>
        throw BsonError.CodecError(
          fullName,
          BsonError.SerializationError(e),
        ) // scalafix:ok

  override def decode(reader: BsonReader, decoderContext: DecoderContext): A =
    try
      val name = reader.readString()
      valueByName(name).fold[A](
        throw BsonError.CoproductError(
          name,
          BsonError.GeneralError("unsupported discriminator value"), // scalafix:ok
        ),
      )(identity[A])
    catch
      case e: BsonSerializationException =>
        throw BsonError.CodecError(fullName, BsonError.SerializationError(e)) // scalafix:ok
      case e: BsonInvalidOperationException =>
        throw BsonError.CodecError(fullName, BsonError.SerializationError(e)) // scalafix:ok
      case e: BsonError =>
        throw BsonError.CodecError(fullName, e) // scalafix:ok
end EnumCodec




© 2015 - 2025 Weber Informatics LLC | Privacy Policy