Skip to content

Commit 461c18a

Browse files
authored
Add LambdaRuntime initializer with LambdaHandler directly with Codable support (#581)
_Add convenience initializer for `LambdaRuntime` to accept `LambdaHandler` instances directly_ ### Motivation: When using the Swift AWS Lambda Runtime with custom handler types that conform to `LambdaHandler`, developers previously had two options to initialize `LambdaRuntime`: 1. Manually wrap their handler with `LambdaCodableAdapter` and `LambdaHandlerAdapter`: ```swift let lambdaHandler = MyHandler() let handler = LambdaCodableAdapter( encoder: JSONEncoder(), decoder: JSONDecoder(), handler: LambdaHandlerAdapter(handler: lambdaHandler) ) let runtime = LambdaRuntime(handler: handler) ``` 2. Use a closure-based initializer that indirectly calls the handler: ```swift let lambdaHandler = MyHandler() let runtime = LambdaRuntime { event, context in try await lambdaHandler.handle(event, context: context) } ``` Both approaches are verbose and don't provide a clean, ergonomic API for the common case of initializing `LambdaRuntime` with a custom `LambdaHandler` instance. The closure approach also creates an unnecessary indirection layer, wrapping the handler in a `ClosureHandler` before adapting it, or using `LambdaCodableAdapter`. ### Modifications: Added a new convenience initializer to `LambdaRuntime` in `Lambda+JSON.swift` that accepts a `LambdaHandler` instance directly: ```swift public convenience init<Event: Decodable, Output, LHandler: LambdaHandler>( decoder: JSONDecoder = JSONDecoder(), encoder: JSONEncoder = JSONEncoder(), logger: Logger = Logger(label: "LambdaRuntime"), lambdaHandler: sending LHandler ) where Handler == LambdaCodableAdapter< LambdaHandlerAdapter<Event, Output, LHandler>, Event, Output, LambdaJSONEventDecoder, LambdaJSONOutputEncoder<Output> >, LHandler.Event == Event, LHandler.Output == Output ``` This initializer handles the wrapping of the `LambdaHandler` with the necessary adapters internally, matching the pattern already established for closure-based handlers. ### Result: Developers can now initialize `LambdaRuntime` with a `LambdaHandler` instance using a clean, direct API: ```swift let lambdaHandler = MyHandler() let runtime = LambdaRuntime(lambdaHandler: lambdaHandler) ``` This provides: - **Better ergonomics**: More intuitive and less verbose API - **Consistency**: Matches the pattern of accepting handlers directly, similar to how `StreamingLambdaHandler` can be used - **No extra indirection**: Avoids wrapping the handler in an unnecessary `ClosureHandler` or `LambdaCodableAdapter` - **Type safety**: Maintains full type inference for `Event` and `Output` types from the handler
1 parent eeb3549 commit 461c18a

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Sources/AWSLambdaRuntime/FoundationSupport/Lambda+JSON.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,5 +160,37 @@ extension LambdaRuntime {
160160

161161
self.init(handler: handler, logger: logger)
162162
}
163+
164+
/// Initialize an instance directly with a `LambdaHandler`.
165+
/// - Parameters:
166+
/// - decoder: The decoder object that will be used to decode the incoming `ByteBuffer` event into the generic `Event` type. `JSONDecoder()` used as default.
167+
/// - encoder: The encoder object that will be used to encode the generic `Output` into a `ByteBuffer`. `JSONEncoder()` used as default.
168+
/// - 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`
170+
public convenience init<Event: Decodable, Output, LHandler: LambdaHandler>(
171+
decoder: JSONDecoder = JSONDecoder(),
172+
encoder: JSONEncoder = JSONEncoder(),
173+
logger: Logger = Logger(label: "LambdaRuntime"),
174+
lambdaHandler: sending LHandler
175+
)
176+
where
177+
Handler == LambdaCodableAdapter<
178+
LambdaHandlerAdapter<Event, Output, LHandler>,
179+
Event,
180+
Output,
181+
LambdaJSONEventDecoder,
182+
LambdaJSONOutputEncoder<Output>
183+
>,
184+
LHandler.Event == Event,
185+
LHandler.Output == Output
186+
{
187+
let handler = LambdaCodableAdapter(
188+
encoder: encoder,
189+
decoder: decoder,
190+
handler: LambdaHandlerAdapter(handler: lambdaHandler)
191+
)
192+
193+
self.init(handler: handler, logger: logger)
194+
}
163195
}
164196
#endif // trait: FoundationJSONSupport

0 commit comments

Comments
 (0)