Skip to content

Commit 792189b

Browse files
committed
Added SNS & SQS Events
1 parent 87121ec commit 792189b

File tree

8 files changed

+791
-1
lines changed

8 files changed

+791
-1
lines changed

Package.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ let package = Package(
2424
.product(name: "NIOFoundationCompat", package: "swift-nio"),
2525
]),
2626
.testTarget(name: "AWSLambdaRuntimeTests", dependencies: ["AWSLambdaRuntime"]),
27-
.target(name: "AWSLambdaEvents", dependencies: []),
27+
.target(name: "AWSLambdaEvents", dependencies: [
28+
.product(name: "NIOHTTP1", package: "swift-nio"),
29+
.product(name: "NIOFoundationCompat", package: "swift-nio"),
30+
]),
2831
.testTarget(name: "AWSLambdaEventsTests", dependencies: ["AWSLambdaEvents"]),
2932
// samples
3033
.target(name: "StringSample", dependencies: ["AWSLambdaRuntime"]),
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
public struct AWSNumber: Codable, Equatable {
16+
public let stringValue: String
17+
18+
public var int: Int? {
19+
Int(self.stringValue)
20+
}
21+
22+
public var double: Double? {
23+
Double(self.stringValue)
24+
}
25+
26+
public init(int: Int) {
27+
self.stringValue = String(int)
28+
}
29+
30+
public init(double: Double) {
31+
self.stringValue = String(double)
32+
}
33+
34+
public init(from decoder: Decoder) throws {
35+
let container = try decoder.singleValueContainer()
36+
self.stringValue = try container.decode(String.self)
37+
}
38+
39+
public func encode(to encoder: Encoder) throws {
40+
var container = encoder.singleValueContainer()
41+
try container.encode(self.stringValue)
42+
}
43+
}

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: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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(AWSNumber)
31+
}
32+
33+
public let messageId: String
34+
public let receiptHandle: String
35+
public let 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: String
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+
public init(from decoder: Decoder) throws {
61+
let container = try decoder.container(keyedBy: CodingKeys.self)
62+
self.messageId = try container.decode(String.self, forKey: .messageId)
63+
self.receiptHandle = try container.decode(String.self, forKey: .receiptHandle)
64+
self.md5OfBody = try container.decode(String.self, forKey: .md5OfBody)
65+
self.md5OfMessageAttributes = try container.decodeIfPresent(String.self, forKey: .md5OfMessageAttributes)
66+
self.attributes = try container.decode([String: String].self, forKey: .attributes)
67+
self.messageAttributes = try container.decode([String: Attribute].self, forKey: .messageAttributes)
68+
self.eventSourceArn = try container.decode(String.self, forKey: .eventSourceArn)
69+
self.eventSource = try container.decode(String.self, forKey: .eventSource)
70+
self.awsRegion = try container.decode(String.self, forKey: .awsRegion)
71+
72+
let body = try container.decode(String?.self, forKey: .body)
73+
self.body = body != "" ? body : nil
74+
}
75+
}
76+
77+
extension SQS.Message.Attribute: Equatable {}
78+
79+
extension SQS.Message.Attribute: Decodable {
80+
enum CodingKeys: String, CodingKey {
81+
case dataType
82+
case stringValue
83+
case binaryValue
84+
85+
// BinaryListValue and StringListValue are unimplemented since
86+
// they are not implemented as discussed here:
87+
// https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_MessageAttributeValue.html
88+
}
89+
90+
public init(from decoder: Decoder) throws {
91+
let container = try decoder.container(keyedBy: CodingKeys.self)
92+
93+
let dataType = try container.decode(String.self, forKey: .dataType)
94+
switch dataType {
95+
case "String":
96+
let value = try container.decode(String.self, forKey: .stringValue)
97+
self = .string(value)
98+
case "Number":
99+
let value = try container.decode(AWSNumber.self, forKey: .stringValue)
100+
self = .number(value)
101+
case "Binary":
102+
let base64encoded = try container.decode(String.self, forKey: .binaryValue)
103+
let bytes = try base64encoded.base64decoded()
104+
self = .binary(bytes)
105+
default:
106+
throw DecodingError.dataCorruptedError(forKey: .dataType, in: container, debugDescription: """
107+
Unexpected value \"\(dataType)\" for key \(CodingKeys.dataType).
108+
Expected `String`, `Binary` or `Number`.
109+
""")
110+
}
111+
}
112+
}

0 commit comments

Comments
 (0)