Skip to content

Commit

Permalink
refactor: Encoding as enum instead of sealed trait
Browse files Browse the repository at this point in the history
  • Loading branch information
jachro committed Feb 1, 2024
1 parent c670d9a commit 4711cc2
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,7 @@ final case class Message(id: MessageId, encoding: Encoding, payload: ByteVector)

final case class MessageId(value: String) extends AnyVal

sealed trait Encoding extends Product:
enum Encoding:
lazy val name: String = productPrefix

object Encoding:

val all: Set[Encoding] = Set(Binary, Json)

def from(v: String): Either[IllegalArgumentException, Encoding] =
all
.find(_.productPrefix.equalsIgnoreCase(v))
.toRight(new IllegalArgumentException(s"'$v' not a valid payload Encoding"))

case object Binary extends Encoding
case object Json extends Encoding
case Binary
case Json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class RedisQueueClient[F[_]: Async: Log](client: RedisClient) extends QueueClien
ByteVector.encodeUtf8(encoding.name).fold(throw _, identity)

private def decodeEncoding(encoding: ByteVector): Encoding =
encoding.decodeUtf8.flatMap(Encoding.from).fold(throw _, identity)
encoding.decodeUtf8.map(Encoding.valueOf).fold(throw _, identity)

override def acquireEventsStream(
queueName: QueueName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ object RedisClientGenerators:
.flatMap(Gen.stringOfN(_, alphaLowerChar).map(QueueName(_)))

val encodingGen: Gen[Encoding] =
Gen.oneOf(Encoding.all)
Gen.oneOf(Encoding.values.toSet)

val clientIdGen: Gen[ClientId] =
Gen
Expand Down

0 comments on commit 4711cc2

Please sign in to comment.