|
| 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