Skip to content

Commit b93a14c

Browse files
committed
LambdaRuntime and closure initializer extensions
1 parent 7a8c0f2 commit b93a14c

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed

Sources/AWSLambdaRuntime/Lambda+Codable.swift

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,117 @@ extension LambdaCodableAdapter {
184184
)
185185
}
186186
}
187+
188+
extension NewLambdaRuntime {
189+
/// Initialize an instance with a ``StreamingLambdaHandler`` in the form of a closure.
190+
/// - Parameter body: The handler in the form of a closure.
191+
package convenience init(
192+
body: @Sendable @escaping (ByteBuffer, LambdaResponseStreamWriter, NewLambdaContext) async throws -> Void
193+
) where Handler == StreamingClosureHandler {
194+
self.init(handler: StreamingClosureHandler(body: body))
195+
}
196+
197+
/// Initialize an instance with a ``NewLambdaHandler`` defined in the form of a closure **with a non-`Void` return type**, an encoder, and a decoder.
198+
/// - Parameter body: The handler in the form of a closure.
199+
/// - Parameter encoder: The encoder object that will be used to encode the generic ``Output`` into a ``ByteBuffer``.
200+
/// - Parameter decoder: The decoder object that will be used to decode the incoming ``ByteBuffer`` event into the generic ``Event`` type.
201+
package convenience init<
202+
Event: Decodable,
203+
Output: Encodable,
204+
Encoder: LambdaOutputEncoder,
205+
Decoder: LambdaEventDecoder
206+
>(
207+
encoder: Encoder,
208+
decoder: Decoder,
209+
body: @escaping (Event, NewLambdaContext) async throws -> Output
210+
)
211+
where
212+
Handler == LambdaCodableAdapter<
213+
LambdaHandlerAdapter<Event, Output, ClosureHandler<Event, Output>>,
214+
Event,
215+
Output,
216+
Decoder,
217+
Encoder
218+
>
219+
{
220+
let handler = LambdaCodableAdapter(
221+
encoder: encoder,
222+
decoder: decoder,
223+
handler: LambdaHandlerAdapter(handler: ClosureHandler(body: body))
224+
)
225+
226+
self.init(handler: handler)
227+
}
228+
229+
/// Initialize an instance with a ``NewLambdaHandler`` defined in the form of a closure **with a `Void` return type**, an encoder, and a decoder.
230+
/// - Parameter body: The handler in the form of a closure.
231+
/// - Parameter encoder: The encoder object that will be used to encode the generic ``Output`` into a ``ByteBuffer``.
232+
/// - Parameter decoder: The decoder object that will be used to decode the incoming ``ByteBuffer`` event into the generic ``Event`` type.
233+
package convenience init<Event: Decodable, Decoder: LambdaEventDecoder>(
234+
decoder: Decoder,
235+
body: @escaping (Event, NewLambdaContext) async throws -> Void
236+
)
237+
where
238+
Handler == LambdaCodableAdapter<
239+
LambdaHandlerAdapter<Event, Void, ClosureHandler<Event, Void>>,
240+
Event,
241+
Void,
242+
Decoder,
243+
VoidEncoder
244+
>
245+
{
246+
let handler = LambdaCodableAdapter(
247+
decoder: decoder,
248+
handler: LambdaHandlerAdapter(handler: ClosureHandler(body: body))
249+
)
250+
251+
self.init(handler: handler)
252+
}
253+
254+
/// Initialize an instance with a ``NewLambdaHandler`` defined in the form of a closure **with a non-`Void` return type**.
255+
/// - note: ``JSONEncoder`` and ``JSONDecoder`` are used as the encoder and decoder objects. Use ``init(encoder:decoder:body:)`` to specify custom encoder and decoder objects.
256+
/// - Parameter body: The handler in the form of a closure.
257+
package convenience init<Event: Decodable, Output>(
258+
body: @escaping (Event, NewLambdaContext) async throws -> Output
259+
)
260+
where
261+
Handler == LambdaCodableAdapter<
262+
LambdaHandlerAdapter<Event, Output, ClosureHandler<Event, Output>>,
263+
Event,
264+
Output,
265+
JSONDecoder,
266+
LambdaJSONOutputEncoder<Output>
267+
>
268+
{
269+
let handler = LambdaCodableAdapter(
270+
encoder: JSONEncoder(),
271+
decoder: JSONDecoder(),
272+
handler: LambdaHandlerAdapter(handler: ClosureHandler(body: body))
273+
)
274+
275+
self.init(handler: handler)
276+
}
277+
278+
/// Initialize an instance with a ``NewLambdaHandler`` defined in the form of a closure **with a `Void` return type**.
279+
/// - note: ``JSONDecoder`` is used as the decoder object. Use ``init(decoder:body:)`` to specify a custom decoder object.
280+
/// - Parameter body: The handler in the form of a closure.
281+
package convenience init<Event: Decodable>(
282+
body: @escaping (Event, NewLambdaContext) async throws -> Void
283+
)
284+
where
285+
Handler == LambdaCodableAdapter<
286+
LambdaHandlerAdapter<Event, Void, ClosureHandler<Event, Void>>,
287+
Event,
288+
Void,
289+
JSONDecoder,
290+
VoidEncoder
291+
>
292+
{
293+
let handler = LambdaCodableAdapter(
294+
decoder: JSONDecoder(),
295+
handler: LambdaHandlerAdapter(handler: ClosureHandler(body: body))
296+
)
297+
298+
self.init(handler: handler)
299+
}
300+
}

Sources/AWSLambdaRuntimeCore/NewLambda.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import Dispatch
1616
import Logging
17+
import NIOCore
1718

1819
extension Lambda {
1920
package static func runLoop<RuntimeClient: LambdaRuntimeClientProtocol, Handler>(
@@ -44,4 +45,11 @@ extension Lambda {
4445
}
4546
}
4647
}
48+
49+
/// The default EventLoop the Lambda is scheduled on.
50+
package static var defaultEventLoop: any EventLoop {
51+
get {
52+
NIOSingletons.posixEventLoopGroup.next()
53+
}
54+
}
4755
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftAWSLambdaRuntime open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the SwiftAWSLambdaRuntime project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import Foundation
16+
//import ServiceLifecycle
17+
import Logging
18+
import NIOCore
19+
import Synchronization
20+
21+
package final class NewLambdaRuntime<Handler>: Sendable where Handler: StreamingLambdaHandler {
22+
let handlerMutex: Mutex<Handler>
23+
let logger: Logger
24+
let eventLoop: EventLoop
25+
26+
package init(
27+
handler: sending Handler,
28+
eventLoop: EventLoop = Lambda.defaultEventLoop,
29+
logger: Logger = Logger(label: "LambdaRuntime")
30+
) {
31+
self.handlerMutex = Mutex(handler)
32+
self.eventLoop = eventLoop
33+
self.logger = logger
34+
}
35+
36+
package func run() async throws {
37+
}
38+
}

0 commit comments

Comments
 (0)