You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add initializer with LambdaHandler directly with Decodable event and Void output (#589)
_Add convenience initializer for `LambdaRuntime` to accept
`LambdaHandler` instances with `Void` output_
### Motivation:
Following PR #581, which added convenience initializers for
`LambdaRuntime` to accept `LambdaHandler` instances with `Encodable`
outputs, there was still a gap for handlers that return `Void`. This is
useful, for example, for AWS event sources like SQS, where the
`AWSLambdaEvents` package provides `SQSEvent` but there's no
corresponding output type - handlers simply process messages and return
`Void`.
Without this initializer, developers had to manually wrap their handlers
with `LambdaCodableAdapter` and `LambdaHandlerAdapter`, which was
verbose and inconsistent with the new API (similar to what is described
in #581)
### Modifications:
1. Added a new `init(decoder:handler:)` convenience initializer to
`LambdaCodableAdapter` in `Lambda+JSON.swift` that accepts a
`JSONDecoder` for handlers with `Void` output.
2. Added a new convenience initializer to `LambdaRuntime` that accepts a
`LambdaHandler` instance with `Void` output directly, handling the
wrapping internally.
3. Updated documentation comments to distinguish between handlers with
`Void` and `Encodable` outputs.
### Result:
Developers can now initialize `LambdaRuntime` with handlers that return
`Void` using a clean, direct API. This is especially useful for event
sources like SQS:
```swift
import AWSLambdaEvents
struct MySQSHandler: LambdaHandler {
func handle(_ event: SQSEvent, context: LambdaContext) async throws {
// Process SQS messages
}
}
let runtime = LambdaRuntime(lambdaHandler: MySQSHandler())
```
This provides API completeness, matching the convenience initializers
for handlers with `Encodable` outputs, and delivers better ergonomics
for common serverless patterns.
/// Initializes an instance given a decoder, and a handler with a `Void` output.
88
+
/// - Parameters:
89
+
/// - decoder: The decoder object that will be used to decode the received `ByteBuffer` event into the generic `Event` type served to the `handler`. By default, a JSONDecoder is used.
90
+
/// - handler: The handler object.
91
+
publicinit(
92
+
decoder:JSONDecoder=JSONDecoder(),
93
+
handler:sending Handler
94
+
)
95
+
where
96
+
Output ==Void,
97
+
Handler.Output ==Void,
98
+
Decoder ==LambdaJSONEventDecoder,
99
+
Encoder ==VoidEncoder
100
+
{
101
+
self.init(
102
+
decoder:LambdaJSONEventDecoder(decoder),
103
+
handler: handler
104
+
)
105
+
}
86
106
}
107
+
87
108
@available(LambdaSwift 2.0,*)
88
109
extensionLambdaResponseStreamWriter{
89
110
/// Writes the HTTP status code and headers to the response stream.
@@ -161,12 +182,41 @@ extension LambdaRuntime {
161
182
self.init(handler: handler, logger: logger)
162
183
}
163
184
164
-
/// Initialize an instance directly with a `LambdaHandler`.
185
+
/// Initialize an instance directly with a `LambdaHandler`, when `Event` is `Decodable` and `Output` is `Void`.
186
+
/// - Parameters:
187
+
/// - decoder: The decoder object that will be used to decode the incoming `ByteBuffer` event into the generic `Event` type. `JSONDecoder()` used as default.
188
+
/// - logger: The logger to use for the runtime. Defaults to a logger with label "LambdaRuntime".
189
+
/// - lambdaHandler: A type that conforms to the `LambdaHandler` protocol, whose `Event` is `Decodable` and `Output` is `Void`
/// Initialize an instance directly with a `LambdaHandler`, when `Event` is `Decodable` and `Output` is `Encodable`.
165
215
/// - Parameters:
166
216
/// - decoder: The decoder object that will be used to decode the incoming `ByteBuffer` event into the generic `Event` type. `JSONDecoder()` used as default.
167
217
/// - encoder: The encoder object that will be used to encode the generic `Output` into a `ByteBuffer`. `JSONEncoder()` used as default.
168
218
/// - logger: The logger to use for the runtime. Defaults to a logger with label "LambdaRuntime".
169
-
/// - lambdaHandler: A type that conforms to the `LambdaHandler` protocol, whose `Event` and `Output` types must be `Decodable`/`Encodable`
219
+
/// - lambdaHandler: A type that conforms to the `LambdaHandler` protocol, whose `Event` is `Decodable` and `Output` is `Encodable`
0 commit comments