diff --git a/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift b/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift index 98a8a496..1476f02d 100644 --- a/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift +++ b/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift @@ -84,6 +84,31 @@ fileprivate struct UncheckedSendableHandler=5.5) && canImport(_Concurrency) +/// Strongly typed, processing protocol for a Lambda that takes a user defined +/// ``EventLoopLambdaHandler/Event`` and returns a user defined +/// ``EventLoopLambdaHandler/Output`` asynchronously. +/// +/// - note: Simpler use cases that do not special initialization should implement this protocol instead of the lower +/// level protocols ``LambdaHandler``, +/// ``EventLoopLambdaHandler`` and +/// ``ByteBufferLambdaHandler``. +@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) +public protocol SimpleLambdaHandler: LambdaHandler { + init() +} + +@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) +extension SimpleLambdaHandler { + init(context: LambdaInitializationContext) async throws { + // no-op initialization, which simple LambdaHandler that do not need initialization + self.init() + } +} +#endif + // MARK: - EventLoopLambdaHandler /// Strongly typed, `EventLoopFuture` based processing protocol for a Lambda that takes a user diff --git a/Tests/AWSLambdaRuntimeCoreTests/LambdaHandlerTest.swift b/Tests/AWSLambdaRuntimeCoreTests/LambdaHandlerTest.swift index 98b49ca7..5785b8a7 100644 --- a/Tests/AWSLambdaRuntimeCoreTests/LambdaHandlerTest.swift +++ b/Tests/AWSLambdaRuntimeCoreTests/LambdaHandlerTest.swift @@ -146,6 +146,27 @@ class LambdaHandlerTest: XCTestCase { let result = Lambda.run(configuration: configuration, handlerType: Handler.self) assertLambdaRuntimeResult(result, shoudHaveRun: maxTimes) } + + @available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) + func testSimpleLambdaHandler() { + let server = MockLambdaServer(behavior: Behavior()) + XCTAssertNoThrow(try server.start().wait()) + defer { XCTAssertNoThrow(try server.stop().wait()) } + + struct Handler: SimpleLambdaHandler { + typealias Event = String + typealias Output = String + + func handle(_ event: String, context: LambdaContext) async throws -> String { + event + } + } + + let maxTimes = Int.random(in: 1 ... 10) + let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes)) + let result = Lambda.run(configuration: configuration, handlerType: Handler.self) + assertLambdaRuntimeResult(result, shoudHaveRun: maxTimes) + } #endif // MARK: - EventLoopLambdaHandler