Skip to content

Commit 8610b59

Browse files
committed
Implement CKafka Event
1 parent b86dd0c commit 8610b59

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ Tencent SCFs can be invoked directly from the Tencent SCF console, Tencent SCF A
337337

338338
* [COS Events](https://cloud.tencent.com/document/product/583/9707)
339339
* [Timer Events](https://cloud.tencent.com/document/product/583/9708)
340+
* [CKafka Messages](https://cloud.tencent.com/document/product/583/17530)
340341

341342
**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.
342343

Sources/TencentSCFEvents/CKafka.swift

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
// https://cloud.tencent.com/document/product/583/17530
16+
17+
public enum CKafka {
18+
public struct Event: Codable, Equatable {
19+
public typealias Record = Message
20+
public let records: [Record]
21+
22+
public enum CodingKeys: String, CodingKey {
23+
case records = "Records"
24+
}
25+
}
26+
27+
public struct Message: Codable, Equatable {
28+
public let topic: String
29+
public let partition: UInt64
30+
public let offset: UInt64
31+
public let key: String
32+
public let body: String
33+
34+
enum WrappingCodingKeys: String, CodingKey {
35+
case ckafka = "Ckafka"
36+
}
37+
38+
enum CodingKeys: String, CodingKey {
39+
case topic
40+
case partition
41+
case offset
42+
case key = "msgKey"
43+
case body = "msgBody"
44+
}
45+
46+
public func encode(to encoder: Encoder) throws {
47+
var wrapperContainer = encoder.container(keyedBy: WrappingCodingKeys.self)
48+
var container = wrapperContainer.nestedContainer(keyedBy: CodingKeys.self, forKey: .ckafka)
49+
try container.encode(topic, forKey: .topic)
50+
try container.encode(partition, forKey: .partition)
51+
try container.encode(offset, forKey: .offset)
52+
try container.encode(key, forKey: .key)
53+
try container.encode(body, forKey: .body)
54+
}
55+
56+
public init(from decoder: Decoder) throws {
57+
let wrapperContainer = try decoder.container(keyedBy: WrappingCodingKeys.self)
58+
let container = try wrapperContainer.nestedContainer(keyedBy: CodingKeys.self, forKey: .ckafka)
59+
topic = try container.decode(String.self, forKey: .topic)
60+
partition = try container.decode(UInt64.self, forKey: .partition)
61+
offset = try container.decode(UInt64.self, forKey: .offset)
62+
key = try container.decode(String.self, forKey: .key)
63+
body = try container.decode(String.self, forKey: .body)
64+
}
65+
}
66+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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 CKafkaTests: XCTestCase {
19+
static let eventBody = """
20+
{
21+
"Records": [
22+
{
23+
"Ckafka": {
24+
"topic": "test-topic",
25+
"partition":1,
26+
"offset":36,
27+
"msgKey": "None",
28+
"msgBody": "Hello from Ckafka!"
29+
}
30+
},
31+
{
32+
"Ckafka": {
33+
"topic": "test-topic",
34+
"partition":1,
35+
"offset":37,
36+
"msgKey": "None",
37+
"msgBody": "Hello from Ckafka again!"
38+
}
39+
}
40+
]
41+
}
42+
"""
43+
44+
func testSimpleEventFromJSON() {
45+
let data = Self.eventBody.data(using: .utf8)!
46+
var event: CKafka.Event?
47+
XCTAssertNoThrow(event = try JSONDecoder().decode(CKafka.Event.self, from: data))
48+
49+
guard event?.records.count == 2 else {
50+
XCTFail("Expected to have two records")
51+
return
52+
}
53+
54+
if let record = event?.records.first {
55+
XCTAssertEqual(record.topic, "test-topic")
56+
XCTAssertEqual(record.partition, 1)
57+
XCTAssertEqual(record.offset, 36)
58+
XCTAssertEqual(record.key, "None")
59+
XCTAssertEqual(record.body, "Hello from Ckafka!")
60+
} else {
61+
XCTFail("Unexpected error")
62+
return
63+
}
64+
65+
if let record = event?.records.last {
66+
XCTAssertEqual(record.topic, "test-topic")
67+
XCTAssertEqual(record.partition, 1)
68+
XCTAssertEqual(record.offset, 37)
69+
XCTAssertEqual(record.key, "None")
70+
XCTAssertEqual(record.body, "Hello from Ckafka again!")
71+
} else {
72+
XCTFail("Unexpected error")
73+
return
74+
}
75+
}
76+
77+
func testEventDecodeAndEncode() {
78+
let data = Self.eventBody.data(using: .utf8)!
79+
let decoder = JSONDecoder()
80+
var event: CKafka.Event?
81+
XCTAssertNoThrow(event = try decoder.decode(CKafka.Event.self, from: data))
82+
83+
var newEvent: CKafka.Event?
84+
XCTAssertNoThrow(newEvent = try decoder.decode(CKafka.Event.self, from: try JSONEncoder().encode(event)))
85+
86+
XCTAssertEqual(event, newEvent)
87+
}
88+
}

0 commit comments

Comments
 (0)