diff --git a/CHANGELOG.md b/CHANGELOG.md index babeffc..eb8459c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/AWS/Lambda/Events/Kafka.hs b/src/AWS/Lambda/Events/Kafka.hs index 38d287c..0fab239 100644 --- a/src/AWS/Lambda/Events/Kafka.hs +++ b/src/AWS/Lambda/Events/Kafka.hs @@ -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 diff --git a/src/AWS/Lambda/Events/SQS.hs b/src/AWS/Lambda/Events/SQS.hs index 69210c1..22cb9ed 100644 --- a/src/AWS/Lambda/Events/SQS.hs +++ b/src/AWS/Lambda/Events/SQS.hs @@ -1,4 +1,4 @@ -{-# LANGUAGE ApplicativeDo #-} +{-# LANGUAGE ApplicativeDo #-} {-# LANGUAGE RecordWildCards #-} {-| @@ -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. -- @@ -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 @@ -46,15 +50,36 @@ 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 +-- 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, @@ -62,3 +87,6 @@ data SQSEvent = SQSEvent { } deriving (Show, Eq, Generic) instance FromJSON SQSEvent + +decodeBase64Text :: Text -> ByteString +decodeBase64Text = B64.decodeLenient . TE.encodeUtf8 diff --git a/test/AWS/Lambda/Events/SQS/Spec.hs b/test/AWS/Lambda/Events/SQS/Spec.hs index 156ae7e..293820c 100644 --- a/test/AWS/Lambda/Events/SQS/Spec.hs +++ b/test/AWS/Lambda/Events/SQS/Spec.hs @@ -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", @@ -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"