{-# LANGUAGE OverloadedStrings #-}

-----------------------------------------------------------------------------
-- |
-- Module with subscription types and functions.
-----------------------------------------------------------------------------
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 (..))

-- | A consumer subscription to a topic.
--
-- ==== __Examples__
--
-- Typically you don't call the constructor directly, but combine settings:
--
-- @
-- consumerSub :: 'Subscription'
-- consumerSub = 'topics' ['TopicName' "kafka-client-example-topic"]
--            <> 'offsetReset' 'Earliest'
--            <> 'extraSubscriptionProps' (fromList [("prop1", "value 1"), ("prop2", "value 2")])
-- @
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 #-}

-- | Build a subscription by giving the list of topic names only
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

-- | Build a subscription by giving the offset reset parameter only
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')])

-- | Build a subscription by giving extra properties only
extraSubscriptionProps :: Map Text Text -> Subscription
extraSubscriptionProps :: Map Text Text -> Subscription
extraSubscriptionProps = Set TopicName -> Map Text Text -> Subscription
Subscription Set TopicName
forall a. Set a
Set.empty