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 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
Prev Previous commit
Next Next commit
Rename to NonFactory* and provide extension points.
  • Loading branch information
tachyonics committed Sep 27, 2023
commit 82ae9d7c02bbb6b0205119e52096090039c2b04c
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
6 changes: 3 additions & 3 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, handlerProvider: CodableSimpleLambdaHandler<Handler>.makeHandler)
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, handlerProvider: CodableLambdaHandler<Handler>.makeHandler)
Self.run(configuration: configuration, handlerProvider: Handler.makeCodableHandler)
}

/// Run a Lambda defined by implementing the ``EventLoopLambdaHandler`` protocol.
Expand Down Expand Up @@ -82,7 +82,7 @@ public enum Lambda {
/// - 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(),
handlerProvider: @escaping (LambdaInitializationContext) -> EventLoopFuture<some CoreByteBufferLambdaHandler>
handlerProvider: @escaping (LambdaInitializationContext) -> EventLoopFuture<some NonFactoryByteBufferLambdaHandler>
) -> Result<Int, Error> {
let _run = { (configuration: LambdaConfiguration) -> Result<Int, Error> in
Backtrace.install()
Expand Down
Loading