Skip to content

Commit 7a94606

Browse files
authored
Added SNS & SQS Events (#46)
* Added SNS & SQS Events * Added Base64 util
1 parent a810911 commit 7a94606

File tree

8 files changed

+824
-0
lines changed

8 files changed

+824
-0
lines changed

Sources/AWSLambdaEvents/SNS.swift

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftAWSLambdaRuntime open source project
4+
//
5+
// Copyright (c) 2017-2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import struct Foundation.Date
16+
17+
/// https://github.com/aws/aws-lambda-go/blob/master/events/sns.go
18+
public enum SNS {
19+
public struct Event: Decodable {
20+
public struct Record: Decodable {
21+
public let eventVersion: String
22+
public let eventSubscriptionArn: String
23+
public let eventSource: String
24+
public let sns: Message
25+
26+
public enum CodingKeys: String, CodingKey {
27+
case eventVersion = "EventVersion"
28+
case eventSubscriptionArn = "EventSubscriptionArn"
29+
case eventSource = "EventSource"
30+
case sns = "Sns"
31+
}
32+
}
33+
34+
public let records: [Record]
35+
36+
public enum CodingKeys: String, CodingKey {
37+
case records = "Records"
38+
}
39+
}
40+
41+
public struct Message {
42+
public enum Attribute {
43+
case string(String)
44+
case binary([UInt8])
45+
}
46+
47+
public let signature: String
48+
public let messageId: String
49+
public let type: String
50+
public let topicArn: String
51+
public let messageAttributes: [String: Attribute]
52+
public let signatureVersion: String
53+
54+
@ISO8601WithFractionalSecondsCoding
55+
public var timestamp: Date
56+
public let signingCertURL: String
57+
public let message: String
58+
public let unsubscribeUrl: String
59+
public let subject: String?
60+
}
61+
}
62+
63+
extension SNS.Message: Decodable {
64+
enum CodingKeys: String, CodingKey {
65+
case signature = "Signature"
66+
case messageId = "MessageId"
67+
case type = "Type"
68+
case topicArn = "TopicArn"
69+
case messageAttributes = "MessageAttributes"
70+
case signatureVersion = "SignatureVersion"
71+
case timestamp = "Timestamp"
72+
case signingCertURL = "SigningCertUrl"
73+
case message = "Message"
74+
case unsubscribeUrl = "UnsubscribeUrl"
75+
case subject = "Subject"
76+
}
77+
}
78+
79+
extension SNS.Message.Attribute: Equatable {}
80+
81+
extension SNS.Message.Attribute: Decodable {
82+
enum CodingKeys: String, CodingKey {
83+
case dataType = "Type"
84+
case dataValue = "Value"
85+
}
86+
87+
public init(from decoder: Decoder) throws {
88+
let container = try decoder.container(keyedBy: CodingKeys.self)
89+
90+
let dataType = try container.decode(String.self, forKey: .dataType)
91+
// https://docs.aws.amazon.com/sns/latest/dg/sns-message-attributes.html#SNSMessageAttributes.DataTypes
92+
switch dataType {
93+
case "String":
94+
let value = try container.decode(String.self, forKey: .dataValue)
95+
self = .string(value)
96+
case "Binary":
97+
let base64encoded = try container.decode(String.self, forKey: .dataValue)
98+
let bytes = try base64encoded.base64decoded()
99+
self = .binary(bytes)
100+
default:
101+
throw DecodingError.dataCorruptedError(forKey: .dataType, in: container, debugDescription: """
102+
Unexpected value \"\(dataType)\" for key \(CodingKeys.dataType).
103+
Expected `String` or `Binary`.
104+
""")
105+
}
106+
}
107+
}

Sources/AWSLambdaEvents/SQS.swift

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftAWSLambdaRuntime open source project
4+
//
5+
// Copyright (c) 2017-2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
/// https://github.com/aws/aws-lambda-go/blob/master/events/sqs.go
16+
public enum SQS {
17+
public struct Event: Decodable {
18+
public let records: [Message]
19+
20+
enum CodingKeys: String, CodingKey {
21+
case records = "Records"
22+
}
23+
}
24+
25+
public struct Message {
26+
/// https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_MessageAttributeValue.html
27+
public enum Attribute {
28+
case string(String)
29+
case binary([UInt8])
30+
case number(String)
31+
}
32+
33+
public let messageId: String
34+
public let receiptHandle: String
35+
public var body: String
36+
public let md5OfBody: String
37+
public let md5OfMessageAttributes: String?
38+
public let attributes: [String: String]
39+
public let messageAttributes: [String: Attribute]
40+
public let eventSourceArn: String
41+
public let eventSource: String
42+
public let awsRegion: AWSRegion
43+
}
44+
}
45+
46+
extension SQS.Message: Decodable {
47+
enum CodingKeys: String, CodingKey {
48+
case messageId
49+
case receiptHandle
50+
case body
51+
case md5OfBody
52+
case md5OfMessageAttributes
53+
case attributes
54+
case messageAttributes
55+
case eventSourceArn = "eventSourceARN"
56+
case eventSource
57+
case awsRegion
58+
}
59+
}
60+
61+
extension SQS.Message.Attribute: Equatable {}
62+
63+
extension SQS.Message.Attribute: Decodable {
64+
enum CodingKeys: String, CodingKey {
65+
case dataType
66+
case stringValue
67+
case binaryValue
68+
69+
// BinaryListValue and StringListValue are unimplemented since
70+
// they are not implemented as discussed here:
71+
// https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_MessageAttributeValue.html
72+
}
73+
74+
public init(from decoder: Decoder) throws {
75+
let container = try decoder.container(keyedBy: CodingKeys.self)
76+
77+
let dataType = try container.decode(String.self, forKey: .dataType)
78+
switch dataType {
79+
case "String":
80+
let value = try container.decode(String.self, forKey: .stringValue)
81+
self = .string(value)
82+
case "Number":
83+
let value = try container.decode(String.self, forKey: .stringValue)
84+
self = .number(value)
85+
case "Binary":
86+
let base64encoded = try container.decode(String.self, forKey: .binaryValue)
87+
let bytes = try base64encoded.base64decoded()
88+
self = .binary(bytes)
89+
default:
90+
throw DecodingError.dataCorruptedError(forKey: .dataType, in: container, debugDescription: """
91+
Unexpected value \"\(dataType)\" for key \(CodingKeys.dataType).
92+
Expected `String`, `Binary` or `Number`.
93+
""")
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)