Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Examples/LambdaFunctions/Sources/Benchmark/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct BenchmarkHandler: EventLoopLambdaHandler {
typealias In = String
typealias Out = String

func handle(event: String, context: Lambda.Context) -> EventLoopFuture<String> {
func handle(_ event: String, context: Lambda.Context) -> EventLoopFuture<String> {
context.eventLoop.makeSucceededFuture("hello, world!")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ struct CurrencyExchangeHandler: LambdaHandler {
self.calculator = ExchangeRatesCalculator()
}

func handle(event: Request, context: Lambda.Context) async throws -> [Exchange] {
func handle(_ event: Request, context: Lambda.Context) async throws -> [Exchange] {
try await withCheckedThrowingContinuation { continuation in
self.calculator.run(logger: context.logger) { result in
switch result {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct ErrorsHappenHandler: LambdaHandler {

init(context: Lambda.InitializationContext) async throws {}

func handle(event request: Request, context: Lambda.Context) async throws -> Response {
func handle(_ request: Request, context: Lambda.Context) async throws -> Response {
// switch over the error type "requested" by the request, and trigger such error accordingly
switch request.error {
// no error here!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct HelloWorldHandler: LambdaHandler {
// setup your resources that you want to reuse here.
}

func handle(event: String, context: Lambda.Context) async throws -> String {
func handle(_ event: String, context: Lambda.Context) async throws -> String {
"hello, world"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct MyLambdaHandler: LambdaHandler {
// setup your resources that you want to reuse for every invocation here.
}

func handle(event request: Request, context: Lambda.Context) async throws -> Response {
func handle(_ request: Request, context: Lambda.Context) async throws -> Response {
// TODO: something useful
Response(message: "Hello, \(request.name)!")
}
Expand Down
14 changes: 7 additions & 7 deletions Sources/AWSLambdaRuntimeCore/LambdaHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ public protocol LambdaHandler: EventLoopLambdaHandler {
/// - context: Runtime `Context`.
///
/// - Returns: A Lambda result ot type `Out`.
func handle(event: In, context: Lambda.Context) async throws -> Out
func handle(_ event: In, context: Lambda.Context) async throws -> Out
}

@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
extension LambdaHandler {
public func handle(event: In, context: Lambda.Context) -> EventLoopFuture<Out> {
public func handle(_ event: In, context: Lambda.Context) -> EventLoopFuture<Out> {
let promise = context.eventLoop.makePromise(of: Out.self)
promise.completeWithTask {
try await self.handle(event: event, context: context)
try await self.handle(event, context: context)
}
return promise.futureResult
}
Expand Down Expand Up @@ -82,7 +82,7 @@ public protocol EventLoopLambdaHandler: ByteBufferLambdaHandler {
///
/// - Returns: An `EventLoopFuture` to report the result of the Lambda back to the runtime engine.
/// The `EventLoopFuture` should be completed with either a response of type `Out` or an `Error`
func handle(event: In, context: Lambda.Context) -> EventLoopFuture<Out>
func handle(_ event: In, context: Lambda.Context) -> EventLoopFuture<Out>

/// Encode a response of type `Out` to `ByteBuffer`
/// Concrete Lambda handlers implement this method to provide coding functionality.
Expand All @@ -106,15 +106,15 @@ public protocol EventLoopLambdaHandler: ByteBufferLambdaHandler {
extension EventLoopLambdaHandler {
/// Driver for `ByteBuffer` -> `In` decoding and `Out` -> `ByteBuffer` encoding
@inlinable
public func handle(event: ByteBuffer, context: Lambda.Context) -> EventLoopFuture<ByteBuffer?> {
public func handle(_ event: ByteBuffer, context: Lambda.Context) -> EventLoopFuture<ByteBuffer?> {
let input: In
do {
input = try self.decode(buffer: event)
} catch {
return context.eventLoop.makeFailedFuture(CodecError.requestDecoding(error))
}

return self.handle(event: input, context: context).flatMapThrowing { output in
return self.handle(input, context: context).flatMapThrowing { output in
do {
return try self.encode(allocator: context.allocator, value: output)
} catch {
Expand Down Expand Up @@ -148,7 +148,7 @@ public protocol ByteBufferLambdaHandler {
///
/// - Returns: An `EventLoopFuture` to report the result of the Lambda back to the runtime engine.
/// The `EventLoopFuture` should be completed with either a response encoded as `ByteBuffer` or an `Error`
func handle(event: ByteBuffer, context: Lambda.Context) -> EventLoopFuture<ByteBuffer?>
func handle(_ event: ByteBuffer, context: Lambda.Context) -> EventLoopFuture<ByteBuffer?>

/// Clean up the Lambda resources asynchronously.
/// Concrete Lambda handlers implement this method to shutdown resources like `HTTPClient`s and database connections.
Expand Down
4 changes: 2 additions & 2 deletions Sources/AWSLambdaRuntimeCore/LambdaRunner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ extension Lambda {
self.isGettingNextInvocation = true
return self.runtimeClient.getNextInvocation(logger: logger).peekError { error in
logger.error("could not fetch work from lambda runtime engine: \(error)")
}.flatMap { invocation, event in
}.flatMap { invocation, bytes in
// 2. send invocation to handler
self.isGettingNextInvocation = false
let context = Context(logger: logger,
eventLoop: self.eventLoop,
allocator: self.allocator,
invocation: invocation)
logger.debug("sending invocation to lambda handler \(handler)")
return handler.handle(event: event, context: context)
return handler.handle(bytes, context: context)
// Hopping back to "our" EventLoop is important in case the handler returns a future that
// originiated from a foreign EventLoop/EventLoopGroup.
// This can happen if the handler uses a library (lets say a DB client) that manages its own threads/loops
Expand Down
2 changes: 1 addition & 1 deletion Sources/AWSLambdaTesting/Lambda+Testing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ extension Lambda {
let handler = try promise.futureResult.wait()

return try eventLoop.flatSubmit {
handler.handle(event: event, context: context)
handler.handle(event, context: context)
}.wait()
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/CodableSample/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct Handler: EventLoopLambdaHandler {
typealias In = Request
typealias Out = Response

func handle(event: Request, context: Lambda.Context) -> EventLoopFuture<Response> {
func handle(_ event: Request, context: Lambda.Context) -> EventLoopFuture<Response> {
// as an example, respond with the input event's reversed body
context.eventLoop.makeSucceededFuture(Response(body: String(event.body.reversed())))
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/StringSample/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct Handler: EventLoopLambdaHandler {
typealias In = String
typealias Out = String

func handle(event: String, context: Lambda.Context) -> EventLoopFuture<String> {
func handle(_ event: String, context: Lambda.Context) -> EventLoopFuture<String> {
// as an example, respond with the event's reversed body
context.eventLoop.makeSucceededFuture(String(event.reversed()))
}
Expand Down
16 changes: 8 additions & 8 deletions Tests/AWSLambdaRuntimeCoreTests/LambdaHandlerTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class LambdaHandlerTest: XCTestCase {
self.initialized = true
}

func handle(event: String, context: Lambda.Context) async throws -> String {
func handle(_ event: String, context: Lambda.Context) async throws -> String {
event
}
}
Expand Down Expand Up @@ -68,7 +68,7 @@ class LambdaHandlerTest: XCTestCase {
throw TestError("kaboom")
}

func handle(event: String, context: Lambda.Context) async throws {
func handle(_ event: String, context: Lambda.Context) async throws {
XCTFail("How can this be called if init failed")
}
}
Expand All @@ -91,7 +91,7 @@ class LambdaHandlerTest: XCTestCase {

init(context: Lambda.InitializationContext) {}

func handle(event: String, context: Lambda.Context) async throws -> String {
func handle(_ event: String, context: Lambda.Context) async throws -> String {
event
}
}
Expand All @@ -114,7 +114,7 @@ class LambdaHandlerTest: XCTestCase {

init(context: Lambda.InitializationContext) {}

func handle(event: String, context: Lambda.Context) async throws {}
func handle(_ event: String, context: Lambda.Context) async throws {}
}

let maxTimes = Int.random(in: 1 ... 10)
Expand All @@ -136,7 +136,7 @@ class LambdaHandlerTest: XCTestCase {

init(context: Lambda.InitializationContext) {}

func handle(event: String, context: Lambda.Context) async throws -> String {
func handle(_ event: String, context: Lambda.Context) async throws -> String {
throw TestError("boom")
}
}
Expand All @@ -159,7 +159,7 @@ class LambdaHandlerTest: XCTestCase {
typealias In = String
typealias Out = String

func handle(event: String, context: Lambda.Context) -> EventLoopFuture<String> {
func handle(_ event: String, context: Lambda.Context) -> EventLoopFuture<String> {
context.eventLoop.makeSucceededFuture(event)
}
}
Expand All @@ -181,7 +181,7 @@ class LambdaHandlerTest: XCTestCase {
typealias In = String
typealias Out = Void

func handle(event: String, context: Lambda.Context) -> EventLoopFuture<Void> {
func handle(_ event: String, context: Lambda.Context) -> EventLoopFuture<Void> {
context.eventLoop.makeSucceededFuture(())
}
}
Expand All @@ -203,7 +203,7 @@ class LambdaHandlerTest: XCTestCase {
typealias In = String
typealias Out = String

func handle(event: String, context: Lambda.Context) -> EventLoopFuture<String> {
func handle(_ event: String, context: Lambda.Context) -> EventLoopFuture<String> {
context.eventLoop.makeFailedFuture(TestError("boom"))
}
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/AWSLambdaRuntimeCoreTests/LambdaHandlers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct EchoHandler: EventLoopLambdaHandler {
typealias In = String
typealias Out = String

func handle(event: String, context: Lambda.Context) -> EventLoopFuture<String> {
func handle(_ event: String, context: Lambda.Context) -> EventLoopFuture<String> {
context.eventLoop.makeSucceededFuture(event)
}
}
Expand All @@ -34,7 +34,7 @@ struct FailedHandler: EventLoopLambdaHandler {
self.reason = reason
}

func handle(event: String, context: Lambda.Context) -> EventLoopFuture<Void> {
func handle(_ event: String, context: Lambda.Context) -> EventLoopFuture<Void> {
context.eventLoop.makeFailedFuture(TestError(self.reason))
}
}
2 changes: 1 addition & 1 deletion Tests/AWSLambdaRuntimeCoreTests/LambdaLifecycleTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class LambdaLifecycleTest: XCTestCase {
self.shutdown = shutdown
}

func handle(event: ByteBuffer, context: Lambda.Context) -> EventLoopFuture<ByteBuffer?> {
func handle(_ event: ByteBuffer, context: Lambda.Context) -> EventLoopFuture<ByteBuffer?> {
self.handler(context, event)
}

Expand Down
16 changes: 8 additions & 8 deletions Tests/AWSLambdaRuntimeTests/Lambda+CodeableTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class CodableLambdaTest: XCTestCase {

let expected: Request

func handle(event: Request, context: Lambda.Context) -> EventLoopFuture<Void> {
func handle(_ event: Request, context: Lambda.Context) -> EventLoopFuture<Void> {
XCTAssertEqual(event, self.expected)
return context.eventLoop.makeSucceededVoidFuture()
}
Expand All @@ -52,7 +52,7 @@ class CodableLambdaTest: XCTestCase {
let handler = Handler(expected: request)

XCTAssertNoThrow(inputBuffer = try JSONEncoder().encode(request, using: self.allocator))
XCTAssertNoThrow(outputBuffer = try handler.handle(event: XCTUnwrap(inputBuffer), context: self.newContext()).wait())
XCTAssertNoThrow(outputBuffer = try handler.handle(XCTUnwrap(inputBuffer), context: self.newContext()).wait())
XCTAssertNil(outputBuffer)
}

Expand All @@ -68,7 +68,7 @@ class CodableLambdaTest: XCTestCase {

let expected: Request

func handle(event: Request, context: Lambda.Context) -> EventLoopFuture<Response> {
func handle(_ event: Request, context: Lambda.Context) -> EventLoopFuture<Response> {
XCTAssertEqual(event, self.expected)
return context.eventLoop.makeSucceededFuture(Response(requestId: event.requestId))
}
Expand All @@ -77,7 +77,7 @@ class CodableLambdaTest: XCTestCase {
let handler = Handler(expected: request)

XCTAssertNoThrow(inputBuffer = try JSONEncoder().encode(request, using: self.allocator))
XCTAssertNoThrow(outputBuffer = try handler.handle(event: XCTUnwrap(inputBuffer), context: self.newContext()).wait())
XCTAssertNoThrow(outputBuffer = try handler.handle(XCTUnwrap(inputBuffer), context: self.newContext()).wait())
XCTAssertNoThrow(response = try JSONDecoder().decode(Response.self, from: XCTUnwrap(outputBuffer)))
XCTAssertEqual(response?.requestId, request.requestId)
}
Expand All @@ -93,7 +93,7 @@ class CodableLambdaTest: XCTestCase {

init(context: Lambda.InitializationContext) async throws {}

func handle(event: Request, context: Lambda.Context) async throws {
func handle(_ event: Request, context: Lambda.Context) async throws {
XCTAssertEqual(event, self.expected)
}
}
Expand All @@ -107,7 +107,7 @@ class CodableLambdaTest: XCTestCase {
handler.expected = request

XCTAssertNoThrow(inputBuffer = try JSONEncoder().encode(request, using: self.allocator))
XCTAssertNoThrow(outputBuffer = try handler.handle(event: XCTUnwrap(inputBuffer), context: self.newContext()).wait())
XCTAssertNoThrow(outputBuffer = try handler.handle(XCTUnwrap(inputBuffer), context: self.newContext()).wait())
XCTAssertNil(outputBuffer)
}
}
Expand All @@ -122,7 +122,7 @@ class CodableLambdaTest: XCTestCase {

init(context: Lambda.InitializationContext) async throws {}

func handle(event: Request, context: Lambda.Context) async throws -> Response {
func handle(_ event: Request, context: Lambda.Context) async throws -> Response {
XCTAssertEqual(event, self.expected)
return Response(requestId: event.requestId)
}
Expand All @@ -138,7 +138,7 @@ class CodableLambdaTest: XCTestCase {
handler.expected = request

XCTAssertNoThrow(inputBuffer = try JSONEncoder().encode(request, using: self.allocator))
XCTAssertNoThrow(outputBuffer = try handler.handle(event: XCTUnwrap(inputBuffer), context: self.newContext()).wait())
XCTAssertNoThrow(outputBuffer = try handler.handle(XCTUnwrap(inputBuffer), context: self.newContext()).wait())
XCTAssertNoThrow(response = try JSONDecoder().decode(Response.self, from: XCTUnwrap(outputBuffer)))
XCTAssertEqual(response?.requestId, request.requestId)
}
Expand Down
8 changes: 4 additions & 4 deletions Tests/AWSLambdaTestingTests/Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class LambdaTestingTests: XCTestCase {

init(context: Lambda.InitializationContext) {}

func handle(event: Request, context: Lambda.Context) async throws -> Response {
func handle(_ event: Request, context: Lambda.Context) async throws -> Response {
Response(message: "echo" + event.name)
}
}
Expand All @@ -59,7 +59,7 @@ class LambdaTestingTests: XCTestCase {

init(context: Lambda.InitializationContext) {}

func handle(event: Request, context: Lambda.Context) async throws {
func handle(_ event: Request, context: Lambda.Context) async throws {
LambdaTestingTests.VoidLambdaHandlerInvokeCount += 1
}
}
Expand All @@ -79,7 +79,7 @@ class LambdaTestingTests: XCTestCase {

init(context: Lambda.InitializationContext) {}

func handle(event: String, context: Lambda.Context) async throws {
func handle(_ event: String, context: Lambda.Context) async throws {
throw MyError()
}
}
Expand All @@ -96,7 +96,7 @@ class LambdaTestingTests: XCTestCase {

init(context: Lambda.InitializationContext) {}

func handle(event: String, context: Lambda.Context) async throws -> String {
func handle(_ event: String, context: Lambda.Context) async throws -> String {
try await Task.sleep(nanoseconds: 500 * 1000 * 1000)
return event
}
Expand Down