@@ -4,15 +4,15 @@ import NIO
4
4
/// https://github.com/aws/aws-lambda-go/blob/master/events/sqs.go
5
5
public struct SQS {
6
6
7
- public struct Event : Codable {
7
+ public struct Event : Decodable {
8
8
public let records : [ Message ]
9
9
10
10
enum CodingKeys : String , CodingKey {
11
11
case records = " Records "
12
12
}
13
13
}
14
14
15
- public struct Message : Codable {
15
+ public struct Message : DecodableBody {
16
16
17
17
/// https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_MessageAttributeValue.html
18
18
public enum Attribute {
@@ -23,28 +23,49 @@ public struct SQS {
23
23
24
24
public let messageId : String
25
25
public let receiptHandle : String
26
- public let body : String
26
+ public let body : String ?
27
27
public let md5OfBody : String
28
28
public let md5OfMessageAttributes : String ?
29
29
public let attributes : [ String : String ]
30
30
public let messageAttributes : [ String : Attribute ]
31
31
public let eventSourceArn : String
32
32
public let eventSource : String
33
33
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)
34
64
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
47
67
}
68
+
48
69
}
49
70
50
71
extension SQS . Message . Attribute : Equatable { }
0 commit comments