Skip to content

add new abstraction for simple Lambda that does not need explicit initialization #259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 25 additions & 0 deletions Sources/AWSLambdaRuntimeCore/LambdaHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,31 @@ fileprivate struct UncheckedSendableHandler<Underlying: LambdaHandler, Event, Ou
}
#endif

// MARK: - SimpleLambdaHandler

#if compiler(>=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
Expand Down
21 changes: 21 additions & 0 deletions Tests/AWSLambdaRuntimeCoreTests/LambdaHandlerTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down