Skip to content

Commit b20d95c

Browse files
committed
Update swiftformat
1 parent 2004900 commit b20d95c

File tree

22 files changed

+87
-92
lines changed

22 files changed

+87
-92
lines changed

.swiftformat

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,11 @@
99
--patternlet inline
1010
--stripunusedargs unnamed-only
1111
--ifdef no-indent
12+
--extensionacl on-declarations
13+
--disable typeSugar
14+
--disable andOperator
15+
--disable wrapMultilineStatementBraces
16+
--disable enumNamespaces
17+
--disable redundantExtensionACL
1218

1319
# rules

Examples/CloudFunctions/Sources/CurrencyExchange/main.swift

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,18 +161,16 @@ struct ExchangeRatesCalculator {
161161
dateFormatter.dateFormat = "dd/MMM/yy"
162162
let interval: DateInterval?
163163
if let period = try document.nodes(forXPath: "/exchangeRateMonthList/@Period").first?.stringValue,
164-
period.count == 26
165-
{
164+
period.count == 26 {
166165
// "01/Sep/2018 to 30/Sep/2018"
167166
let startString = period[period.startIndex ..< period.index(period.startIndex, offsetBy: 11)]
168167
let to = period[startString.endIndex ..< period.index(startString.endIndex, offsetBy: 4)]
169168
let endString = period[to.endIndex ..< period.index(to.endIndex, offsetBy: 11)]
170169
if let startDate = dateFormatter.date(from: String(startString)),
171-
let startDay = calendar.dateInterval(of: .day, for: startDate),
172-
to == " to ",
173-
let endDate = dateFormatter.date(from: String(endString)),
174-
let endDay = calendar.dateInterval(of: .day, for: endDate)
175-
{
170+
let startDay = calendar.dateInterval(of: .day, for: startDate),
171+
to == " to ",
172+
let endDate = dateFormatter.date(from: String(endString)),
173+
let endDay = calendar.dateInterval(of: .day, for: endDate) {
176174
interval = DateInterval(start: startDay.start, end: endDay.end)
177175
} else {
178176
interval = nil
@@ -184,9 +182,8 @@ struct ExchangeRatesCalculator {
184182
let ratesByCurrencyCode: [String: Decimal?] = Dictionary(uniqueKeysWithValues: try currencyCodes.map {
185183
let xpathCurrency = $0.replacingOccurrences(of: "'", with: "&apos;")
186184
if let rateString = try document.nodes(forXPath: "/exchangeRateMonthList/exchangeRate/currencyCode[text()='\(xpathCurrency)']/../rateNew/text()").first?.stringValue,
187-
// We must parse the decimal data using the UK locale, not the system one.
188-
let rate = Decimal(string: rateString, locale: self.locale)
189-
{
185+
// We must parse the decimal data using the UK locale, not the system one.
186+
let rate = Decimal(string: rateString, locale: self.locale) {
190187
return ($0, rate)
191188
} else {
192189
return ($0, nil)

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ let package = Package(
2222
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
2323
.package(url: "https://github.com/swift-server/swift-backtrace.git", from: "1.1.0"),
2424
.package(url: "https://github.com/stevapple/tencent-cloud-core.git", .upToNextMinor(from: "0.2.0")),
25-
.package(name: "Gzip", url: "https://github.com/1024jp/GzipSwift.git", from: "5.1.0")
25+
.package(name: "Gzip", url: "https://github.com/1024jp/GzipSwift.git", from: "5.1.0"),
2626
],
2727
targets: [
2828
.target(name: "TencentSCFRuntime", dependencies: [
@@ -46,7 +46,7 @@ let package = Package(
4646
.product(name: "NIOFoundationCompat", package: "swift-nio"),
4747
]),
4848
.target(name: "TencentSCFEvents", dependencies: [
49-
.byName(name: "Gzip")
49+
.byName(name: "Gzip"),
5050
]),
5151
.testTarget(name: "TencentSCFEventsTests", dependencies: ["TencentSCFEvents"]),
5252
// testing helper

Sources/TencentSCFEvents/APIGateway/APIGateway.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public enum APIGateway {
5353
public typealias Response = APIResponse
5454
}
5555

56-
internal extension APIGateway {
57-
static let defaultJSONDecoder = JSONDecoder()
58-
static let defaultJSONEncoder = JSONEncoder()
56+
extension APIGateway {
57+
internal static let defaultJSONDecoder = JSONDecoder()
58+
internal static let defaultJSONEncoder = JSONEncoder()
5959
}

Sources/TencentSCFEvents/APIResponse.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ extension APIResponse {
3333
do {
3434
rawBody = try body()
3535
} catch let err {
36-
self.headers = headers.merging(["Content-Type": "application/json"]) { (_, default) in `default` }
36+
self.headers = headers.merging(["Content-Type": "application/json"]) { _, `default` in `default` }
3737
self.statusCode = .internalServerError
3838
self.isBase64Encoded = false
3939
if let err = err as? EncodingError {
@@ -45,7 +45,7 @@ extension APIResponse {
4545
}
4646

4747
self.statusCode = statusCode
48-
self.headers = headers.merging(["Content-Type": rawBody.defaultMIME]) { (custom, _) in custom }
48+
self.headers = headers.merging(["Content-Type": rawBody.defaultMIME]) { custom, _ in custom }
4949

5050
switch rawBody {
5151
case .data(let dataBody):
@@ -67,7 +67,7 @@ extension APIResponse {
6767
case null
6868

6969
public static func codable<T: Encodable>(_ body: T) throws -> Self {
70-
return .json(String(data: try APIGateway.defaultJSONEncoder.encode(body), encoding: .utf8) ?? "")
70+
.json(String(data: try APIGateway.defaultJSONEncoder.encode(body), encoding: .utf8) ?? "")
7171
}
7272

7373
var defaultMIME: String {
@@ -80,3 +80,4 @@ extension APIResponse {
8080
}
8181
}
8282
}
83+

Sources/TencentSCFEvents/CLS/CLSLog.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
//
1313
//===------------------------------------------------------------------------------------===//
1414

15-
import Gzip
1615
import struct Foundation.Data
1716
import struct Foundation.Date
17+
import Gzip
1818

1919
// https://intl.cloud.tencent.com/document/product/583/38845
2020

@@ -29,7 +29,7 @@ public enum CLS {
2929
public init(from decoder: Decoder) throws {
3030
let rawContainer = try decoder.singleValueContainer()
3131
let data = try rawContainer.decode(CLS.Raw.self).data.gunzipped()
32-
32+
3333
let _self = try CLS.jsonDecoder.decode(CLS._Logs.self, from: data)
3434
self.topicId = _self.topicId
3535
self.topicName = _self.topicName
@@ -49,7 +49,7 @@ public enum CLS {
4949
case timestamp
5050
case content
5151
}
52-
52+
5353
public init(from decoder: Decoder) throws {
5454
let container = try decoder.container(keyedBy: CodingKeys.self)
5555
let timestring = try container.decode(String.self, forKey: .timestamp)

Sources/TencentSCFEvents/CLS/CLSRaw.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import struct Foundation.Data
1616

1717
// https://intl.cloud.tencent.com/document/product/583/38845
1818

19-
extension CLS{
19+
extension CLS {
2020
struct _Logs: Decodable {
2121
let topicId: String
2222
let topicName: String
@@ -35,6 +35,7 @@ extension CLS{
3535
enum WrappingCodingKeys: String, CodingKey {
3636
case logs = "clslogs"
3737
}
38+
3839
enum CodingKeys: String, CodingKey {
3940
case data
4041
}

Sources/TencentSCFEvents/Utils/Base64.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ extension Base64 {
4949

5050
@inlinable
5151
static func decode<Buffer: Collection>(encoded: Buffer, options: DecodingOptions = [])
52-
throws -> [UInt8] where Buffer.Element == UInt8
53-
{
52+
throws -> [UInt8] where Buffer.Element == UInt8 {
5453
let alphabet = options.contains(.base64UrlAlphabet)
5554
? Base64.decodeBase64Url
5655
: Base64.decodeBase64

Sources/TencentSCFEvents/Utils/HTTP.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ extension HTTPResponseStatus: Codable {
170170

171171
extension String {
172172
internal var isValidHTTPToken: Bool {
173-
self.utf8.allSatisfy { (char) -> Bool in
173+
self.utf8.allSatisfy { char -> Bool in
174174
switch char {
175175
case UInt8(ascii: "a") ... UInt8(ascii: "z"),
176176
UInt8(ascii: "A") ... UInt8(ascii: "Z"),

Sources/TencentSCFEvents/Utils/String.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
//
2626
//===------------------------------------------------------------------------------------===//
2727

28-
internal extension String {
29-
func jsonEncoded() -> String {
28+
extension String {
29+
internal func jsonEncoded() -> String {
3030
var bytes = [UInt8]()
3131
let stringBytes = self.utf8
3232
var startCopyIndex = stringBytes.startIndex

Sources/TencentSCFRuntime/SCF+Codable.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,31 +91,31 @@ internal struct CodableVoidClosureWrapper<In: Decodable>: SCFHandler {
9191
}
9292

9393
/// Implementation of a`ByteBuffer` to `In` decoding.
94-
public extension EventLoopSCFHandler where In: Decodable {
95-
func decode(buffer: ByteBuffer) throws -> In {
94+
extension EventLoopSCFHandler where In: Decodable {
95+
public func decode(buffer: ByteBuffer) throws -> In {
9696
try self.decoder.decode(In.self, from: buffer)
9797
}
9898
}
9999

100100
/// Implementation of `Out` to `ByteBuffer` encoding.
101-
public extension EventLoopSCFHandler where Out: Encodable {
102-
func encode(allocator: ByteBufferAllocator, value: Out) throws -> ByteBuffer? {
101+
extension EventLoopSCFHandler where Out: Encodable {
102+
public func encode(allocator: ByteBufferAllocator, value: Out) throws -> ByteBuffer? {
103103
try self.encoder.encode(value, using: allocator)
104104
}
105105
}
106106

107107
/// Default `ByteBuffer` to `In` decoder using Foundation's JSONDecoder.
108108
/// Advanced users that want to inject their own codec can do it by overriding these functions.
109-
public extension EventLoopSCFHandler where In: Decodable {
110-
var decoder: SCFCodableDecoder {
109+
extension EventLoopSCFHandler where In: Decodable {
110+
public var decoder: SCFCodableDecoder {
111111
SCF.defaultJSONDecoder
112112
}
113113
}
114114

115115
/// Default `Out` to `ByteBuffer` encoder using Foundation's JSONEncoder.
116116
/// Advanced users that want to inject their own codec can do it by overriding these functions.
117-
public extension EventLoopSCFHandler where Out: Encodable {
118-
var encoder: SCFCodableEncoder {
117+
extension EventLoopSCFHandler where Out: Encodable {
118+
public var encoder: SCFCodableEncoder {
119119
SCF.defaultJSONEncoder
120120
}
121121
}
@@ -128,9 +128,9 @@ public protocol SCFCodableEncoder {
128128
func encode<T: Encodable>(_ value: T, using allocator: ByteBufferAllocator) throws -> ByteBuffer
129129
}
130130

131-
private extension SCF {
132-
static let defaultJSONDecoder = JSONDecoder()
133-
static let defaultJSONEncoder = JSONEncoder()
131+
extension SCF {
132+
fileprivate static let defaultJSONDecoder = JSONDecoder()
133+
fileprivate static let defaultJSONEncoder = JSONEncoder()
134134
}
135135

136136
extension JSONDecoder: SCFCodableDecoder {}

Sources/TencentSCFRuntimeCore/SCF+String.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ internal struct StringVoidClosureWrapper: SCFHandler {
9898
}
9999
}
100100

101-
public extension EventLoopSCFHandler where In == String {
101+
extension EventLoopSCFHandler where In == String {
102102
/// Implementation of a `ByteBuffer` to `String` decoding.
103-
func decode(buffer: ByteBuffer) throws -> String {
103+
public func decode(buffer: ByteBuffer) throws -> String {
104104
var buffer = buffer
105105
guard let string = buffer.readString(length: buffer.readableBytes) else {
106106
fatalError("buffer.readString(length: buffer.readableBytes) failed")
@@ -109,9 +109,9 @@ public extension EventLoopSCFHandler where In == String {
109109
}
110110
}
111111

112-
public extension EventLoopSCFHandler where Out == String {
112+
extension EventLoopSCFHandler where Out == String {
113113
/// Implementation of `String` to `ByteBuffer` encoding.
114-
func encode(allocator: ByteBufferAllocator, value: String) throws -> ByteBuffer? {
114+
public func encode(allocator: ByteBufferAllocator, value: String) throws -> ByteBuffer? {
115115
// FIXME: reusable buffer
116116
var buffer = allocator.buffer(capacity: value.utf8.count)
117117
buffer.writeString(value)

Sources/TencentSCFRuntimeCore/SCFContext.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,7 @@ extension SCF {
109109
/// The role credential from SCF environment.
110110
public var credential: TencentCloud.Credential? {
111111
if let secretId = SCF.Env["TENCENTCLOUD_SECRETID"],
112-
let secretKey = SCF.Env["TENCENTCLOUD_SECRETKEY"]
113-
{
112+
let secretKey = SCF.Env["TENCENTCLOUD_SECRETKEY"] {
114113
return TencentCloud.Credential(secretId: secretId,
115114
secretKey: secretKey,
116115
sessionToken: SCF.Env["TENCENTCLOUD_SESSIONTOKEN"])

Sources/TencentSCFRuntimeCore/SCFHandler.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,20 @@ public protocol SCFHandler: EventLoopSCFHandler {
5151
func handle(context: SCF.Context, event: In, callback: @escaping (Result<Out, Error>) -> Void)
5252
}
5353

54-
internal extension SCF {
55-
static let defaultOffloadQueue = DispatchQueue(label: "SCFHandler.offload")
54+
extension SCF {
55+
internal static let defaultOffloadQueue = DispatchQueue(label: "SCFHandler.offload")
5656
}
5757

58-
public extension SCFHandler {
58+
extension SCFHandler {
5959
/// The queue on which `handle` is invoked on.
60-
var offloadQueue: DispatchQueue {
60+
public var offloadQueue: DispatchQueue {
6161
SCF.defaultOffloadQueue
6262
}
6363

6464
/// `SCFHandler` is offloading the processing to a `DispatchQueue`.
6565
/// This is slower but safer, in case the implementation blocks the `EventLoop`.
6666
/// Performance sensitive cloud functions should be based on `EventLoopSCFHandler` which does not offload.
67-
func handle(context: SCF.Context, event: In) -> EventLoopFuture<Out> {
67+
public func handle(context: SCF.Context, event: In) -> EventLoopFuture<Out> {
6868
let promise = context.eventLoop.makePromise(of: Out.self)
6969
// FIXME: reusable DispatchQueue
7070
self.offloadQueue.async {
@@ -74,8 +74,8 @@ public extension SCFHandler {
7474
}
7575
}
7676

77-
public extension SCFHandler {
78-
func shutdown(context: SCF.ShutdownContext) -> EventLoopFuture<Void> {
77+
extension SCFHandler {
78+
public func shutdown(context: SCF.ShutdownContext) -> EventLoopFuture<Void> {
7979
let promise = context.eventLoop.makePromise(of: Void.self)
8080
self.offloadQueue.async {
8181
do {
@@ -90,7 +90,7 @@ public extension SCFHandler {
9090

9191
/// Clean up the SCF resources synchronously.
9292
/// Concrete SCF handlers implement this method to shutdown resources like `HTTPClient`s and database connections.
93-
func syncShutdown(context: SCF.ShutdownContext) throws {
93+
public func syncShutdown(context: SCF.ShutdownContext) throws {
9494
// noop
9595
}
9696
}
@@ -138,9 +138,9 @@ public protocol EventLoopSCFHandler: ByteBufferSCFHandler {
138138
func decode(buffer: ByteBuffer) throws -> In
139139
}
140140

141-
public extension EventLoopSCFHandler {
141+
extension EventLoopSCFHandler {
142142
/// Driver for `ByteBuffer` -> `In` decoding and `Out` -> `ByteBuffer` encoding
143-
func handle(context: SCF.Context, event: ByteBuffer) -> EventLoopFuture<ByteBuffer?> {
143+
public func handle(context: SCF.Context, event: ByteBuffer) -> EventLoopFuture<ByteBuffer?> {
144144
switch self.decodeIn(buffer: event) {
145145
case .failure(let error):
146146
return context.eventLoop.makeFailedFuture(CodecError.requestDecoding(error))
@@ -174,8 +174,8 @@ public extension EventLoopSCFHandler {
174174
}
175175

176176
/// Implementation of `ByteBuffer` to `Void` decoding.
177-
public extension EventLoopSCFHandler where Out == Void {
178-
func encode(allocator: ByteBufferAllocator, value: Void) throws -> ByteBuffer? {
177+
extension EventLoopSCFHandler where Out == Void {
178+
public func encode(allocator: ByteBufferAllocator, value: Void) throws -> ByteBuffer? {
179179
nil
180180
}
181181
}
@@ -206,8 +206,8 @@ public protocol ByteBufferSCFHandler {
206206
func shutdown(context: SCF.ShutdownContext) -> EventLoopFuture<Void>
207207
}
208208

209-
public extension ByteBufferSCFHandler {
210-
func shutdown(context: SCF.ShutdownContext) -> EventLoopFuture<Void> {
209+
extension ByteBufferSCFHandler {
210+
public func shutdown(context: SCF.ShutdownContext) -> EventLoopFuture<Void> {
211211
context.eventLoop.makeSucceededFuture(())
212212
}
213213
}

Sources/TencentSCFRuntimeCore/SCFLifecycle.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ extension SCF {
102102
self.run(promise: finishedPromise)
103103
return finishedPromise.futureResult.mapResult { (handler, $0) }
104104
}
105-
.flatMap { (handler, runnerResult) -> EventLoopFuture<Int> in
105+
.flatMap { handler, runnerResult -> EventLoopFuture<Int> in
106106
// After the SCF finishPromise has succeeded or failed we need to shutdown
107107
// the handler.
108108
let shutdownContext = ShutdownContext(logger: logger, eventLoop: self.eventLoop)
@@ -111,7 +111,7 @@ extension SCF {
111111
// it with the runner result.
112112
logger.error("Error shutting down handler: \(error)")
113113
throw RuntimeError.shutdownError(shutdownError: error, runnerResult: runnerResult)
114-
}.flatMapResult { (_) -> Result<Int, Error> in
114+
}.flatMapResult { _ -> Result<Int, Error> in
115115
// We had no error shutting down the cloud function, so let's return the
116116
// runner's result.
117117
runnerResult

Sources/TencentSCFRuntimeCore/SCFRunner.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ extension SCF {
117117
}
118118
}
119119

120-
private extension SCF.Context {
121-
convenience init(logger: Logger, eventLoop: EventLoop, allocator: ByteBufferAllocator, invocation: SCF.Invocation) {
120+
extension SCF.Context {
121+
fileprivate convenience init(logger: Logger, eventLoop: EventLoop, allocator: ByteBufferAllocator, invocation: SCF.Invocation) {
122122
self.init(requestID: invocation.requestID,
123123
memoryLimit: invocation.memoryLimit,
124124
timeLimit: .milliseconds(Int(invocation.timeLimit)),
@@ -158,8 +158,8 @@ extension EventLoopFuture {
158158
}
159159
}
160160

161-
private extension Result {
162-
var successful: Bool {
161+
extension Result {
162+
private var successful: Bool {
163163
switch self {
164164
case .success:
165165
return true

0 commit comments

Comments
 (0)