Skip to content

Commit c8d2ebd

Browse files
committed
Create AWSRegion enum
1 parent 5b000b1 commit c8d2ebd

File tree

7 files changed

+139
-7
lines changed

7 files changed

+139
-7
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
// list all available regions using aws cli:
16+
// $ aws ssm get-parameters-by-path --path /aws/service/global-infrastructure/services/lambda/regions --output json
17+
18+
/// Enumeration of the AWS Regions.
19+
public enum AWSRegion: String, Codable {
20+
case ap_northeast_1 = "ap-northeast-1"
21+
case ap_northeast_2 = "ap-northeast-2"
22+
case ap_east_1 = "ap-east-1"
23+
case ap_southeast_1 = "ap-southeast-1"
24+
case ap_southeast_2 = "ap-southeast-2"
25+
case ap_south_1 = "ap-south-1"
26+
27+
case cn_north_1 = "cn-north-1"
28+
case cn_northwest_1 = "cn-northwest-1"
29+
30+
case eu_north_1 = "eu-north-1"
31+
case eu_west_1 = "eu-west-1"
32+
case eu_west_2 = "eu-west-2"
33+
case eu_west_3 = "eu-west-3"
34+
case eu_central_1 = "eu-central-1"
35+
36+
case us_east_1 = "us-east-1"
37+
case us_east_2 = "us-east-2"
38+
case us_west_1 = "us-west-1"
39+
case us_west_2 = "us-west-2"
40+
case us_gov_east_1 = "us-gov-east-1"
41+
case us_gov_west_1 = "us-gov-west-1"
42+
43+
case ca_central_1 = "ca-central-1"
44+
case sa_east_1 = "sa-east-1"
45+
case me_south_1 = "me-south-1"
46+
}

Sources/AWSLambdaEvents/Cloudwatch.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,20 @@
1515
import struct Foundation.Date
1616

1717
public enum Cloudwatch {
18+
/// CloudWatch.Event is the outer structure of an event sent via CloudWatch Events.
19+
///
20+
/// **NOTE**: For examples of events that come via CloudWatch Events, see https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/EventTypes.html
1821
public struct Event<Detail: Decodable>: Decodable {
1922
public let id: String
20-
public let detailType: String
2123
public let source: String
2224
public let accountId: String
2325

2426
@ISO8601Coding
2527
public var time: Date
26-
public let region: String
28+
public let region: AWSRegion
2729
public let resources: [String]
30+
31+
public let detailType: String
2832
public let detail: Detail
2933

3034
enum CodingKeys: String, CodingKey {

Sources/AWSLambdaEvents/DateWrappers.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public struct ISO8601Coding: Decodable {
2828
let dateString = try container.decode(String.self)
2929
guard let date = Self.dateFormatter.date(from: dateString) else {
3030
throw DecodingError.dataCorruptedError(in: container, debugDescription:
31-
"Expected date to be in iso8601 date format, but `\(dateString) does not forfill format`")
31+
"Expected date to be in iso8601 date format, but `\(dateString)` does not forfill format")
3232
}
3333
self.wrappedValue = date
3434
}
@@ -49,7 +49,7 @@ public struct ISO8601WithFractionalSecondsCoding: Decodable {
4949
let dateString = try container.decode(String.self)
5050
guard let date = Self.dateFormatter.date(from: dateString) else {
5151
throw DecodingError.dataCorruptedError(in: container, debugDescription:
52-
"Expected date to be in iso8601 date format with fractional seconds, but `\(dateString) does not forfill format`")
52+
"Expected date to be in iso8601 date format with fractional seconds, but `\(dateString)` does not forfill format")
5353
}
5454
self.wrappedValue = date
5555
}

Sources/AWSLambdaEvents/S3.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public enum S3 {
2020
public struct Record: Decodable {
2121
public let eventVersion: String
2222
public let eventSource: String
23-
public let awsRegion: String
23+
public let awsRegion: AWSRegion
2424

2525
@ISO8601WithFractionalSecondsCoding
2626
public var eventTime: Date

Tests/AWSLambdaEventsTests/CloudwatchTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class CloudwatchTests: XCTestCase {
4646
XCTAssertEqual(event.source, "aws.events")
4747
XCTAssertEqual(event.accountId, "123456789012")
4848
XCTAssertEqual(event.time, Date(timeIntervalSince1970: 0))
49-
XCTAssertEqual(event.region, "us-east-1")
49+
XCTAssertEqual(event.region, .us_east_1)
5050
XCTAssertEqual(event.resources, ["arn:aws:events:us-east-1:123456789012:rule/ExampleRule"])
5151
}
5252
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
@testable import AWSLambdaEvents
16+
import XCTest
17+
18+
class DateWrapperTests: XCTestCase {
19+
func testISO8601CodingWrapperSuccess() {
20+
struct TestEvent: Decodable {
21+
@ISO8601Coding
22+
var date: Date
23+
}
24+
25+
let json = #"{"date":"2020-03-26T16:53:05Z"}"#
26+
var event: TestEvent?
27+
XCTAssertNoThrow(event = try JSONDecoder().decode(TestEvent.self, from: json.data(using: .utf8)!))
28+
29+
XCTAssertEqual(event?.date, Date(timeIntervalSince1970: 1_585_241_585))
30+
}
31+
32+
func testISO8601CodingWrapperFailure() {
33+
struct TestEvent: Decodable {
34+
@ISO8601Coding
35+
var date: Date
36+
}
37+
38+
let date = "2020-03-26T16:53:05" // missing Z at end
39+
let json = #"{"date":"\#(date)"}"#
40+
XCTAssertThrowsError(_ = try JSONDecoder().decode(TestEvent.self, from: json.data(using: .utf8)!)) { error in
41+
guard case DecodingError.dataCorrupted(let context) = error else {
42+
XCTFail("Unexpected error: \(error)"); return
43+
}
44+
45+
XCTAssertEqual(context.codingPath.compactMap { $0.stringValue }, ["date"])
46+
XCTAssertEqual(context.debugDescription, "Expected date to be in iso8601 date format, but `\(date)` does not forfill format")
47+
XCTAssertNil(context.underlyingError)
48+
}
49+
}
50+
51+
func testISO8601WithFractionalSecondsCodingWrapperSuccess() {
52+
struct TestEvent: Decodable {
53+
@ISO8601WithFractionalSecondsCoding
54+
var date: Date
55+
}
56+
57+
let json = #"{"date":"2020-03-26T16:53:05.123Z"}"#
58+
var event: TestEvent?
59+
XCTAssertNoThrow(event = try JSONDecoder().decode(TestEvent.self, from: json.data(using: .utf8)!))
60+
61+
XCTAssertEqual(event?.date, Date(timeIntervalSince1970: 1_585_241_585.123))
62+
}
63+
64+
func testISO8601WithFractionalSecondsCodingWrapperFailure() {
65+
struct TestEvent: Decodable {
66+
@ISO8601WithFractionalSecondsCoding
67+
var date: Date
68+
}
69+
70+
let date = "2020-03-26T16:53:05Z" // missing fractional seconds
71+
let json = #"{"date":"\#(date)"}"#
72+
XCTAssertThrowsError(_ = try JSONDecoder().decode(TestEvent.self, from: json.data(using: .utf8)!)) { error in
73+
guard case DecodingError.dataCorrupted(let context) = error else {
74+
XCTFail("Unexpected error: \(error)"); return
75+
}
76+
77+
XCTAssertEqual(context.codingPath.compactMap { $0.stringValue }, ["date"])
78+
XCTAssertEqual(context.debugDescription, "Expected date to be in iso8601 date format with fractional seconds, but `\(date)` does not forfill format")
79+
XCTAssertNil(context.underlyingError)
80+
}
81+
}
82+
}

Tests/AWSLambdaEventsTests/S3Tests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class S3Tests: XCTestCase {
6969

7070
XCTAssertEqual(record.eventVersion, "2.1")
7171
XCTAssertEqual(record.eventSource, "aws:s3")
72-
XCTAssertEqual(record.awsRegion, "eu-central-1")
72+
XCTAssertEqual(record.awsRegion, .eu_central_1)
7373
XCTAssertEqual(record.eventName, "ObjectCreated:Put")
7474
XCTAssertEqual(record.eventTime, Date(timeIntervalSince1970: 1_578_907_540.621))
7575
XCTAssertEqual(record.userIdentity, S3.UserIdentity(principalId: "AWS:AAAAAAAJ2MQ4YFQZ7AULJ"))

0 commit comments

Comments
 (0)