|
| 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 | +} |
0 commit comments