Skip to content

Commit d11afb1

Browse files
committed
Added Cloudwatch Event
1 parent 643dd91 commit d11afb1

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed
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+
import struct Foundation.Date
16+
17+
public enum Cloudwatch {
18+
public struct Event<Detail: Decodable>: Decodable {
19+
public let id: String
20+
public let detailType: String
21+
public let source: String
22+
public let accountId: String
23+
24+
@ISO8601Coding
25+
public var time: Date
26+
public let region: String
27+
public let resources: [String]
28+
public let detail: Detail
29+
30+
enum CodingKeys: String, CodingKey {
31+
case id
32+
case detailType = "detail-type"
33+
case source
34+
case accountId = "account"
35+
case time
36+
case region
37+
case resources
38+
case detail
39+
}
40+
}
41+
42+
public struct ScheduledEvent: Codable {}
43+
}

Sources/AWSLambdaEvents/DateWrappers.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,27 @@
1515
import struct Foundation.Date
1616
import class Foundation.ISO8601DateFormatter
1717

18+
@propertyWrapper
19+
public struct ISO8601Coding: Decodable {
20+
public let wrappedValue: Date
21+
22+
public init(wrappedValue: Date) {
23+
self.wrappedValue = wrappedValue
24+
}
25+
26+
public init(from decoder: Decoder) throws {
27+
let container = try decoder.singleValueContainer()
28+
let dateString = try container.decode(String.self)
29+
guard let date = Self.dateFormatter.date(from: dateString) else {
30+
throw DecodingError.dataCorruptedError(in: container, debugDescription:
31+
"Expected date to be in iso8601 date format, but `\(dateString) does not forfill format`")
32+
}
33+
self.wrappedValue = date
34+
}
35+
36+
private static let dateFormatter = ISO8601DateFormatter()
37+
}
38+
1839
@propertyWrapper
1940
public struct ISO8601WithFractionalSecondsCoding: Decodable {
2041
public let wrappedValue: Date
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 CloudwatchTests: XCTestCase {
19+
static let scheduledEventPayload = """
20+
{
21+
"id": "cdc73f9d-aea9-11e3-9d5a-835b769c0d9c",
22+
"detail-type": "Scheduled Event",
23+
"source": "aws.events",
24+
"account": "123456789012",
25+
"time": "1970-01-01T00:00:00Z",
26+
"region": "us-east-1",
27+
"resources": [
28+
"arn:aws:events:us-east-1:123456789012:rule/ExampleRule"
29+
],
30+
"detail": {}
31+
}
32+
"""
33+
34+
func testScheduledEventFromJSON() {
35+
let data = CloudwatchTests.scheduledEventPayload.data(using: .utf8)!
36+
var maybeEvent: Cloudwatch.Event<Cloudwatch.ScheduledEvent>?
37+
XCTAssertNoThrow(maybeEvent = try JSONDecoder().decode(Cloudwatch.Event<Cloudwatch.ScheduledEvent>.self, from: data))
38+
39+
guard let event = maybeEvent else {
40+
XCTFail("Expected to have an event")
41+
return
42+
}
43+
44+
XCTAssertEqual(event.id, "cdc73f9d-aea9-11e3-9d5a-835b769c0d9c")
45+
XCTAssertEqual(event.detailType, "Scheduled Event")
46+
XCTAssertEqual(event.source, "aws.events")
47+
XCTAssertEqual(event.accountId, "123456789012")
48+
XCTAssertEqual(event.time, Date(timeIntervalSince1970: 0))
49+
XCTAssertEqual(event.region, "us-east-1")
50+
XCTAssertEqual(event.resources, ["arn:aws:events:us-east-1:123456789012:rule/ExampleRule"])
51+
}
52+
}

0 commit comments

Comments
 (0)