Skip to content

Commit

Permalink
Declare queue type
Browse files Browse the repository at this point in the history
  • Loading branch information
geirolz committed May 31, 2024
1 parent 402a870 commit 723f7c2
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package dev.profunktor.fs2rabbit.algebra

import cats.effect.Sync
import cats.syntax.functor._
import cats.syntax.all._
import dev.profunktor.fs2rabbit.arguments._
import dev.profunktor.fs2rabbit.config.declaration.{DeclarationExchangeConfig, DeclarationQueueConfig}
import dev.profunktor.fs2rabbit.effects.BoolValue.syntax._
Expand Down Expand Up @@ -63,26 +63,30 @@ object Declaration {
}

override def declareQueue(channel: AMQPChannel, config: DeclarationQueueConfig): F[Unit] =
Sync[F].blocking {
channel.value.queueDeclare(
config.queueName.value,
config.durable.isTrue,
config.exclusive.isTrue,
config.autoDelete.isTrue,
config.arguments
)
}.void
Sync[F].fromEither(config.validatedArguments).flatMap { args =>
Sync[F].blocking {
channel.value.queueDeclare(
config.queueName.value,
config.durable.isTrue,
config.exclusive.isTrue,
config.autoDelete.isTrue,
args
)
}.void
}

override def declareQueueNoWait(channel: AMQPChannel, config: DeclarationQueueConfig): F[Unit] =
Sync[F].blocking {
channel.value.queueDeclareNoWait(
config.queueName.value,
config.durable.isTrue,
config.exclusive.isTrue,
config.autoDelete.isTrue,
config.arguments
)
}.void
Sync[F].fromEither(config.validatedArguments).flatMap { args =>
Sync[F].blocking {
channel.value.queueDeclareNoWait(
config.queueName.value,
config.durable.isTrue,
config.exclusive.isTrue,
config.autoDelete.isTrue,
args
)
}.void
}

override def declareQueuePassive(channel: AMQPChannel, queueName: QueueName): F[Unit] =
Sync[F].blocking {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package dev.profunktor.fs2rabbit.config

import dev.profunktor.fs2rabbit.arguments.Arguments
import dev.profunktor.fs2rabbit.model.{ExchangeName, ExchangeType, QueueName}
import dev.profunktor.fs2rabbit.model.{ExchangeName, ExchangeType, QueueName, QueueType}

object declaration {

Expand All @@ -26,12 +26,44 @@ object declaration {
durable: DurableCfg,
exclusive: ExclusiveCfg,
autoDelete: AutoDeleteCfg,
arguments: Arguments
)
arguments: Arguments,
queueType: Option[QueueType]
) {

lazy val validatedArguments: Either[IllegalArgumentException, Arguments] =
queueType match {
case Some(_) if arguments.contains("x-queue-type") =>
Left(
new IllegalArgumentException(
"Queue type defined twice. It is set in the arguments and in the DeclarationQueueConfig."
)
)
case Some(queueType) =>
Right(arguments + ("x-queue-type" -> queueType.asString))
case None =>
Right(arguments)
}
}
object DeclarationQueueConfig {

def default(queueName: QueueName): DeclarationQueueConfig =
DeclarationQueueConfig(queueName, NonDurable, NonExclusive, NonAutoDelete, Map.empty)
DeclarationQueueConfig(
queueName = queueName,
durable = NonDurable,
exclusive = NonExclusive,
autoDelete = NonAutoDelete,
arguments = Map.empty,
queueType = None
)

def classic(queueName: QueueName): DeclarationQueueConfig =
default(queueName).copy(queueType = Some(QueueType.Classic))

def quorum(queueName: QueueName): DeclarationQueueConfig =
default(queueName).copy(queueType = Some(QueueType.Quorum))

def stream(queueName: QueueName): DeclarationQueueConfig =
default(queueName).copy(queueType = Some(QueueType.Stream))
}

sealed trait DurableCfg extends Product with Serializable
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/scala/dev/profunktor/fs2rabbit/model.scala
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ object model {
sealed trait QueueType extends Product with Serializable {
def asString: String = this match {
case QueueType.Classic => "classic"
case QueueType.Quorum => "quorum"
case QueueType.Stream => "stream"
case QueueType.Quorum => "quorum"
case QueueType.Stream => "stream"
}
}
object QueueType {
case object Classic extends QueueType
case object Classic extends QueueType
case object Quorum extends QueueType
case object Stream extends QueueType
}
Expand Down

0 comments on commit 723f7c2

Please sign in to comment.