Skip to content

Commit 17ed17b

Browse files
committed
separate protocol for simpler lambda (no init)
1 parent 2ed058b commit 17ed17b

File tree

18 files changed

+81
-149
lines changed

18 files changed

+81
-149
lines changed

Examples/Benchmark/BenchmarkHandler.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ import NIOCore
2222

2323
@main
2424
struct BenchmarkHandler: EventLoopLambdaHandler {
25-
typealias Event = String
26-
typealias Output = String
27-
2825
static func makeHandler(context: LambdaInitializationContext) -> EventLoopFuture<Self> {
2926
context.eventLoop.makeSucceededFuture(BenchmarkHandler())
3027
}

Examples/Deployment/Sources/Benchmark/BenchmarkHandler.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ import NIOCore
2222

2323
@main
2424
struct BenchmarkHandler: EventLoopLambdaHandler {
25-
typealias Event = String
26-
typealias Output = String
27-
2825
static func makeHandler(context: LambdaInitializationContext) -> EventLoopFuture<Self> {
2926
context.eventLoop.makeSucceededFuture(BenchmarkHandler())
3027
}

Examples/Deployment/Sources/HelloWorld/HelloWorldHandler.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import AWSLambdaRuntime
1616

1717
// introductory example, the obligatory "hello, world!"
1818
@main
19-
struct HelloWorldHandler: LambdaHandler {
19+
struct HelloWorldHandler: SimpleLambdaHandler {
2020
func handle(_ event: String, context: LambdaContext) async throws -> String {
2121
"hello, world"
2222
}

Examples/Echo/Lambda.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import AWSLambdaRuntime
1717
// in this example we are receiving and responding with strings
1818

1919
@main
20-
struct MyLambda: LambdaHandler {
20+
struct MyLambda: SimpleLambdaHandler {
2121
func handle(_ input: String, context: LambdaContext) async throws -> String {
2222
// as an example, respond with the input's reversed
2323
String(input.reversed())

Examples/ErrorHandling/Lambda.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ import AWSLambdaRuntime
1717
// MARK: - Run Lambda
1818

1919
@main
20-
struct MyLambda: LambdaHandler {
21-
typealias Event = Request
22-
typealias Output = Response
23-
20+
struct MyLambda: SimpleLambdaHandler {
2421
func handle(_ request: Request, context: LambdaContext) async throws -> Response {
2522
// switch over the error type "requested" by the request, and trigger such error accordingly
2623
switch request.error {

Examples/Foundation/Lambda.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ import Logging
2525

2626
@main
2727
struct MyLambda: LambdaHandler {
28-
typealias Event = Request
29-
typealias Output = [Exchange]
30-
3128
let calculator: ExchangeRatesCalculator
3229

3330
init(context: LambdaInitializationContext) async throws {

Examples/JSON/Lambda.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ struct Response: Codable {
2626
// codables to model your request and response objects
2727

2828
@main
29-
struct MyLambda: LambdaHandler {
30-
typealias Event = Request
31-
typealias Output = Response
32-
29+
struct MyLambda: SimpleLambdaHandler {
3330
func handle(_ event: Request, context: LambdaContext) async throws -> Response {
3431
// as an example, respond with the input event's reversed body
3532
Response(body: String(event.body.reversed()))

Examples/LocalDebugging/MyLambda/Lambda.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ import Shared
1919
// a local server simulator which will allow local debugging
2020

2121
@main
22-
struct MyLambda: LambdaHandler {
23-
typealias Event = Request
24-
typealias Output = Response
25-
22+
struct MyLambda: SimpleLambdaHandler {
2623
func handle(_ request: Request, context: LambdaContext) async throws -> Response {
2724
// TODO: something useful
2825
Response(message: "Hello, \(request.name)!")

Examples/Testing/Sources/Lambda.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import AWSLambdaRuntime
1717
// in this example we are receiving and responding with strings
1818

1919
@main
20-
struct MyLambda: LambdaHandler {
20+
struct MyLambda: SimpleLambdaHandler {
2121
func handle(_ event: String, context: LambdaContext) async throws -> String {
2222
// as an example, respond with the event's reversed body
2323
String(event.reversed())

Sources/AWSLambdaRuntimeCore/Lambda+String.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import NIOCore
1919
extension LambdaHandler where Event == String {
2020
/// Implementation of a `ByteBuffer` to `String` decoding.
2121
@inlinable
22-
public func decode(buffer: ByteBuffer) throws -> String {
22+
public func decode(buffer: ByteBuffer) throws -> Event {
2323
guard let value = buffer.getString(at: buffer.readerIndex, length: buffer.readableBytes) else {
2424
throw CodecError.invalidString
2525
}
@@ -31,7 +31,7 @@ extension LambdaHandler where Event == String {
3131
extension LambdaHandler where Output == String {
3232
/// Implementation of `String` to `ByteBuffer` encoding.
3333
@inlinable
34-
public func encode(value: String, into buffer: inout ByteBuffer) throws {
34+
public func encode(value: Output, into buffer: inout ByteBuffer) throws {
3535
buffer.writeString(value)
3636
}
3737
}
@@ -41,7 +41,7 @@ extension LambdaHandler where Output == String {
4141
extension EventLoopLambdaHandler where Event == String {
4242
/// Implementation of `String` to `ByteBuffer` encoding.
4343
@inlinable
44-
public func decode(buffer: ByteBuffer) throws -> String {
44+
public func decode(buffer: ByteBuffer) throws -> Event {
4545
guard let value = buffer.getString(at: buffer.readerIndex, length: buffer.readableBytes) else {
4646
throw CodecError.invalidString
4747
}
@@ -52,7 +52,7 @@ extension EventLoopLambdaHandler where Event == String {
5252
extension EventLoopLambdaHandler where Output == String {
5353
/// Implementation of a `ByteBuffer` to `String` decoding.
5454
@inlinable
55-
public func encode(value: String, into buffer: inout ByteBuffer) throws {
55+
public func encode(value: Output, into buffer: inout ByteBuffer) throws {
5656
buffer.writeString(value)
5757
}
5858
}

Sources/AWSLambdaRuntimeCore/LambdaHandler.swift

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,31 @@
1515
import Dispatch
1616
import NIOCore
1717

18+
// MARK: - SimpleLambdaHandler
19+
20+
/// Strongly typed, processing protocol for a Lambda that takes a user defined
21+
/// ``LambdaHandler/Event`` and returns a user defined
22+
/// ``LambdaHandler/Output`` asynchronously.
23+
///
24+
/// - note: Most users should implement the ``LambdaHandler`` protocol instead
25+
/// which defines the Lambda initialization method.
26+
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
27+
public protocol SimpleLambdaHandler: LambdaHandler {
28+
init()
29+
}
30+
31+
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
32+
extension SimpleLambdaHandler {
33+
public init(context: LambdaInitializationContext) async throws {
34+
self.init()
35+
}
36+
}
37+
1838
// MARK: - LambdaHandler
1939

2040
/// Strongly typed, processing protocol for a Lambda that takes a user defined
21-
/// ``EventLoopLambdaHandler/Event`` and returns a user defined
22-
/// ``EventLoopLambdaHandler/Output`` asynchronously.
41+
/// ``LambdaHandler/Event`` and returns a user defined
42+
/// ``LambdaHandler/Output`` asynchronously.
2343
///
2444
/// - note: Most users should implement this protocol instead of the lower
2545
/// level protocols ``EventLoopLambdaHandler`` and
@@ -33,8 +53,12 @@ public protocol LambdaHandler {
3353
/// The lambda function's output. Can be `Void`.
3454
associatedtype Output
3555

36-
init()
37-
56+
/// The Lambda initialization method.
57+
/// Use this method to initialize resources that will be used in every request.
58+
///
59+
/// Examples for this can be HTTP or database clients.
60+
/// - parameters:
61+
/// - context: Runtime ``LambdaInitializationContext``.
3862
init(context: LambdaInitializationContext) async throws
3963

4064
/// The Lambda handling method.
@@ -66,23 +90,6 @@ public protocol LambdaHandler {
6690
func decode(buffer: ByteBuffer) throws -> Event
6791
}
6892

69-
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
70-
extension LambdaHandler {
71-
public init() {
72-
self.init()
73-
}
74-
75-
/// The Lambda initialization method.
76-
/// Use this method to initialize resources that will be used in every request.
77-
///
78-
/// Examples for this can be HTTP or database clients.
79-
/// - parameters:
80-
/// - context: Runtime ``LambdaInitializationContext``.
81-
public init(context: LambdaInitializationContext) async throws {
82-
self.init()
83-
}
84-
}
85-
8693
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
8794
final class CodableLambdaHandler<Underlying: LambdaHandler>: ByteBufferLambdaHandler {
8895
private let handler: Underlying
@@ -133,7 +140,7 @@ final class CodableLambdaHandler<Underlying: LambdaHandler>: ByteBufferLambdaHan
133140
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
134141
extension LambdaHandler where Output == Void {
135142
@inlinable
136-
public func encode(value: Void, into buffer: inout ByteBuffer) throws {}
143+
public func encode(value: Output, into buffer: inout ByteBuffer) throws {}
137144
}
138145

139146
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
@@ -173,10 +180,7 @@ internal struct UncheckedSendableHandler<Underlying: LambdaHandler, Event, Outpu
173180
// MARK: - EventLoopLambdaHandler
174181

175182
/// Strongly typed, `EventLoopFuture` based processing protocol for a Lambda that takes a user
176-
/// defined ``Event`` and returns a user defined ``Output`` asynchronously.
177-
///
178-
/// ``EventLoopLambdaHandler`` extends ``ByteBufferLambdaHandler``, performing
179-
/// `ByteBuffer` -> ``Event`` decoding and ``Output`` -> `ByteBuffer` encoding.
183+
/// defined ``EventLoopLambdaHandler/Event`` and returns a user defined ``EventLoopLambdaHandler/Output`` asynchronously.
180184
///
181185
/// - note: To implement a Lambda, implement either ``LambdaHandler`` or the
182186
/// ``EventLoopLambdaHandler`` protocol. The ``LambdaHandler`` will offload
@@ -235,7 +239,7 @@ public protocol EventLoopLambdaHandler {
235239
/// Implementation of `ByteBuffer` to `Void` decoding.
236240
extension EventLoopLambdaHandler where Output == Void {
237241
@inlinable
238-
public func encode(value: Void, into buffer: inout ByteBuffer) throws {}
242+
public func encode(value: Output, into buffer: inout ByteBuffer) throws {}
239243
}
240244

241245
internal final class CodableEventLoopLambdaHandler<Underlying: EventLoopLambdaHandler>: ByteBufferLambdaHandler {

0 commit comments

Comments
 (0)