Skip to content

Allow custom initialisation of the HandlerType of the LambdaRuntime. #308

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 3 commits 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
44 changes: 5 additions & 39 deletions Sources/AWSLambdaRuntime/Lambda+Codable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ import class Foundation.JSONEncoder
import NIOCore
import NIOFoundationCompat

// MARK: - SimpleLambdaHandler Codable support
// MARK: - NonFactoryLambdaHandler Codable support

/// Implementation of `ByteBuffer` to `Event` decoding.
extension SimpleLambdaHandler where Event: Decodable {
extension NonFactoryLambdaHandler where Event: Decodable {
@inlinable
public func decode(buffer: ByteBuffer) throws -> Event {
try self.decoder.decode(Event.self, from: buffer)
}
}

/// Implementation of `Output` to `ByteBuffer` encoding.
extension SimpleLambdaHandler where Output: Encodable {
extension NonFactoryLambdaHandler where Output: Encodable {
@inlinable
public func encode(value: Output, into buffer: inout ByteBuffer) throws {
try self.encoder.encode(value, into: &buffer)
Expand All @@ -39,49 +39,15 @@ extension SimpleLambdaHandler where Output: Encodable {

/// Default `ByteBuffer` to `Event` decoder using Foundation's `JSONDecoder`.
/// Advanced users who want to inject their own codec can do it by overriding these functions.
extension SimpleLambdaHandler where Event: Decodable {
extension NonFactoryLambdaHandler where Event: Decodable {
public var decoder: LambdaCodableDecoder {
Lambda.defaultJSONDecoder
}
}

/// Default `Output` to `ByteBuffer` encoder using Foundation's `JSONEncoder`.
/// Advanced users who want to inject their own codec can do it by overriding these functions.
extension SimpleLambdaHandler where Output: Encodable {
public var encoder: LambdaCodableEncoder {
Lambda.defaultJSONEncoder
}
}

// MARK: - LambdaHandler Codable support

/// Implementation of `ByteBuffer` to `Event` decoding.
extension LambdaHandler where Event: Decodable {
@inlinable
public func decode(buffer: ByteBuffer) throws -> Event {
try self.decoder.decode(Event.self, from: buffer)
}
}

/// Implementation of `Output` to `ByteBuffer` encoding.
extension LambdaHandler where Output: Encodable {
@inlinable
public func encode(value: Output, into buffer: inout ByteBuffer) throws {
try self.encoder.encode(value, into: &buffer)
}
}

/// Default `ByteBuffer` to `Event` decoder using Foundation's `JSONDecoder`.
/// Advanced users who want to inject their own codec can do it by overriding these functions.
extension LambdaHandler where Event: Decodable {
public var decoder: LambdaCodableDecoder {
Lambda.defaultJSONDecoder
}
}

/// Default `Output` to `ByteBuffer` encoder using Foundation's `JSONEncoder`.
/// Advanced users who want to inject their own codec can do it by overriding these functions.
extension LambdaHandler where Output: Encodable {
extension NonFactoryLambdaHandler where Output: Encodable {
public var encoder: LambdaCodableEncoder {
Lambda.defaultJSONEncoder
}
Expand Down
27 changes: 3 additions & 24 deletions Sources/AWSLambdaRuntimeCore/Lambda+String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
//===----------------------------------------------------------------------===//
import NIOCore

// MARK: - SimpleLambdaHandler String support
// MARK: - NonFactoryLambdaHandler String support

extension SimpleLambdaHandler where Event == String {
extension NonFactoryLambdaHandler where Event == String {
/// Implementation of a `ByteBuffer` to `String` decoding.
@inlinable
public func decode(buffer: ByteBuffer) throws -> Event {
Expand All @@ -26,28 +26,7 @@ extension SimpleLambdaHandler where Event == String {
}
}

extension SimpleLambdaHandler where Output == String {
/// Implementation of `String` to `ByteBuffer` encoding.
@inlinable
public func encode(value: Output, into buffer: inout ByteBuffer) throws {
buffer.writeString(value)
}
}

// MARK: - LambdaHandler String support

extension LambdaHandler where Event == String {
/// Implementation of a `ByteBuffer` to `String` decoding.
@inlinable
public func decode(buffer: ByteBuffer) throws -> Event {
guard let value = buffer.getString(at: buffer.readerIndex, length: buffer.readableBytes) else {
throw CodecError.invalidString
}
return value
}
}

extension LambdaHandler where Output == String {
extension NonFactoryLambdaHandler where Output == String {
/// Implementation of `String` to `ByteBuffer` encoding.
@inlinable
public func encode(value: Output, into buffer: inout ByteBuffer) throws {
Expand Down
13 changes: 7 additions & 6 deletions Sources/AWSLambdaRuntimeCore/Lambda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public enum Lambda {
configuration: LambdaConfiguration = .init(),
handlerType: Handler.Type
) -> Result<Int, Error> {
Self.run(configuration: configuration, handlerType: CodableSimpleLambdaHandler<Handler>.self)
Self.run(configuration: configuration, handlerProvider: Handler.makeCodableHandler)
}

/// Run a Lambda defined by implementing the ``LambdaHandler`` protocol.
Expand All @@ -52,7 +52,7 @@ public enum Lambda {
configuration: LambdaConfiguration = .init(),
handlerType: Handler.Type
) -> Result<Int, Error> {
Self.run(configuration: configuration, handlerType: CodableLambdaHandler<Handler>.self)
Self.run(configuration: configuration, handlerProvider: Handler.makeCodableHandler)
}

/// Run a Lambda defined by implementing the ``EventLoopLambdaHandler`` protocol.
Expand All @@ -68,7 +68,7 @@ public enum Lambda {
configuration: LambdaConfiguration = .init(),
handlerType: Handler.Type
) -> Result<Int, Error> {
Self.run(configuration: configuration, handlerType: CodableEventLoopLambdaHandler<Handler>.self)
Self.run(configuration: configuration, handlerProvider: CodableEventLoopLambdaHandler<Handler>.makeHandler)
}

/// Run a Lambda defined by implementing the ``ByteBufferLambdaHandler`` protocol.
Expand All @@ -77,12 +77,12 @@ public enum Lambda {
///
/// - parameters:
/// - configuration: A Lambda runtime configuration object
/// - handlerType: The Handler to create and invoke.
/// - handlerProvider: Provides the Handler to invoke.
///
/// - note: This is a blocking operation that will run forever, as its lifecycle is managed by the AWS Lambda Runtime Engine.
internal static func run(
configuration: LambdaConfiguration = .init(),
handlerType: (some ByteBufferLambdaHandler).Type
handlerProvider: @escaping (LambdaInitializationContext) -> EventLoopFuture<some NonFactoryByteBufferLambdaHandler>
) -> Result<Int, Error> {
let _run = { (configuration: LambdaConfiguration) -> Result<Int, Error> in
Backtrace.install()
Expand All @@ -91,7 +91,8 @@ public enum Lambda {

var result: Result<Int, Error>!
MultiThreadedEventLoopGroup.withCurrentThreadAsEventLoop { eventLoop in
let runtime = LambdaRuntime(handlerType: handlerType, eventLoop: eventLoop, logger: logger, configuration: configuration)
let runtime = LambdaRuntime(handlerProvider: handlerProvider, eventLoop: eventLoop,
logger: logger, configuration: configuration)
#if DEBUG
let signalSource = trap(signal: configuration.lifecycle.stopSignal) { signal in
logger.info("intercepted signal: \(signal)")
Expand Down
Loading