@@ -4,15 +4,15 @@ import NIO
44/// https://github.com/aws/aws-lambda-go/blob/master/events/sqs.go
55public struct SQS {
66
7- public struct Event : Codable {
7+ public struct Event : Decodable {
88 public let records : [ Message ]
99
1010 enum CodingKeys : String , CodingKey {
1111 case records = " Records "
1212 }
1313 }
1414
15- public struct Message : Codable {
15+ public struct Message : DecodableBody {
1616
1717 /// https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_MessageAttributeValue.html
1818 public enum Attribute {
@@ -23,28 +23,49 @@ public struct SQS {
2323
2424 public let messageId : String
2525 public let receiptHandle : String
26- public let body : String
26+ public let body : String ?
2727 public let md5OfBody : String
2828 public let md5OfMessageAttributes : String ?
2929 public let attributes : [ String : String ]
3030 public let messageAttributes : [ String : Attribute ]
3131 public let eventSourceArn : String
3232 public let eventSource : String
3333 public let awsRegion : String
34+ }
35+ }
36+
37+ extension SQS . Message : Decodable {
38+
39+ enum CodingKeys : String , CodingKey {
40+ case messageId
41+ case receiptHandle
42+ case body
43+ case md5OfBody
44+ case md5OfMessageAttributes
45+ case attributes
46+ case messageAttributes
47+ case eventSourceArn = " eventSourceARN "
48+ case eventSource
49+ case awsRegion
50+ }
51+
52+ public init ( from decoder: Decoder ) throws {
53+
54+ let container = try decoder. container ( keyedBy: CodingKeys . self)
55+ self . messageId = try container. decode ( String . self, forKey: . messageId)
56+ self . receiptHandle = try container. decode ( String . self, forKey: . receiptHandle)
57+ self . md5OfBody = try container. decode ( String . self, forKey: . md5OfBody)
58+ self . md5OfMessageAttributes = try container. decodeIfPresent ( String . self, forKey: . md5OfMessageAttributes)
59+ self . attributes = try container. decode ( [ String : String ] . self, forKey: . attributes)
60+ self . messageAttributes = try container. decode ( [ String : Attribute ] . self, forKey: . messageAttributes)
61+ self . eventSourceArn = try container. decode ( String . self, forKey: . eventSourceArn)
62+ self . eventSource = try container. decode ( String . self, forKey: . eventSource)
63+ self . awsRegion = try container. decode ( String . self, forKey: . awsRegion)
3464
35- enum CodingKeys : String , CodingKey {
36- case messageId
37- case receiptHandle
38- case body
39- case md5OfBody
40- case md5OfMessageAttributes
41- case attributes
42- case messageAttributes
43- case eventSourceArn = " eventSourceARN "
44- case eventSource
45- case awsRegion
46- }
65+ let body = try container. decode ( String ? . self, forKey: . body)
66+ self . body = body != " " ? body : nil
4767 }
68+
4869}
4970
5071extension SQS . Message . Attribute : Equatable { }
0 commit comments