Skip to content

Commit 4c6b4bc

Browse files
committed
Change useless Codables into Encodable and Decodable
1 parent 7a826d4 commit 4c6b4bc

File tree

17 files changed

+84
-201
lines changed

17 files changed

+84
-201
lines changed

Examples/CloudFunctions/Sources/ErrorHandling/main.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ SCF.run { (context: SCF.Context, request: Request, callback: (Result<Response, E
4949

5050
// MARK: - Request and Response
5151

52-
struct Request: Codable {
52+
struct Request: Decodable {
5353
let requestID: String
5454
let error: Error
5555

@@ -58,7 +58,7 @@ struct Request: Codable {
5858
self.error = error ?? .none
5959
}
6060

61-
public enum Error: Codable, RawRepresentable {
61+
public enum Error: Decodable, RawRepresentable {
6262
case none
6363
case managed
6464
case unmanaged(String)
@@ -92,7 +92,7 @@ struct Request: Codable {
9292
}
9393
}
9494

95-
struct Response: Codable {
95+
struct Response: Encodable {
9696
let scfRequestID: String
9797
let requestID: String
9898
let status: Status
@@ -103,7 +103,7 @@ struct Response: Codable {
103103
self.status = status
104104
}
105105

106-
public enum Status: Int, Codable {
106+
public enum Status: Int, Encodable {
107107
case ok
108108
case error
109109
}

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@ More commonly, the event would be a JSON, which is modeled using `Codable`, for
7272
// Import the module.
7373
import TencentSCFRuntime
7474

75-
// Request, uses Codable for transparent JSON encoding.
76-
private struct Request: Codable {
75+
// Request, uses Decodable for transparent JSON decoding.
76+
private struct Request: Decodable {
7777
let name: String
7878
}
7979

80-
// Response, uses Codable for transparent JSON encoding.
81-
private struct Response: Codable {
80+
// Response, uses Encodable for transparent JSON encoding.
81+
private struct Response: Encodable {
8282
let message: String
8383
}
8484

Sources/CodableSample/main.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828
import NIO
2929
import TencentSCFRuntime
3030

31-
struct Request: Codable {
31+
struct Request: Decodable {
3232
let body: String
3333
}
3434

35-
struct Response: Codable {
35+
struct Response: Encodable {
3636
let body: String
3737
}
3838

Sources/TencentSCFEvents/APIGateway/APIGateway.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import class Foundation.JSONEncoder
2020
public enum APIGateway {
2121
/// `APIGateway.Request` contains data coming from the API Gateway.
2222
public struct Request<T: Decodable> {
23-
public struct Context: Codable {
23+
public struct Context: Decodable {
2424
public let identity: [String: String]
2525
public let serviceId: String
2626
public let path: String
@@ -42,15 +42,15 @@ public enum APIGateway {
4242
public let body: T?
4343
}
4444

45-
public enum Stage: String, Codable {
45+
public enum Stage: String, Decodable {
4646
case test
4747
case debug
4848
case prepub
4949
case release
5050
}
5151

5252
/// `APIGateway.Response` stores response ready for sending to the API Gateway.
53-
public struct Response: Codable {
53+
public struct Response: Encodable {
5454
public let statusCode: HTTPResponseStatus
5555
public let headers: HTTPHeaders
5656
public let body: String

Sources/TencentSCFEvents/CKafka.swift

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// https://cloud.tencent.com/document/product/583/17530
1616

1717
public enum CKafka {
18-
public struct Event: Codable, Equatable {
18+
public struct Event: Decodable, Equatable {
1919
public typealias Record = Message
2020
public let records: [Record]
2121

@@ -24,7 +24,7 @@ public enum CKafka {
2424
}
2525
}
2626

27-
public struct Message: Codable, Equatable {
27+
public struct Message: Decodable, Equatable {
2828
public let topic: String
2929
public let partition: UInt64
3030
public let offset: UInt64
@@ -52,15 +52,5 @@ public enum CKafka {
5252
self.key = try container.decode(String.self, forKey: .key)
5353
self.body = try container.decode(String.self, forKey: .body)
5454
}
55-
56-
public func encode(to encoder: Encoder) throws {
57-
var wrapperContainer = encoder.container(keyedBy: WrappingCodingKeys.self)
58-
var container = wrapperContainer.nestedContainer(keyedBy: CodingKeys.self, forKey: .ckafka)
59-
try container.encode(self.topic, forKey: .topic)
60-
try container.encode(self.partition, forKey: .partition)
61-
try container.encode(self.offset, forKey: .offset)
62-
try container.encode(self.key, forKey: .key)
63-
try container.encode(self.body, forKey: .body)
64-
}
6555
}
6656
}

Sources/TencentSCFEvents/CMQTopic.swift

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ import struct Foundation.Date
1818

1919
public enum CMQ {
2020
public enum Topic {
21-
public struct Event: Codable, Equatable {
21+
public struct Event: Decodable, Equatable {
2222
public let records: [Record]
2323

2424
public enum CodingKeys: String, CodingKey {
2525
case records = "Records"
2626
}
2727
}
2828

29-
public struct Record: Codable, Equatable {
29+
public struct Record: Decodable, Equatable {
3030
internal static let type: String = "topic"
3131

3232
public let topicOwner: UInt64
@@ -58,19 +58,9 @@ public enum CMQ {
5858
topicName = try container.decode(String.self, forKey: .topicName)
5959
subscriptionName = try container.decode(String.self, forKey: .subscriptionName)
6060
}
61-
62-
public func encode(to encoder: Encoder) throws {
63-
var wrapperContainer = encoder.container(keyedBy: WrappingCodingKeys.self)
64-
try wrapperContainer.encode(message, forKey: .cmq)
65-
var container = wrapperContainer.nestedContainer(keyedBy: CodingKeys.self, forKey: .cmq)
66-
try container.encode(Self.type, forKey: .type)
67-
try container.encode(topicOwner, forKey: .topicOwner)
68-
try container.encode(topicName, forKey: .topicName)
69-
try container.encode(subscriptionName, forKey: .subscriptionName)
70-
}
7161
}
7262

73-
public struct Message: Codable, Equatable {
63+
public struct Message: Decodable, Equatable {
7464
internal static let separator: Character = ","
7565

7666
public let id: String
@@ -100,15 +90,6 @@ public enum CMQ {
10090
requestId = try container.decode(String.self, forKey: .requestId)
10191
publishTime = try container.decode(Date.self, forKey: .publishTime, using: DateCoding.ISO8601WithFractionalSeconds.self)
10292
}
103-
104-
public func encode(to encoder: Encoder) throws {
105-
var container = encoder.container(keyedBy: CodingKeys.self)
106-
try container.encode(id, forKey: .messageId)
107-
try container.encode(body, forKey: .messageBody)
108-
try container.encode(tags.joined(separator: .init(Self.separator)), forKey: .messageTags)
109-
try container.encode(publishTime, forKey: .publishTime, using: DateCoding.ISO8601WithFractionalSeconds.self)
110-
try container.encode(requestId, forKey: .requestId)
111-
}
11293
}
11394
}
11495
}

Sources/TencentSCFEvents/COS/COS.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import struct Foundation.URL
1818
// https://cloud.tencent.com/document/product/583/9707
1919

2020
public enum COS {
21-
public struct Event: Codable, Equatable {
21+
public struct Event: Decodable, Equatable {
2222
public struct Record: Equatable {
2323
public let cos: Entity
2424

@@ -39,7 +39,7 @@ public enum COS {
3939
}
4040
}
4141

42-
public struct RequestParameters: Codable, Equatable {
42+
public struct RequestParameters: Decodable, Equatable {
4343
public let sourceIP: String
4444
public let headers: [String: String]
4545

@@ -49,7 +49,7 @@ public enum COS {
4949
}
5050
}
5151

52-
public struct Entity: Codable, Equatable {
52+
public struct Entity: Decodable, Equatable {
5353
public let notificationId: String
5454
public let schemaVersion: String
5555
public let bucket: Bucket
@@ -63,7 +63,7 @@ public enum COS {
6363
}
6464
}
6565

66-
public struct Bucket: Codable, Equatable {
66+
public struct Bucket: Decodable, Equatable {
6767
public let region: String
6868
public let name: String
6969
public let appid: String

Sources/TencentSCFEvents/COS/EventRecord+Codable.swift renamed to Sources/TencentSCFEvents/COS/EventRecord+Decodable.swift

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import struct Foundation.Date
1616

17-
extension COS.Event.Record: Codable {
17+
extension COS.Event.Record: Decodable {
1818
enum CodingKeys: String, CodingKey {
1919
case event
2020
case cos
@@ -45,19 +45,4 @@ extension COS.Event.Record: Codable {
4545
requestParameters = try eventContainer.decode(COS.RequestParameters.self, forKey: .requestParameters)
4646
eventTime = try eventContainer.decode(Date.self, forKey: .eventTime, using: DateCoding.UnixTimestamp.self)
4747
}
48-
49-
public func encode(to encoder: Encoder) throws {
50-
var container = encoder.container(keyedBy: CodingKeys.self)
51-
try container.encode(cos, forKey: .cos)
52-
53-
var eventContainer = container.nestedContainer(keyedBy: EventCodingKeys.self, forKey: .event)
54-
try eventContainer.encode(eventName, forKey: .eventName)
55-
try eventContainer.encode(eventVersion, forKey: .eventVersion)
56-
try eventContainer.encode(eventSource, forKey: .eventSource)
57-
try eventContainer.encode(eventQueue, forKey: .eventQueue)
58-
try eventContainer.encode(eventTime, forKey: .eventTime, using: DateCoding.UnixTimestamp.self)
59-
try eventContainer.encode(requestParameters, forKey: .requestParameters)
60-
try eventContainer.encode(requestId, forKey: .requestId)
61-
try eventContainer.encode(reservedInfo, forKey: .reservedInfo)
62-
}
6348
}

Sources/TencentSCFEvents/COS/Object+Codable.swift renamed to Sources/TencentSCFEvents/COS/Object+Decodable.swift

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import struct Foundation.URL
1717

1818
// https://cloud.tencent.com/document/product/583/9707
1919

20-
extension COS.Object: Codable {
20+
extension COS.Object: Decodable {
2121
enum CodingKeys: String, CodingKey {
2222
case url
2323
case key
@@ -53,21 +53,4 @@ extension COS.Object: Codable {
5353
customMeta = try container.decode([String: String].self, forKey: .meta)
5454
.compactMapKeys { $0.starts(with: "x-cos-meta-") ? String($0.dropFirst(11)) : nil }
5555
}
56-
57-
public func encode(to encoder: Encoder) throws {
58-
var container = encoder.container(keyedBy: CodingKeys.self)
59-
try container.encode(url, forKey: .url)
60-
try container.encode(fullKey, forKey: .key)
61-
try container.encode(vid, forKey: .vid)
62-
try container.encode(size, forKey: .size)
63-
try container.encode(customMeta.mapKeys { "x-cos-meta-" + $0 }, forKey: .meta)
64-
65-
var metaContainer = container.nestedContainer(keyedBy: MetaCodingKeys.self, forKey: .meta)
66-
try metaContainer.encodeIfPresent(contentType, forKey: .contentType)
67-
try metaContainer.encodeIfPresent(cacheControl, forKey: .cacheControl)
68-
try metaContainer.encodeIfPresent(contentDisposition, forKey: .contentDisposition)
69-
try metaContainer.encodeIfPresent(contentEncoding, forKey: .contentEncoding)
70-
try metaContainer.encodeIfPresent(requestId, forKey: .requestId)
71-
try metaContainer.encodeIfPresent(expireTime, forKey: .expireTime, using: DateCoding.RFC5322.self)
72-
}
7356
}

Sources/TencentSCFEvents/CTimer.swift

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ import struct Foundation.Date
1717
// https://cloud.tencent.com/document/product/583/9708
1818

1919
public enum CTimer {
20-
public struct Event: Codable, Equatable {
21-
internal static let type: String = "Timer"
22-
20+
public struct Event: Decodable, Equatable {
21+
private static let type: String = "Timer"
2322
public let trigger: String
2423
public var time: Date
2524
public let message: String
@@ -41,13 +40,5 @@ public enum CTimer {
4140
time = try container.decode(Date.self, forKey: .time, using: DateCoding.ISO8601.self)
4241
message = try container.decode(String.self, forKey: .message)
4342
}
44-
45-
public func encode(to encoder: Encoder) throws {
46-
var container = encoder.container(keyedBy: CodingKeys.self)
47-
try container.encode(Self.type, forKey: .type)
48-
try container.encode(trigger, forKey: .trigger)
49-
try container.encode(time, forKey: .time, using: DateCoding.ISO8601.self)
50-
try container.encode(message, forKey: .message)
51-
}
5243
}
5344
}

0 commit comments

Comments
 (0)