{-# LANGUAGE OverloadedStrings #-}
module Kafka.Consumer.Subscription
( Subscription(..)
, topics
, offsetReset
, extraSubscriptionProps
)
where
import Data.Map (Map)
import qualified Data.Map as M
import Data.Semigroup as Sem
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Text (Text)
import Kafka.Consumer.Types (OffsetReset (..))
import Kafka.Types (TopicName (..))
data Subscription = Subscription (Set TopicName) (Map Text Text)
instance Sem.Semigroup Subscription where
(Subscription ts1 :: Set TopicName
ts1 m1 :: Map Text Text
m1) <> :: Subscription -> Subscription -> Subscription
<> (Subscription ts2 :: Set TopicName
ts2 m2 :: Map Text Text
m2) =
let ts' :: Set TopicName
ts' = Set TopicName -> Set TopicName -> Set TopicName
forall a. Ord a => Set a -> Set a -> Set a
Set.union Set TopicName
ts1 Set TopicName
ts2
ps' :: Map Text Text
ps' = Map Text Text -> Map Text Text -> Map Text Text
forall k a. Ord k => Map k a -> Map k a -> Map k a
M.union Map Text Text
m1 Map Text Text
m2
in Set TopicName -> Map Text Text -> Subscription
Subscription Set TopicName
ts' Map Text Text
ps'
{-# INLINE (<>) #-}
instance Monoid Subscription where
mempty :: Subscription
mempty = Set TopicName -> Map Text Text -> Subscription
Subscription Set TopicName
forall a. Set a
Set.empty Map Text Text
forall k a. Map k a
M.empty
{-# INLINE mempty #-}
mappend :: Subscription -> Subscription -> Subscription
mappend = Subscription -> Subscription -> Subscription
forall a. Semigroup a => a -> a -> a
(Sem.<>)
{-# INLINE mappend #-}
topics :: [TopicName] -> Subscription
topics :: [TopicName] -> Subscription
topics ts :: [TopicName]
ts = Set TopicName -> Map Text Text -> Subscription
Subscription ([TopicName] -> Set TopicName
forall a. Ord a => [a] -> Set a
Set.fromList [TopicName]
ts) Map Text Text
forall k a. Map k a
M.empty
offsetReset :: OffsetReset -> Subscription
offsetReset :: OffsetReset -> Subscription
offsetReset o :: OffsetReset
o =
let o' :: Text
o' = case OffsetReset
o of
Earliest -> "earliest"
Latest -> "latest"
in Set TopicName -> Map Text Text -> Subscription
Subscription Set TopicName
forall a. Set a
Set.empty ([(Text, Text)] -> Map Text Text
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList [("auto.offset.reset", Text
o')])
extraSubscriptionProps :: Map Text Text -> Subscription
= Set TopicName -> Map Text Text -> Subscription
Subscription Set TopicName
forall a. Set a
Set.empty