Skip to content

Commit b86dd0c

Browse files
committed
Implement SCFTimer Event
1 parent 20630fc commit b86dd0c

File tree

4 files changed

+129
-10
lines changed

4 files changed

+129
-10
lines changed

README.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -335,14 +335,10 @@ By default, the library also registers a Signal handler that traps `INT` and `TE
335335

336336
Tencent SCFs can be invoked directly from the Tencent SCF console, Tencent SCF API, TCCLI and Tencent Cloud toolkit. More commonly, they are invoked as a reaction to an events coming from the Tencent Cloud platform. To make it easier to integrate with Tencent Cloud platform events, the library includes an `TencentSCFEvents` target which provides abstractions for many commonly used events. Additional events can be easily modeled when needed following the same patterns set by `TencentSCFEvents`. Integration points with the Tencent Cloud Platform include:
337337

338-
* [APIGateway Proxy](https://docs.aws.amazon.com/lambda/latest/dg/services-apigateway.html)
339-
* [COS](https://cloud.tencent.com/document/product/583/9707)
340-
* [SES Events](https://docs.aws.amazon.com/lambda/latest/dg/services-ses.html)
341-
* [SNS Events](https://docs.aws.amazon.com/lambda/latest/dg/with-sns.html)
342-
* [SQS Events](https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html)
343-
* [CloudWatch Events](https://docs.aws.amazon.com/lambda/latest/dg/services-cloudwatchevents.html)
344-
345-
**Note**: Each one of the integration points mentioned above includes a set of `Codable` structs that mirror AWS' data model for these APIs.
338+
* [COS Events](https://cloud.tencent.com/document/product/583/9707)
339+
* [Timer Events](https://cloud.tencent.com/document/product/583/9708)
340+
341+
**Note**: Each one of the integration points mentioned above includes a set of `Codable` structs that transform Tencent Cloud's data model for these APIs.
346342

347343
## Performance
348344

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//===------------------------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftTencentSCFRuntime open source project
4+
//
5+
// Copyright (c) 2020 stevapple and the SwiftTencentSCFRuntime 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 SwiftTencentSCFRuntime project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===------------------------------------------------------------------------------------===//
14+
15+
import struct Foundation.Date
16+
17+
// https://cloud.tencent.com/document/product/583/9708
18+
19+
public enum SCFTimer {
20+
public struct Event: Codable, Equatable {
21+
public let trigger: String
22+
public var time: Date
23+
public let message: String
24+
25+
enum CodingKeys: String, CodingKey {
26+
case type = "Type"
27+
case trigger = "TriggerName"
28+
case time = "Time"
29+
case message = "Message"
30+
}
31+
32+
public func encode(to encoder: Encoder) throws {
33+
var container = encoder.container(keyedBy: CodingKeys.self)
34+
try container.encode("Timer", forKey: .type)
35+
try container.encode(trigger, forKey: .trigger)
36+
try container.encode(time, forKey: .time, using: DateCoding.ISO8601.self)
37+
try container.encode(message, forKey: .message)
38+
}
39+
40+
public init(from decoder: Decoder) throws {
41+
let container = try decoder.container(keyedBy: CodingKeys.self)
42+
let type = try container.decode(String.self, forKey: .type)
43+
guard type == "Timer" else {
44+
throw DecodingError.dataCorruptedError(forKey: CodingKeys.type, in: container, debugDescription: #"Expected type to be "Timer", but `\#(type)` does not match"#)
45+
}
46+
trigger = try container.decode(String.self, forKey: .trigger)
47+
time = try container.decode(Date.self, forKey: .time, using: DateCoding.ISO8601.self)
48+
message = try container.decode(String.self, forKey: .message)
49+
}
50+
}
51+
}

Tests/TencentSCFEventsTests/COSTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class COSTests: XCTestCase {
7373
"""
7474

7575
func testSimpleEventFromJSON() {
76-
let data = COSTests.eventBody.data(using: .utf8)!
76+
let data = Self.eventBody.data(using: .utf8)!
7777
var event: COS.Event?
7878
XCTAssertNoThrow(event = try JSONDecoder().decode(COS.Event.self, from: data))
7979

@@ -108,7 +108,7 @@ class COSTests: XCTestCase {
108108
}
109109

110110
func testEventDecodeAndEncode() {
111-
let data = COSTests.eventBody.data(using: .utf8)!
111+
let data = Self.eventBody.data(using: .utf8)!
112112
let decoder = JSONDecoder()
113113
var event: COS.Event?
114114
XCTAssertNoThrow(event = try decoder.decode(COS.Event.self, from: data))
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//===------------------------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftTencentSCFRuntime open source project
4+
//
5+
// Copyright (c) 2020 stevapple and the SwiftTencentSCFRuntime 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 SwiftTencentSCFRuntime project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===------------------------------------------------------------------------------------===//
14+
15+
@testable import TencentSCFEvents
16+
import XCTest
17+
18+
class SCFTimerTests: XCTestCase {
19+
static let eventBody = """
20+
{
21+
"Type":"Timer",
22+
"TriggerName":"EveryDay",
23+
"Time":"2019-02-21T11:49:00Z",
24+
"Message":"user define msg body"
25+
}
26+
"""
27+
28+
func testSimpleEventFromJSON() {
29+
let data = Self.eventBody.data(using: .utf8)!
30+
var event: SCFTimer.Event?
31+
XCTAssertNoThrow(event = try JSONDecoder().decode(SCFTimer.Event.self, from: data))
32+
33+
XCTAssertEqual(event?.message, "user define msg body")
34+
XCTAssertEqual(event?.trigger, "EveryDay")
35+
XCTAssertEqual(event?.time.description, "2019-02-21 11:49:00 +0000")
36+
}
37+
38+
func testEventTypeNotMatch() {
39+
let type = "time"
40+
let wrongJson = """
41+
{
42+
"Type":"\(type)",
43+
"TriggerName":"EveryDay",
44+
"Time":"2019-02-21T11:49:00Z",
45+
"Message":"user define msg body"
46+
}
47+
"""
48+
let data = wrongJson.data(using: .utf8)!
49+
50+
XCTAssertThrowsError(_ = try JSONDecoder().decode(SCFTimer.Event.self, from: data)) { error in
51+
guard case DecodingError.dataCorrupted(let context) = error else {
52+
XCTFail("Unexpected error: \(error)"); return
53+
}
54+
55+
XCTAssertEqual(context.codingPath.compactMap { $0.stringValue }, ["Type"])
56+
XCTAssertEqual(context.debugDescription, #"Expected type to be "Timer", but `\#(type)` does not match"#)
57+
XCTAssertNil(context.underlyingError)
58+
}
59+
}
60+
61+
func testEventDecodeAndEncode() {
62+
let data = Self.eventBody.data(using: .utf8)!
63+
let decoder = JSONDecoder()
64+
var event: SCFTimer.Event?
65+
XCTAssertNoThrow(event = try decoder.decode(SCFTimer.Event.self, from: data))
66+
67+
var newEvent: SCFTimer.Event?
68+
XCTAssertNoThrow(newEvent = try decoder.decode(SCFTimer.Event.self, from: try JSONEncoder().encode(event)))
69+
70+
XCTAssertEqual(event, newEvent)
71+
}
72+
}

0 commit comments

Comments
 (0)