Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ The format is based on [Keep a Changelog][chg] and this project adheres to

## Unreleased
- Add `messageGroupId` to SQS Attributes.
- Fix the type of `messageAttributes` in `SQSEvent`.
- Fix header parsing to accept `null` values serialised as `{}`

## `1.1` - 2023-12-18

Expand Down
7 changes: 6 additions & 1 deletion src/AWS/Lambda/Events/Kafka.hs
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,15 @@ instance FromJSON (Record' ByteString) where
partition <- o .: "partition"
offset <- o .: "offset"
timestamp <- parseTimestamp o
headers <- o .: "headers"
headers <- o .: "headers" >>= fmap catMaybes . traverse parseNullableHeader
key <- fmap (B64.decodeLenient . TE.encodeUtf8) <$> o .:? "key"
value <- fmap (B64.decodeLenient . TE.encodeUtf8) <$> o .:? "value"
pure Record { topic, partition, offset, timestamp, headers, key, value }
where
-- AWS serialises Kafka headers with null values as {}, which has no
-- key to extract. Skip these rather than failing.
parseNullableHeader (Object o) | KM.null o = pure Nothing
parseNullableHeader v = Just <$> parseJSON v

-- | Encodes keys and values into base64.
instance ToJSON (Record' ByteString) where
Expand Down
44 changes: 36 additions & 8 deletions src/AWS/Lambda/Events/SQS.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{-# LANGUAGE ApplicativeDo #-}
{-# LANGUAGE ApplicativeDo #-}
{-# LANGUAGE RecordWildCards #-}

{-|
Expand All @@ -13,13 +13,17 @@ Stability : stable
module AWS.Lambda.Events.SQS (
Records (..),
Attributes (..),
MessageAttributeValue (..),
SQSEvent (..)
) where

import Data.Aeson (FromJSON (..), withObject, (.:))
import Data.Map (Map)
import Data.Text (Text)
import GHC.Generics (Generic)
import Data.Aeson (FromJSON (..), withObject, (.:), (.:?))
import Data.ByteString (ByteString)
import qualified Data.ByteString.Base64 as B64
import Data.Map (Map)
import Data.Text (Text)
import qualified Data.Text.Encoding as TE
import GHC.Generics (Generic)

-- | Represents an event from AWS SQS.
--
Expand All @@ -37,7 +41,7 @@ data Attributes = Attributes {
sentTimestamp :: Text,
senderId :: Text,
approximateFirstReceiveTimestamp :: Text,
messageGroupId :: Text
messageGroupId :: Maybe Text
} deriving (Show, Eq, Generic)

instance FromJSON Attributes where
Expand All @@ -46,19 +50,43 @@ instance FromJSON Attributes where
sentTimestamp <- v .: "SentTimestamp"
senderId <- v .: "SenderId"
approximateFirstReceiveTimestamp <- v .: "ApproximateFirstReceiveTimestamp"
messageGroupId <- v .: "MessageGroupId"
messageGroupId <- v .:? "MessageGroupId"
pure Attributes {..}

-- | An SQS message attribute value as it appears in Lambda SQS event payloads.
--
-- See the <https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html#example-standard-queue-message-event AWS Lambda SQS event payload example>
-- for the JSON shape used under @messageAttributes@.
data MessageAttributeValue = MessageAttributeValue {
stringValue :: Maybe Text,
binaryValue :: Maybe ByteString,
stringListValues :: [Text],
binaryListValues :: [ByteString],
dataType :: Text
} deriving (Show, Eq, Generic)

instance FromJSON MessageAttributeValue where
parseJSON = withObject "MessageAttributeValue" $ \v -> do
stringValue <- v .:? "stringValue"
binaryValue <- fmap decodeBase64Text <$> v .:? "binaryValue"
stringListValues <- maybe [] id <$> v .:? "stringListValues"
binaryListValues <- maybe [] (map decodeBase64Text) <$> v .:? "binaryListValues"
dataType <- v .: "dataType"
pure MessageAttributeValue {..}

data SQSEvent = SQSEvent {
messageId :: Text,
receiptHandle :: Text,
body :: Text,
attributes :: Attributes,
messageAttributes :: Map Text Text,
messageAttributes :: Map Text MessageAttributeValue,
md5OfBody :: Text,
eventSource :: Text,
eventSourceARN :: Text,
awsRegion :: Text
} deriving (Show, Eq, Generic)

instance FromJSON SQSEvent

decodeBase64Text :: Text -> ByteString
decodeBase64Text = B64.decodeLenient . TE.encodeUtf8
36 changes: 31 additions & 5 deletions test/AWS/Lambda/Events/SQS/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,18 @@ samplePayload = [r|
"MessageGroupId": "group-1"
},
"messageAttributes": {
"attribute1": "value1",
"attribute2": "value2"
"attribute1": {
"stringValue": "value1",
"stringListValues": [],
"binaryListValues": [],
"dataType": "String"
},
"attribute2": {
"binaryValue": "dmFsdWUy",
"stringListValues": [],
"binaryListValues": [],
"dataType": "Binary"
}
},
"md5OfBody": "9a0364b9e99bb480dd25e1f0284c8555",
"eventSource": "aws:sqs",
Expand All @@ -54,11 +64,27 @@ expectedRecords = Records
, sentTimestamp = "1523232000000"
, senderId = "123456789012"
, approximateFirstReceiveTimestamp = "1523232000001"
, messageGroupId = "group-1"
, messageGroupId = Just "group-1"
}
, messageAttributes = M.fromList
[ ("attribute1", "value1")
, ("attribute2", "value2")
[ ( "attribute1"
, MessageAttributeValue
{ stringValue = Just "value1"
, binaryValue = Nothing
, stringListValues = []
, binaryListValues = []
, dataType = "String"
}
)
, ( "attribute2"
, MessageAttributeValue
{ stringValue = Nothing
, binaryValue = Just "value2"
, stringListValues = []
, binaryListValues = []
, dataType = "Binary"
}
)
]
, md5OfBody = "9a0364b9e99bb480dd25e1f0284c8555"
, eventSource = "aws:sqs"
Expand Down