Skip to content

Commit 30bfa4f

Browse files
committed
move the convenience initializers in a separate file
1 parent 01a4443 commit 30bfa4f

File tree

2 files changed

+72
-60
lines changed

2 files changed

+72
-60
lines changed

Sources/AWSLambdaRuntimeService/LambdaRuntime+ServiceLifeCycle.swift

+8-60
Original file line numberDiff line numberDiff line change
@@ -15,75 +15,23 @@
1515
@_exported import AWSLambdaRuntime
1616

1717
import ServiceLifecycle
18-
#if canImport(FoundationEssentials)
19-
import FoundationEssentials
20-
#else
21-
import struct Foundation.Data
22-
import class Foundation.JSONDecoder
23-
import class Foundation.JSONEncoder
24-
#endif
18+
2519

2620
///
2721
/// Encapsulate a LambdaRuntime+Codable to offer the same API but this time exposed as a Swift Service
28-
/// This allows to continue to avoid the Service payload for Lambda functions that doesn't need it
22+
/// This allows to avoid the Service extra payload for Lambda functions that doesn't need it
2923
///
3024
public class LambdaRuntimeService<Handler>: Service, @unchecked Sendable where Handler: StreamingLambdaHandler {
3125

32-
private let runtime: LambdaRuntime<Handler>
33-
34-
/// Initialize an instance with a `LambdaHandler` defined in the form of a closure **with a non-`Void` return type**.
35-
/// - Parameters:
36-
/// - decoder: The decoder object that will be used to decode the incoming `ByteBuffer` event into the generic `Event` type. `JSONDecoder()` used as default.
37-
/// - encoder: The encoder object that will be used to encode the generic `Output` into a `ByteBuffer`. `JSONEncoder()` used as default.
38-
/// - body: The handler in the form of a closure.
39-
public init<Event: Decodable, Output>(
40-
decoder: JSONDecoder = JSONDecoder(),
41-
encoder: JSONEncoder = JSONEncoder(),
42-
body: sending @escaping (Event, LambdaContext) async throws -> Output
43-
)
44-
where
45-
Handler == LambdaCodableAdapter<
46-
LambdaHandlerAdapter<Event, Output, ClosureHandler<Event, Output>>,
47-
Event,
48-
Output,
49-
LambdaJSONEventDecoder,
50-
LambdaJSONOutputEncoder<Output>
51-
>
52-
{
53-
let handler = LambdaCodableAdapter(
54-
encoder: encoder,
55-
decoder: decoder,
56-
handler: LambdaHandlerAdapter(handler: ClosureHandler(body: body))
57-
)
26+
let runtime: LambdaRuntime<Handler>
5827

59-
self.runtime = LambdaRuntime(handler: handler)
28+
public func run() async throws {
29+
try await cancelWhenGracefulShutdown {
30+
try await self.runtime.run()
31+
}
6032
}
6133

62-
/// Initialize an instance with a `LambdaHandler` defined in the form of a closure **with a `Void` return type**.
63-
/// - Parameter body: The handler in the form of a closure.
64-
/// - Parameter decoder: The decoder object that will be used to decode the incoming `ByteBuffer` event into the generic `Event` type. `JSONDecoder()` used as default.
65-
public init<Event: Decodable>(
66-
decoder: JSONDecoder = JSONDecoder(),
67-
body: sending @escaping (Event, LambdaContext) async throws -> Void
68-
)
69-
where
70-
Handler == LambdaCodableAdapter<
71-
LambdaHandlerAdapter<Event, Void, ClosureHandler<Event, Void>>,
72-
Event,
73-
Void,
74-
LambdaJSONEventDecoder,
75-
VoidEncoder
76-
>
77-
{
78-
let handler = LambdaCodableAdapter(
79-
decoder: LambdaJSONEventDecoder(decoder),
80-
handler: LambdaHandlerAdapter(handler: ClosureHandler(body: body))
81-
)
82-
34+
init(handler: sending Handler) {
8335
self.runtime = LambdaRuntime(handler: handler)
8436
}
85-
86-
public func run() async throws {
87-
try await self.runtime.run()
88-
}
8937
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import AWSLambdaRuntime
2+
3+
#if canImport(FoundationEssentials)
4+
import FoundationEssentials
5+
#else
6+
import struct Foundation.Data
7+
import class Foundation.JSONDecoder
8+
import class Foundation.JSONEncoder
9+
#endif
10+
11+
public extension LambdaRuntimeService {
12+
13+
/// Initialize an instance with a `LambdaHandler` defined in the form of a closure **with a non-`Void` return type**.
14+
/// - Parameters:
15+
/// - decoder: The decoder object that will be used to decode the incoming `ByteBuffer` event into the generic `Event` type. `JSONDecoder()` used as default.
16+
/// - encoder: The encoder object that will be used to encode the generic `Output` into a `ByteBuffer`. `JSONEncoder()` used as default.
17+
/// - body: The handler in the form of a closure.
18+
convenience init<Event: Decodable, Output>(
19+
decoder: JSONDecoder = JSONDecoder(),
20+
encoder: JSONEncoder = JSONEncoder(),
21+
body: sending @escaping (Event, LambdaContext) async throws -> Output
22+
)
23+
where
24+
Handler == LambdaCodableAdapter<
25+
LambdaHandlerAdapter<Event, Output, ClosureHandler<Event, Output>>,
26+
Event,
27+
Output,
28+
LambdaJSONEventDecoder,
29+
LambdaJSONOutputEncoder<Output>
30+
>
31+
{
32+
let handler = LambdaCodableAdapter(
33+
encoder: encoder,
34+
decoder: decoder,
35+
handler: LambdaHandlerAdapter(handler: ClosureHandler(body: body))
36+
)
37+
38+
self.init(handler: handler)
39+
}
40+
41+
/// Initialize an instance with a `LambdaHandler` defined in the form of a closure **with a `Void` return type**.
42+
/// - Parameter body: The handler in the form of a closure.
43+
/// - Parameter decoder: The decoder object that will be used to decode the incoming `ByteBuffer` event into the generic `Event` type. `JSONDecoder()` used as default.
44+
convenience init<Event: Decodable>(
45+
decoder: JSONDecoder = JSONDecoder(),
46+
body: sending @escaping (Event, LambdaContext) async throws -> Void
47+
)
48+
where
49+
Handler == LambdaCodableAdapter<
50+
LambdaHandlerAdapter<Event, Void, ClosureHandler<Event, Void>>,
51+
Event,
52+
Void,
53+
LambdaJSONEventDecoder,
54+
VoidEncoder
55+
>
56+
{
57+
let handler = LambdaCodableAdapter(
58+
decoder: LambdaJSONEventDecoder(decoder),
59+
handler: LambdaHandlerAdapter(handler: ClosureHandler(body: body))
60+
)
61+
62+
self.init(handler: handler)
63+
}
64+
}

0 commit comments

Comments
 (0)