Skip to content

Commit

Permalink
apply swiftformat (swift-server#342)
Browse files Browse the repository at this point in the history
* apply swiftformat

* update dep on Swift Docc to v1.3.0

* force usage of swift docc plugin 1.3.0
  • Loading branch information
sebsto authored Aug 26, 2024
1 parent 79fa2c2 commit 8676c89
Show file tree
Hide file tree
Showing 23 changed files with 115 additions and 118 deletions.
5 changes: 2 additions & 3 deletions Examples/Foundation/Lambda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ struct ExchangeRatesCalculator {
monthIndex: Array<Date>.Index,
currencies: [String],
state: [Date: ExchangeRates],
callback: @escaping ((Result<[Date: ExchangeRates], Swift.Error>) -> Void))
{
callback: @escaping ((Result<[Date: ExchangeRates], Swift.Error>) -> Void)) {
if monthIndex == months.count {
return callback(.success(state))
}
Expand Down Expand Up @@ -182,7 +181,7 @@ struct ExchangeRatesCalculator {
interval = nil
}

let ratesByCurrencyCode: [String: Decimal?] = Dictionary(uniqueKeysWithValues: try currencyCodes.map {
let ratesByCurrencyCode: [String: Decimal?] = try Dictionary(uniqueKeysWithValues: currencyCodes.map {
let xpathCurrency = $0.replacingOccurrences(of: "'", with: "&apos;")
if let rateString = try document.nodes(forXPath: "/exchangeRateMonthList/exchangeRate/currencyCode[text()='\(xpathCurrency)']/../rateNew/text()").first?.stringValue,
// We must parse the decimal data using the UK locale, not the system one.
Expand Down
22 changes: 11 additions & 11 deletions Examples/LocalDebugging/MyApp/MyApp/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,35 @@ struct ContentView: View {

var body: some View {
VStack(alignment: .leading, spacing: 20) {
TextField("Username", text: $name)
SecureField("Password", text: $password)
let inputIncomplete = name.isEmpty || password.isEmpty
TextField("Username", text: self.$name)
SecureField("Password", text: self.$password)
let inputIncomplete = self.name.isEmpty || self.password.isEmpty
Button {
Task {
isLoading = true
self.isLoading = true
do {
response = try await self.register()
self.response = try await self.register()
} catch {
response = error.localizedDescription
self.response = error.localizedDescription
}
isLoading = false
self.isLoading = false
}
} label: {
Text("Register")
.padding()
.foregroundColor(.white)
.background(.black)
.border(.black, width: 2)
.opacity(isLoading ? 0 : 1)
.opacity(self.isLoading ? 0 : 1)
.overlay {
if isLoading {
if self.isLoading {
ProgressView()
}
}
}
.disabled(inputIncomplete || isLoading)
.disabled(inputIncomplete || self.isLoading)
.opacity(inputIncomplete ? 0.5 : 1)
Text(response)
Text(self.response)
}.padding(100)
}

Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/apple/swift-nio.git", .upToNextMajor(from: "2.67.0")),
.package(url: "https://github.com/apple/swift-log.git", .upToNextMajor(from: "1.5.4")),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-docc-plugin", exact: "1.3.0"),
],
targets: [
.target(name: "AWSLambdaRuntime", dependencies: [
Expand Down
2 changes: 1 addition & 1 deletion Package@swift-5.7.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ let package = Package(
.package(url: "https://github.com/apple/swift-nio.git", .upToNextMajor(from: "2.43.1")),
.package(url: "https://github.com/apple/swift-log.git", .upToNextMajor(from: "1.4.2")),
.package(url: "https://github.com/swift-server/swift-backtrace.git", .upToNextMajor(from: "1.2.3")),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-docc-plugin", exact: "1.3.0"),
],
targets: [
.target(name: "AWSLambdaRuntime", dependencies: [
Expand Down
2 changes: 1 addition & 1 deletion Package@swift-5.8.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ let package = Package(
.package(url: "https://github.com/apple/swift-nio.git", .upToNextMajor(from: "2.43.1")),
.package(url: "https://github.com/apple/swift-log.git", .upToNextMajor(from: "1.4.2")),
.package(url: "https://github.com/swift-server/swift-backtrace.git", .upToNextMajor(from: "1.2.3")),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-docc-plugin", exact: "1.3.0"),
],
targets: [
.target(name: "AWSLambdaRuntime", dependencies: [
Expand Down
2 changes: 1 addition & 1 deletion Sources/AWSLambdaRuntimeCore/ControlPlaneRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ struct ErrorResponse: Hashable, Codable {
}

extension ErrorResponse {
internal func toJSONBytes() -> [UInt8] {
func toJSONBytes() -> [UInt8] {
var bytes = [UInt8]()
bytes.append(UInt8(ascii: "{"))
bytes.append(contentsOf: #""errorType":"#.utf8)
Expand Down
15 changes: 7 additions & 8 deletions Sources/AWSLambdaRuntimeCore/DetachedTasks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,18 @@
//
//===----------------------------------------------------------------------===//
import Foundation
import Logging
import NIOConcurrencyHelpers
import NIOCore
import Logging

/// A container that allows tasks to finish after a synchronous invocation
/// has produced its response.
actor DetachedTasksContainer: Sendable {

struct Context: Sendable {
let eventLoop: EventLoop
let logger: Logger
}

private var context: Context
private var storage: [RegistrationKey: EventLoopFuture<Void>] = [:]

Expand All @@ -40,17 +39,17 @@ actor DetachedTasksContainer: Sendable {
/// - Returns: A `RegistrationKey` for the registered task.
func detached(task: @Sendable @escaping () async -> Void) {
let key = RegistrationKey()
let promise = context.eventLoop.makePromise(of: Void.self)
let promise = self.context.eventLoop.makePromise(of: Void.self)
promise.completeWithTask(task)
let task = promise.futureResult.always { [weak self] result in
let task = promise.futureResult.always { [weak self] _ in
guard let self else { return }
Task {
await self.removeTask(forKey: key)
}
}
self.storage[key] = task
}

func removeTask(forKey key: RegistrationKey) {
self.storage.removeValue(forKey: key)
}
Expand All @@ -59,9 +58,9 @@ actor DetachedTasksContainer: Sendable {
///
/// - Returns: An `EventLoopFuture<Void>` that completes when all tasks have finished.
func awaitAll() -> EventLoopFuture<Void> {
let tasks = storage.values
let tasks = self.storage.values
if tasks.isEmpty {
return context.eventLoop.makeSucceededVoidFuture()
return self.context.eventLoop.makeSucceededVoidFuture()
} else {
let context = context
return EventLoopFuture.andAllComplete(Array(tasks), on: context.eventLoop).flatMap { [weak self] in
Expand Down
9 changes: 5 additions & 4 deletions Sources/AWSLambdaRuntimeCore/HTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import NIOPosix
/// A barebone HTTP client to interact with AWS Runtime Engine which is an HTTP server.
/// Note that Lambda Runtime API dictate that only one requests runs at a time.
/// This means we can avoid locks and other concurrency concern we would otherwise need to build into the client
internal final class HTTPClient {
final class HTTPClient {
private let eventLoop: EventLoop
private let configuration: LambdaConfiguration.RuntimeEngine
private let targetHost: String
Expand Down Expand Up @@ -120,7 +120,7 @@ internal final class HTTPClient {
}
}

internal struct Request: Equatable {
struct Request: Equatable {
let url: String
let method: HTTPMethod
let targetHost: String
Expand All @@ -138,14 +138,14 @@ internal final class HTTPClient {
}
}

internal struct Response: Equatable {
struct Response: Equatable {
var version: HTTPVersion
var status: HTTPResponseStatus
var headers: HTTPHeaders
var body: ByteBuffer?
}

internal enum Errors: Error {
enum Errors: Error {
case connectionResetByPeer
case timeout
case cancelled
Expand Down Expand Up @@ -277,6 +277,7 @@ private final class LambdaChannelHandler: ChannelDuplexHandler {
switch self.state {
case .idle:
break

case .running(let promise, let timeout):
self.state = .idle
timeout?.cancel()
Expand Down
2 changes: 1 addition & 1 deletion Sources/AWSLambdaRuntimeCore/Lambda+LocalServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ extension Lambda {
/// - body: Code to run within the context of the mock server. Typically this would be a Lambda.run function call.
///
/// - note: This API is designed strictly for local testing and is behind a DEBUG flag
internal static func withLocalServer<Value>(invocationEndpoint: String? = nil, _ body: @escaping () -> Value) throws -> Value {
static func withLocalServer<Value>(invocationEndpoint: String? = nil, _ body: @escaping () -> Value) throws -> Value {
let server = LocalLambda.Server(invocationEndpoint: invocationEndpoint)
try server.start().wait()
defer { try! server.stop() }
Expand Down
18 changes: 9 additions & 9 deletions Sources/AWSLambdaRuntimeCore/Lambda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ public enum Lambda {
/// - handlerType: The Handler to create and invoke.
///
/// - note: This is a blocking operation that will run forever, as its lifecycle is managed by the AWS Lambda Runtime Engine.
internal static func run<Handler: SimpleLambdaHandler>(
static func run<Handler: SimpleLambdaHandler>(
configuration: LambdaConfiguration = .init(),
handlerType: Handler.Type
) -> Result<Int, Error> {
Self.run(configuration: configuration, handlerProvider: CodableSimpleLambdaHandler<Handler>.makeHandler(context:))
self.run(configuration: configuration, handlerProvider: CodableSimpleLambdaHandler<Handler>.makeHandler(context:))
}

/// Run a Lambda defined by implementing the ``LambdaHandler`` protocol.
Expand All @@ -56,11 +56,11 @@ public enum Lambda {
/// - handlerType: The Handler to create and invoke.
///
/// - note: This is a blocking operation that will run forever, as its lifecycle is managed by the AWS Lambda Runtime Engine.
internal static func run<Handler: LambdaHandler>(
static func run<Handler: LambdaHandler>(
configuration: LambdaConfiguration = .init(),
handlerType: Handler.Type
) -> Result<Int, Error> {
Self.run(configuration: configuration, handlerProvider: CodableLambdaHandler<Handler>.makeHandler(context:))
self.run(configuration: configuration, handlerProvider: CodableLambdaHandler<Handler>.makeHandler(context:))
}

/// Run a Lambda defined by implementing the ``EventLoopLambdaHandler`` protocol.
Expand All @@ -72,11 +72,11 @@ public enum Lambda {
/// - handlerType: The Handler to create and invoke.
///
/// - note: This is a blocking operation that will run forever, as its lifecycle is managed by the AWS Lambda Runtime Engine.
internal static func run<Handler: EventLoopLambdaHandler>(
static func run<Handler: EventLoopLambdaHandler>(
configuration: LambdaConfiguration = .init(),
handlerType: Handler.Type
) -> Result<Int, Error> {
Self.run(configuration: configuration, handlerProvider: CodableEventLoopLambdaHandler<Handler>.makeHandler(context:))
self.run(configuration: configuration, handlerProvider: CodableEventLoopLambdaHandler<Handler>.makeHandler(context:))
}

/// Run a Lambda defined by implementing the ``ByteBufferLambdaHandler`` protocol.
Expand All @@ -88,11 +88,11 @@ public enum Lambda {
/// - handlerType: The Handler to create and invoke.
///
/// - note: This is a blocking operation that will run forever, as its lifecycle is managed by the AWS Lambda Runtime Engine.
internal static func run(
static func run(
configuration: LambdaConfiguration = .init(),
handlerType: (some ByteBufferLambdaHandler).Type
) -> Result<Int, Error> {
Self.run(configuration: configuration, handlerProvider: handlerType.makeHandler(context:))
self.run(configuration: configuration, handlerProvider: handlerType.makeHandler(context:))
}

/// Run a Lambda defined by implementing the ``LambdaRuntimeHandler`` protocol.
Expand All @@ -101,7 +101,7 @@ public enum Lambda {
/// - handlerProvider: A provider of the ``LambdaRuntimeHandler`` to invoke.
///
/// - note: This is a blocking operation that will run forever, as its lifecycle is managed by the AWS Lambda Runtime Engine.
internal static func run(
static func run(
configuration: LambdaConfiguration = .init(),
handlerProvider: @escaping (LambdaInitializationContext) -> EventLoopFuture<some LambdaRuntimeHandler>
) -> Result<Int, Error> {
Expand Down
2 changes: 1 addition & 1 deletion Sources/AWSLambdaRuntimeCore/LambdaConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Dispatch
import Logging
import NIOCore

internal struct LambdaConfiguration: CustomStringConvertible {
struct LambdaConfiguration: CustomStringConvertible {
let general: General
let lifecycle: Lifecycle
let runtimeEngine: RuntimeEngine
Expand Down
7 changes: 3 additions & 4 deletions Sources/AWSLambdaRuntimeCore/LambdaContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -197,19 +197,18 @@ public struct LambdaContext: CustomDebugStringConvertible, Sendable {
let remaining = deadline - now
return .milliseconds(remaining)
}

var tasks: DetachedTasksContainer {
self.storage.tasks
}



/// Registers a background task that continues running after the synchronous invocation has completed.
/// This is useful for tasks like flushing metrics or performing clean-up operations without delaying the response.
///
/// - Parameter body: An asynchronous closure that performs the background task.
/// - Warning: You will be billed for the milliseconds of Lambda execution time until the very last
/// background task is finished.
public func detachedBackgroundTask(_ body: @escaping @Sendable () async -> ()) {
public func detachedBackgroundTask(_ body: @escaping @Sendable () async -> Void) {
Task {
await self.tasks.detached(task: body)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/AWSLambdaRuntimeCore/LambdaHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ extension LambdaHandler {
/// unchecked sendable wrapper for the handler
/// this is safe since lambda runtime is designed to calls the handler serially
@usableFromInline
internal struct UncheckedSendableHandler<Underlying: LambdaHandler, Event, Output>: @unchecked Sendable where Event == Underlying.Event, Output == Underlying.Output {
struct UncheckedSendableHandler<Underlying: LambdaHandler, Event, Output>: @unchecked Sendable where Event == Underlying.Event, Output == Underlying.Output {
@usableFromInline
let underlying: Underlying

Expand Down
64 changes: 32 additions & 32 deletions Sources/AWSLambdaRuntimeCore/LambdaRequestID.swift
Original file line number Diff line number Diff line change
Expand Up @@ -217,38 +217,38 @@ extension LambdaRequestID {
// are safe and we don't need Swifts safety guards.

characters.withUnsafeBufferPointer { lookup in
string.0 = lookup[Int(uuid.0 >> 4)]
string.1 = lookup[Int(uuid.0 & 0x0F)]
string.2 = lookup[Int(uuid.1 >> 4)]
string.3 = lookup[Int(uuid.1 & 0x0F)]
string.4 = lookup[Int(uuid.2 >> 4)]
string.5 = lookup[Int(uuid.2 & 0x0F)]
string.6 = lookup[Int(uuid.3 >> 4)]
string.7 = lookup[Int(uuid.3 & 0x0F)]
string.9 = lookup[Int(uuid.4 >> 4)]
string.10 = lookup[Int(uuid.4 & 0x0F)]
string.11 = lookup[Int(uuid.5 >> 4)]
string.12 = lookup[Int(uuid.5 & 0x0F)]
string.14 = lookup[Int(uuid.6 >> 4)]
string.15 = lookup[Int(uuid.6 & 0x0F)]
string.16 = lookup[Int(uuid.7 >> 4)]
string.17 = lookup[Int(uuid.7 & 0x0F)]
string.19 = lookup[Int(uuid.8 >> 4)]
string.20 = lookup[Int(uuid.8 & 0x0F)]
string.21 = lookup[Int(uuid.9 >> 4)]
string.22 = lookup[Int(uuid.9 & 0x0F)]
string.24 = lookup[Int(uuid.10 >> 4)]
string.25 = lookup[Int(uuid.10 & 0x0F)]
string.26 = lookup[Int(uuid.11 >> 4)]
string.27 = lookup[Int(uuid.11 & 0x0F)]
string.28 = lookup[Int(uuid.12 >> 4)]
string.29 = lookup[Int(uuid.12 & 0x0F)]
string.30 = lookup[Int(uuid.13 >> 4)]
string.31 = lookup[Int(uuid.13 & 0x0F)]
string.32 = lookup[Int(uuid.14 >> 4)]
string.33 = lookup[Int(uuid.14 & 0x0F)]
string.34 = lookup[Int(uuid.15 >> 4)]
string.35 = lookup[Int(uuid.15 & 0x0F)]
string.0 = lookup[Int(self.uuid.0 >> 4)]
string.1 = lookup[Int(self.uuid.0 & 0x0F)]
string.2 = lookup[Int(self.uuid.1 >> 4)]
string.3 = lookup[Int(self.uuid.1 & 0x0F)]
string.4 = lookup[Int(self.uuid.2 >> 4)]
string.5 = lookup[Int(self.uuid.2 & 0x0F)]
string.6 = lookup[Int(self.uuid.3 >> 4)]
string.7 = lookup[Int(self.uuid.3 & 0x0F)]
string.9 = lookup[Int(self.uuid.4 >> 4)]
string.10 = lookup[Int(self.uuid.4 & 0x0F)]
string.11 = lookup[Int(self.uuid.5 >> 4)]
string.12 = lookup[Int(self.uuid.5 & 0x0F)]
string.14 = lookup[Int(self.uuid.6 >> 4)]
string.15 = lookup[Int(self.uuid.6 & 0x0F)]
string.16 = lookup[Int(self.uuid.7 >> 4)]
string.17 = lookup[Int(self.uuid.7 & 0x0F)]
string.19 = lookup[Int(self.uuid.8 >> 4)]
string.20 = lookup[Int(self.uuid.8 & 0x0F)]
string.21 = lookup[Int(self.uuid.9 >> 4)]
string.22 = lookup[Int(self.uuid.9 & 0x0F)]
string.24 = lookup[Int(self.uuid.10 >> 4)]
string.25 = lookup[Int(self.uuid.10 & 0x0F)]
string.26 = lookup[Int(self.uuid.11 >> 4)]
string.27 = lookup[Int(self.uuid.11 & 0x0F)]
string.28 = lookup[Int(self.uuid.12 >> 4)]
string.29 = lookup[Int(self.uuid.12 & 0x0F)]
string.30 = lookup[Int(self.uuid.13 >> 4)]
string.31 = lookup[Int(self.uuid.13 & 0x0F)]
string.32 = lookup[Int(self.uuid.14 >> 4)]
string.33 = lookup[Int(self.uuid.14 & 0x0F)]
string.34 = lookup[Int(self.uuid.15 >> 4)]
string.35 = lookup[Int(self.uuid.15 & 0x0F)]
}

return string
Expand Down
Loading

0 comments on commit 8676c89

Please sign in to comment.