Skip to content

Commit ba55312

Browse files
committed
use swift-format script from swift-nio project
1 parent d803861 commit ba55312

File tree

8 files changed

+151
-85
lines changed

8 files changed

+151
-85
lines changed

Plugins/AWSLambdaPackager/PluginUtils.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ struct Utils {
4747
let outputSync = DispatchGroup()
4848
let outputQueue = DispatchQueue(label: "AWSLambdaPackager.output")
4949
let unsafeTransfer = UnsafeTransfer(value: stdout)
50-
let outputHandler = { @Sendable(data:Data?) in
50+
let outputHandler = { @Sendable (data: Data?) in
5151
dispatchPrecondition(condition: .onQueue(outputQueue))
5252

5353
outputSync.enter()

Sources/AWSLambdaRuntimeCore/ControlPlaneRequest.swift

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,38 +29,38 @@ enum ControlPlaneResponse: Hashable {
2929
}
3030

3131
package
32-
struct InvocationMetadata: Hashable {
33-
package let requestID: String
34-
package let deadlineInMillisSinceEpoch: Int64
35-
package let invokedFunctionARN: String
36-
package let traceID: String
37-
package let clientContext: String?
38-
package let cognitoIdentity: String?
32+
struct InvocationMetadata: Hashable
33+
{
34+
package let requestID: String
35+
package let deadlineInMillisSinceEpoch: Int64
36+
package let invokedFunctionARN: String
37+
package let traceID: String
38+
package let clientContext: String?
39+
package let cognitoIdentity: String?
3940

40-
package init(headers: HTTPHeaders) throws
41-
(LambdaRuntimeError) {
42-
guard let requestID = headers.first(name: AmazonHeaders.requestID), !requestID.isEmpty else {
43-
throw LambdaRuntimeError(code: .nextInvocationMissingHeaderRequestID)
44-
}
41+
package init(headers: HTTPHeaders) throws(LambdaRuntimeError) {
42+
guard let requestID = headers.first(name: AmazonHeaders.requestID), !requestID.isEmpty else {
43+
throw LambdaRuntimeError(code: .nextInvocationMissingHeaderRequestID)
44+
}
4545

46-
guard let deadline = headers.first(name: AmazonHeaders.deadline),
47-
let unixTimeInMilliseconds = Int64(deadline)
48-
else {
49-
throw LambdaRuntimeError(code: .nextInvocationMissingHeaderDeadline)
50-
}
46+
guard let deadline = headers.first(name: AmazonHeaders.deadline),
47+
let unixTimeInMilliseconds = Int64(deadline)
48+
else {
49+
throw LambdaRuntimeError(code: .nextInvocationMissingHeaderDeadline)
50+
}
5151

52-
guard let invokedFunctionARN = headers.first(name: AmazonHeaders.invokedFunctionARN) else {
53-
throw LambdaRuntimeError(code: .nextInvocationMissingHeaderInvokeFuctionARN)
54-
}
52+
guard let invokedFunctionARN = headers.first(name: AmazonHeaders.invokedFunctionARN) else {
53+
throw LambdaRuntimeError(code: .nextInvocationMissingHeaderInvokeFuctionARN)
54+
}
5555

56-
self.requestID = requestID
57-
self.deadlineInMillisSinceEpoch = unixTimeInMilliseconds
58-
self.invokedFunctionARN = invokedFunctionARN
59-
self.traceID =
60-
headers.first(name: AmazonHeaders.traceID) ?? "Root=\(AmazonHeaders.generateXRayTraceID());Sampled=0"
61-
self.clientContext = headers["Lambda-Runtime-Client-Context"].first
62-
self.cognitoIdentity = headers["Lambda-Runtime-Cognito-Identity"].first
63-
}
56+
self.requestID = requestID
57+
self.deadlineInMillisSinceEpoch = unixTimeInMilliseconds
58+
self.invokedFunctionARN = invokedFunctionARN
59+
self.traceID =
60+
headers.first(name: AmazonHeaders.traceID) ?? "Root=\(AmazonHeaders.generateXRayTraceID());Sampled=0"
61+
self.clientContext = headers["Lambda-Runtime-Client-Context"].first
62+
self.cognitoIdentity = headers["Lambda-Runtime-Cognito-Identity"].first
63+
}
6464
}
6565

6666
struct ErrorResponse: Hashable, Codable {

Sources/AWSLambdaRuntimeCore/Lambda.swift

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,38 +30,39 @@ import ucrt
3030
#endif
3131

3232
public enum Lambda {
33-
package
34-
static func runLoop<RuntimeClient: LambdaRuntimeClientProtocol, Handler>(
35-
runtimeClient: RuntimeClient,
36-
handler: Handler,
37-
logger: Logger
38-
) async throws where Handler: StreamingLambdaHandler {
39-
var handler = handler
33+
package
34+
static func runLoop<RuntimeClient: LambdaRuntimeClientProtocol, Handler>(
35+
runtimeClient: RuntimeClient,
36+
handler: Handler,
37+
logger: Logger
38+
) async throws where Handler: StreamingLambdaHandler
39+
{
40+
var handler = handler
4041

41-
while !Task.isCancelled {
42-
let (invocation, writer) = try await runtimeClient.nextInvocation()
42+
while !Task.isCancelled {
43+
let (invocation, writer) = try await runtimeClient.nextInvocation()
4344

44-
do {
45-
try await handler.handle(
46-
invocation.event,
47-
responseWriter: writer,
48-
context: LambdaContext(
49-
requestID: invocation.metadata.requestID,
50-
traceID: invocation.metadata.traceID,
51-
invokedFunctionARN: invocation.metadata.invokedFunctionARN,
52-
deadline: DispatchWallTime(millisSinceEpoch: invocation.metadata.deadlineInMillisSinceEpoch),
53-
logger: logger
45+
do {
46+
try await handler.handle(
47+
invocation.event,
48+
responseWriter: writer,
49+
context: LambdaContext(
50+
requestID: invocation.metadata.requestID,
51+
traceID: invocation.metadata.traceID,
52+
invokedFunctionARN: invocation.metadata.invokedFunctionARN,
53+
deadline: DispatchWallTime(millisSinceEpoch: invocation.metadata.deadlineInMillisSinceEpoch),
54+
logger: logger
55+
)
5456
)
55-
)
56-
} catch {
57-
try await writer.reportError(error)
58-
continue
57+
} catch {
58+
try await writer.reportError(error)
59+
continue
60+
}
5961
}
6062
}
61-
}
6263

63-
/// The default EventLoop the Lambda is scheduled on.
64-
public static var defaultEventLoop: any EventLoop = NIOSingletons.posixEventLoopGroup.next()
64+
/// The default EventLoop the Lambda is scheduled on.
65+
public static var defaultEventLoop: any EventLoop = NIOSingletons.posixEventLoopGroup.next()
6566
}
6667

6768
// MARK: - Public API

Sources/AWSLambdaRuntimeCore/LambdaContext.swift

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -120,20 +120,22 @@ public struct LambdaContext: CustomDebugStringConvertible, Sendable {
120120
"\(Self.self)(requestID: \(self.requestID), traceID: \(self.traceID), invokedFunctionARN: \(self.invokedFunctionARN), cognitoIdentity: \(self.cognitoIdentity ?? "nil"), clientContext: \(self.clientContext ?? "nil"), deadline: \(self.deadline))"
121121
}
122122

123-
/// This interface is not part of the public API and must not be used by adopters. This API is not part of semver versioning.
124-
package
125-
static func __forTestsOnly(
126-
requestID: String,
127-
traceID: String,
128-
invokedFunctionARN: String,
129-
timeout: DispatchTimeInterval,
130-
logger: Logger
131-
) -> LambdaContext {
132-
LambdaContext(
133-
requestID: requestID,
134-
traceID: traceID,
135-
invokedFunctionARN: invokedFunctionARN,
136-
deadline: .now() + timeout,
137-
logger: logger
138-
)
139-
}}
123+
/// This interface is not part of the public API and must not be used by adopters. This API is not part of semver versioning.
124+
package
125+
static func __forTestsOnly(
126+
requestID: String,
127+
traceID: String,
128+
invokedFunctionARN: String,
129+
timeout: DispatchTimeInterval,
130+
logger: Logger
131+
) -> LambdaContext
132+
{
133+
LambdaContext(
134+
requestID: requestID,
135+
traceID: traceID,
136+
invokedFunctionARN: invokedFunctionARN,
137+
deadline: .now() + timeout,
138+
logger: logger
139+
)
140+
}
141+
}

Sources/AWSLambdaRuntimeCore/LambdaRuntimeClient.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ private final class LambdaChannelHandler<Delegate: LambdaChannelHandlerDelegate>
462462
]
463463
}
464464

465-
func nextInvocation(isolation: isolated (any Actor)? = #isolation) async throws -> Invocation {
465+
func nextInvocation(isolation: isolated (any Actor)? = #isolation) async throws -> Invocation {
466466
switch self.state {
467467
case .connected(let context, .idle):
468468
return try await withCheckedThrowingContinuation {
@@ -485,7 +485,7 @@ private final class LambdaChannelHandler<Delegate: LambdaChannelHandlerDelegate>
485485

486486
func reportError(
487487
isolation: isolated (any Actor)? =
488-
#isolation,
488+
#isolation,
489489
_ error: any Error,
490490
requestID: String
491491
) async throws {

Sources/AWSLambdaRuntimeCore/LambdaRuntimeClientProtocol.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,29 @@
1515
import NIOCore
1616

1717
package
18-
protocol LambdaRuntimeClientResponseStreamWriter: LambdaResponseStreamWriter {
18+
protocol LambdaRuntimeClientResponseStreamWriter: LambdaResponseStreamWriter
19+
{
1920
func write(_ buffer: ByteBuffer) async throws
2021
func finish() async throws
2122
func writeAndFinish(_ buffer: ByteBuffer) async throws
2223
func reportError(_ error: any Error) async throws
2324
}
2425

2526
package
26-
protocol LambdaRuntimeClientProtocol {
27+
protocol LambdaRuntimeClientProtocol
28+
{
2729
associatedtype Writer: LambdaRuntimeClientResponseStreamWriter
2830

2931
func nextInvocation() async throws -> (Invocation, Writer)
3032
}
3133

3234
package
33-
struct Invocation {
34-
package var metadata: InvocationMetadata
35-
package var event: ByteBuffer
35+
struct Invocation
36+
{
37+
package var metadata: InvocationMetadata
38+
package var event: ByteBuffer
3639

37-
package init(metadata: InvocationMetadata, event: ByteBuffer) {
40+
package init(metadata: InvocationMetadata, event: ByteBuffer) {
3841
self.metadata = metadata
3942
self.event = event
4043
}

Sources/AWSLambdaRuntimeCore/LambdaRuntimeError.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
package
16-
struct LambdaRuntimeError: Error {
17-
package enum Code {
16+
struct LambdaRuntimeError: Error
17+
{
18+
package enum Code {
1819
case closingRuntimeClient
1920

2021
case connectionToControlPlaneLost
@@ -35,12 +36,12 @@ struct LambdaRuntimeError: Error {
3536
case invalidPort
3637
}
3738

38-
package init(code: Code, underlying: (any Error)? = nil) {
39+
package init(code: Code, underlying: (any Error)? = nil) {
3940
self.code = code
4041
self.underlying = underlying
4142
}
4243

43-
package var code: Code
44-
package var underlying: (any Error)?
44+
package var code: Code
45+
package var underlying: (any Error)?
4546

4647
}

scripts/check-swift-format.sh

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/bin/bash
2+
##===----------------------------------------------------------------------===##
3+
##
4+
## This source file is part of the SwiftAWSLambdaRuntime open source project
5+
##
6+
## Copyright (c) 2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors
7+
## Licensed under Apache License v2.0
8+
##
9+
## See LICENSE.txt for license information
10+
## See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
11+
##
12+
## SPDX-License-Identifier: Apache-2.0
13+
##
14+
##===----------------------------------------------------------------------===##
15+
##===----------------------------------------------------------------------===##
16+
##
17+
## This source file is part of the SwiftNIO open source project
18+
##
19+
## Copyright (c) 2024 Apple Inc. and the SwiftNIO project authors
20+
## Licensed under Apache License v2.0
21+
##
22+
## See LICENSE.txt for license information
23+
## See CONTRIBUTORS.txt for the list of SwiftNIO project authors
24+
##
25+
## SPDX-License-Identifier: Apache-2.0
26+
##
27+
##===----------------------------------------------------------------------===##
28+
set -euo pipefail
29+
30+
log() { printf -- "** %s\n" "$*" >&2; }
31+
error() { printf -- "** ERROR: %s\n" "$*" >&2; }
32+
fatal() { error "$@"; exit 1; }
33+
34+
35+
if [[ -f .swiftformatignore ]]; then
36+
log "Found swiftformatignore file..."
37+
38+
log "Running swift format format..."
39+
tr '\n' '\0' < .swiftformatignore| xargs -0 -I% printf '":(exclude)%" '| xargs git ls-files -z '*.swift' | xargs -0 swift format format --parallel --in-place
40+
41+
log "Running swift format lint..."
42+
43+
tr '\n' '\0' < .swiftformatignore | xargs -0 -I% printf '":(exclude)%" '| xargs git ls-files -z '*.swift' | xargs -0 swift format lint --strict --parallel
44+
else
45+
log "Running swift format format..."
46+
git ls-files -z '*.swift' | xargs -0 swift format format --parallel --in-place
47+
48+
log "Running swift format lint..."
49+
50+
git ls-files -z '*.swift' | xargs -0 swift format lint --strict --parallel
51+
fi
52+
53+
54+
55+
log "Checking for modified files..."
56+
57+
GIT_PAGER='' git diff --exit-code '*.swift'
58+
59+
log "✅ Found no formatting issues."

0 commit comments

Comments
 (0)