{-|
Module      : AWS.Lambda.Events.SQS
Description : Data types for working with SQS events.
Copyright   : (c) Nike, Inc., 2019
License     : BSD3
Maintainer  : nathan.fairhurst@nike.com, fernando.freire@nike.com
Stability   : stable
-}

module AWS.Lambda.Events.SQS (
  Records (..),
  Attributes (..),
  SQSEvent (..)
) where

import Data.Aeson   (FromJSON (..), withObject, (.:))
import Data.Map     (Map)
import Data.Text    (Text)
import GHC.Generics (Generic)

newtype Records = Records {
  Records -> [SQSEvent]
records :: [SQSEvent]
} deriving (Int -> Records -> ShowS
[Records] -> ShowS
Records -> String
(Int -> Records -> ShowS)
-> (Records -> String) -> ([Records] -> ShowS) -> Show Records
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Records] -> ShowS
$cshowList :: [Records] -> ShowS
show :: Records -> String
$cshow :: Records -> String
showsPrec :: Int -> Records -> ShowS
$cshowsPrec :: Int -> Records -> ShowS
Show, Records -> Records -> Bool
(Records -> Records -> Bool)
-> (Records -> Records -> Bool) -> Eq Records
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Records -> Records -> Bool
$c/= :: Records -> Records -> Bool
== :: Records -> Records -> Bool
$c== :: Records -> Records -> Bool
Eq)

instance FromJSON Records where
  parseJSON :: Value -> Parser Records
parseJSON = String -> (Object -> Parser Records) -> Value -> Parser Records
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"Records" ((Object -> Parser Records) -> Value -> Parser Records)
-> (Object -> Parser Records) -> Value -> Parser Records
forall a b. (a -> b) -> a -> b
$ \Object
v -> [SQSEvent] -> Records
Records ([SQSEvent] -> Records) -> Parser [SQSEvent] -> Parser Records
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v Object -> Text -> Parser [SQSEvent]
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"Records"

data Attributes = Attributes {
  Attributes -> Text
approximateReceiveCount          :: Text,
  Attributes -> Text
sentTimestamp                    :: Text,
  Attributes -> Text
senderId                         :: Text,
  Attributes -> Text
approximateFirstReceiveTimestamp :: Text
} deriving (Int -> Attributes -> ShowS
[Attributes] -> ShowS
Attributes -> String
(Int -> Attributes -> ShowS)
-> (Attributes -> String)
-> ([Attributes] -> ShowS)
-> Show Attributes
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Attributes] -> ShowS
$cshowList :: [Attributes] -> ShowS
show :: Attributes -> String
$cshow :: Attributes -> String
showsPrec :: Int -> Attributes -> ShowS
$cshowsPrec :: Int -> Attributes -> ShowS
Show, Attributes -> Attributes -> Bool
(Attributes -> Attributes -> Bool)
-> (Attributes -> Attributes -> Bool) -> Eq Attributes
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Attributes -> Attributes -> Bool
$c/= :: Attributes -> Attributes -> Bool
== :: Attributes -> Attributes -> Bool
$c== :: Attributes -> Attributes -> Bool
Eq)

instance FromJSON Attributes where
  parseJSON :: Value -> Parser Attributes
parseJSON = String
-> (Object -> Parser Attributes) -> Value -> Parser Attributes
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"Attributes" ((Object -> Parser Attributes) -> Value -> Parser Attributes)
-> (Object -> Parser Attributes) -> Value -> Parser Attributes
forall a b. (a -> b) -> a -> b
$ \Object
v ->
    Text -> Text -> Text -> Text -> Attributes
Attributes
      (Text -> Text -> Text -> Text -> Attributes)
-> Parser Text -> Parser (Text -> Text -> Text -> Attributes)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v Object -> Text -> Parser Text
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"ApproximateReceiveCount"
      Parser (Text -> Text -> Text -> Attributes)
-> Parser Text -> Parser (Text -> Text -> Attributes)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Text -> Parser Text
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"SentTimestamp"
      Parser (Text -> Text -> Attributes)
-> Parser Text -> Parser (Text -> Attributes)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Text -> Parser Text
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"SenderId"
      Parser (Text -> Attributes) -> Parser Text -> Parser Attributes
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Text -> Parser Text
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"ApproximateFirstReceiveTimestamp"

data SQSEvent = SQSEvent {
  SQSEvent -> Text
messageId         :: Text,
  SQSEvent -> Text
receiptHandle     :: Text,
  SQSEvent -> Text
body              :: Text,
  SQSEvent -> Attributes
attributes        :: Attributes,
  SQSEvent -> Map Text Text
messageAttributes :: Map Text Text,
  SQSEvent -> Text
md5OfBody         :: Text,
  SQSEvent -> Text
eventSource       :: Text,
  SQSEvent -> Text
eventSourceARN    :: Text,
  SQSEvent -> Text
awsRegion         :: Text
} deriving (Int -> SQSEvent -> ShowS
[SQSEvent] -> ShowS
SQSEvent -> String
(Int -> SQSEvent -> ShowS)
-> (SQSEvent -> String) -> ([SQSEvent] -> ShowS) -> Show SQSEvent
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SQSEvent] -> ShowS
$cshowList :: [SQSEvent] -> ShowS
show :: SQSEvent -> String
$cshow :: SQSEvent -> String
showsPrec :: Int -> SQSEvent -> ShowS
$cshowsPrec :: Int -> SQSEvent -> ShowS
Show, SQSEvent -> SQSEvent -> Bool
(SQSEvent -> SQSEvent -> Bool)
-> (SQSEvent -> SQSEvent -> Bool) -> Eq SQSEvent
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SQSEvent -> SQSEvent -> Bool
$c/= :: SQSEvent -> SQSEvent -> Bool
== :: SQSEvent -> SQSEvent -> Bool
$c== :: SQSEvent -> SQSEvent -> Bool
Eq, (forall x. SQSEvent -> Rep SQSEvent x)
-> (forall x. Rep SQSEvent x -> SQSEvent) -> Generic SQSEvent
forall x. Rep SQSEvent x -> SQSEvent
forall x. SQSEvent -> Rep SQSEvent x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep SQSEvent x -> SQSEvent
$cfrom :: forall x. SQSEvent -> Rep SQSEvent x
Generic)

instance FromJSON SQSEvent