Skip to content

Commit daf16ef

Browse files
committed
PR review
1 parent 3f849a0 commit daf16ef

File tree

6 files changed

+26
-44
lines changed

6 files changed

+26
-44
lines changed

Package.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ let package = Package(
1515
.library(name: "AWSLambdaTesting", targets: ["AWSLambdaTesting"]),
1616
],
1717
dependencies: [
18-
.package(url: "https://github.com/apple/swift-nio.git", .upToNextMajor(from: "2.17.0")),
18+
.package(url: "https://github.com/apple/swift-nio.git", .branch("main")),
1919
.package(url: "https://github.com/apple/swift-log.git", .upToNextMajor(from: "1.0.0")),
2020
.package(url: "https://github.com/swift-server/swift-backtrace.git", .upToNextMajor(from: "1.1.0")),
2121
],
@@ -29,6 +29,7 @@ let package = Package(
2929
.product(name: "Logging", package: "swift-log"),
3030
.product(name: "Backtrace", package: "swift-backtrace"),
3131
.product(name: "NIOHTTP1", package: "swift-nio"),
32+
.product(name: "_NIOConcurrency", package: "swift-nio")
3233
]),
3334
.testTarget(name: "AWSLambdaRuntimeCoreTests", dependencies: [
3435
.byName(name: "AWSLambdaRuntimeCore"),

Sources/AWSLambdaRuntime/Lambda+Codable.swift

+10-16
Original file line numberDiff line numberDiff line change
@@ -82,29 +82,23 @@ internal struct CodableVoidClosureWrapper<In: Decodable>: LambdaHandler {
8282

8383
#if compiler(>=5.5) && $AsyncAwait
8484
extension Lambda {
85-
/// An async Lambda Closure that takes a `In: Decodable` and returns an `Out: Encodable`
86-
public typealias CodableAsyncClosure<In: Decodable, Out: Encodable> = (Lambda.Context, In) async throws -> Out
87-
8885
/// Run a Lambda defined by implementing the `CodableAsyncClosure` function.
8986
///
9087
/// - parameters:
9188
/// - closure: `CodableAsyncClosure` based Lambda.
9289
///
9390
/// - note: This is a blocking operation that will run forever, as its lifecycle is managed by the AWS Lambda Runtime Engine.
94-
public static func run<In: Decodable, Out: Encodable>(_ closure: @escaping CodableAsyncClosure<In, Out>) {
91+
public static func run<In: Decodable, Out: Encodable>(_ closure: @escaping (In, Lambda.Context) async throws -> Out) {
9592
self.run(CodableAsyncWrapper(closure))
9693
}
9794

98-
/// An asynchronous Lambda Closure that takes a `In: Decodable` and returns nothing.
99-
public typealias CodableVoidAsyncClosure<In: Decodable> = (Lambda.Context, In) async throws -> Void
100-
10195
/// Run a Lambda defined by implementing the `CodableVoidAsyncClosure` function.
10296
///
10397
/// - parameters:
10498
/// - closure: `CodableVoidAsyncClosure` based Lambda.
10599
///
106100
/// - note: This is a blocking operation that will run forever, as its lifecycle is managed by the AWS Lambda Runtime Engine.
107-
public static func run<In: Decodable>(_ closure: @escaping CodableVoidAsyncClosure<In>) {
101+
public static func run<In: Decodable>(_ closure: @escaping (In, Lambda.Context) async throws -> Void) {
108102
self.run(CodableVoidAsyncWrapper(closure))
109103
}
110104
}
@@ -113,29 +107,29 @@ internal struct CodableAsyncWrapper<In: Decodable, Out: Encodable>: AsyncLambdaH
113107
typealias In = In
114108
typealias Out = Out
115109

116-
private let closure: Lambda.CodableAsyncClosure<In, Out>
110+
private let closure: (In, Lambda.Context) async throws -> Out
117111

118-
init(_ closure: @escaping Lambda.CodableAsyncClosure<In, Out>) {
112+
init(_ closure: @escaping (In, Lambda.Context) async throws -> Out) {
119113
self.closure = closure
120114
}
121115

122-
func handle(context: Lambda.Context, event: In) async throws -> Out {
123-
try await self.closure(context, event)
116+
func handle(event: In, context: Lambda.Context) async throws -> Out {
117+
try await self.closure(event, context)
124118
}
125119
}
126120

127121
internal struct CodableVoidAsyncWrapper<In: Decodable>: AsyncLambdaHandler {
128122
typealias In = In
129123
typealias Out = Void
130124

131-
private let closure: Lambda.CodableVoidAsyncClosure<In>
125+
private let closure: (In, Lambda.Context) async throws -> Void
132126

133-
init(_ closure: @escaping Lambda.CodableVoidAsyncClosure<In>) {
127+
init(_ closure: @escaping (In, Lambda.Context) async throws -> Void) {
134128
self.closure = closure
135129
}
136130

137-
func handle(context: Lambda.Context, event: In) async throws {
138-
try await self.closure(context, event)
131+
func handle(event: In, context: Lambda.Context) async throws {
132+
try await self.closure(event, context)
139133
}
140134
}
141135
#endif

Sources/AWSLambdaRuntimeCore/Lambda.swift

+4-10
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import Darwin.C
2121
import Backtrace
2222
import Logging
2323
import NIO
24+
import _NIOConcurrency
2425

2526
public enum Lambda {
2627
public typealias Handler = ByteBufferLambdaHandler
@@ -59,17 +60,10 @@ public enum Lambda {
5960
#if compiler(>=5.5) && $AsyncAwait
6061
public static func run(_ factory: @escaping (InitializationContext) async throws -> Handler) {
6162
self.run { context -> EventLoopFuture<Handler> in
62-
@asyncHandler func _createLambda(_ context: InitializationContext, promise: EventLoopPromise<Handler>) {
63-
do {
64-
let handler = try await factory(context)
65-
promise.succeed(handler)
66-
} catch {
67-
promise.fail(error)
68-
}
69-
}
70-
7163
let promise = context.eventLoop.makePromise(of: Handler.self)
72-
_createLambda(context, promise: promise)
64+
promise.completeWithAsync {
65+
try await factory(context)
66+
}
7367
return promise.futureResult
7468
}
7569
}

Sources/AWSLambdaRuntimeCore/LambdaHandler.swift

+5-12
Original file line numberDiff line numberDiff line change
@@ -92,27 +92,20 @@ public protocol AsyncLambdaHandler: EventLoopLambdaHandler {
9292
/// Concrete Lambda handlers implement this method to provide the Lambda functionality.
9393
///
9494
/// - parameters:
95-
/// - context: Runtime `Context`.
9695
/// - event: Event of type `In` representing the event or request.
96+
/// - context: Runtime `Context`.
9797
///
9898
/// - Returns: A Lambda result ot type `Out`.
99-
func handle(context: Lambda.Context, event: In) async throws -> Out
99+
func handle(event: In, context: Lambda.Context) async throws -> Out
100100
}
101101

102102
extension AsyncLambdaHandler {
103103
public func handle(context: Lambda.Context, event: In) -> EventLoopFuture<Out> {
104104
let promise = context.eventLoop.makePromise(of: Out.self)
105-
self._run(context: context, event: event, promise: promise)
106-
return promise.futureResult
107-
}
108-
109-
@asyncHandler private func _run(context: Lambda.Context, event: In, promise: EventLoopPromise<Out>) {
110-
do {
111-
let result = try await self.handle(context: context, event: event)
112-
promise.succeed(result)
113-
} catch {
114-
promise.fail(error)
105+
promise.completeWithAsync {
106+
try await self.handle(event: event, context: context)
115107
}
108+
return promise.futureResult
116109
}
117110
}
118111
#endif

Tests/AWSLambdaRuntimeCoreTests/LambdaHandlerTest.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class LambdaHandlerTest: XCTestCase {
9292
typealias In = String
9393
typealias Out = String
9494

95-
func handle(context: Lambda.Context, event: String) async throws -> String {
95+
func handle(event: String, context: Lambda.Context) async throws -> String {
9696
event
9797
}
9898
}
@@ -112,7 +112,7 @@ class LambdaHandlerTest: XCTestCase {
112112
typealias In = String
113113
typealias Out = Void
114114

115-
func handle(context: Lambda.Context, event: String) async throws {}
115+
func handle(event: String, context: Lambda.Context) async throws {}
116116
}
117117

118118
let maxTimes = Int.random(in: 1 ... 10)
@@ -130,7 +130,7 @@ class LambdaHandlerTest: XCTestCase {
130130
typealias In = String
131131
typealias Out = String
132132

133-
func handle(context: Lambda.Context, event: String) async throws -> String {
133+
func handle(event: String, context: Lambda.Context) async throws -> String {
134134
throw TestError("boom")
135135
}
136136
}

Tests/AWSLambdaRuntimeTests/Lambda+CodeableTest.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class CodableLambdaTest: XCTestCase {
6969
var inputBuffer: ByteBuffer?
7070
var outputBuffer: ByteBuffer?
7171

72-
let closureWrapper = CodableVoidAsyncWrapper { (_, req: Request) in
72+
let closureWrapper = CodableVoidAsyncWrapper { (req: Request, _) in
7373
XCTAssertEqual(request, req)
7474
}
7575

@@ -84,7 +84,7 @@ class CodableLambdaTest: XCTestCase {
8484
var outputBuffer: ByteBuffer?
8585
var response: Response?
8686

87-
let closureWrapper = CodableAsyncWrapper { (_, req: Request) -> Response in
87+
let closureWrapper = CodableAsyncWrapper { (req: Request, _) -> Response in
8888
XCTAssertEqual(req, request)
8989
return Response(requestId: req.requestId)
9090
}

0 commit comments

Comments
 (0)