Skip to content

Commit 3bcc17a

Browse files
committed
Refactor
1 parent 6bf879c commit 3bcc17a

File tree

2 files changed

+34
-27
lines changed

2 files changed

+34
-27
lines changed

Sources/AWSLambdaRuntimeCore/NewLambda+JSON.swift

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,20 @@ package protocol LambdaEventDecoder {
2929
/// The protocol an encoder must conform to so that it can be used with ``LambdaCodableAdapter`` to encode the generic
3030
/// ``Output`` object into a ``ByteBuffer``.
3131
package protocol LambdaOutputEncoder {
32+
associatedtype Output
33+
3234
/// Encode the generic type `Output` the handler has returned into a ``ByteBuffer``.
3335
/// - Parameters:
3436
/// - value: The object to encode into a ``ByteBuffer``.
3537
/// - buffer: The ``ByteBuffer`` where the encoded value will be written to.
36-
func encode<Output: Encodable>(_ value: Output, into buffer: inout ByteBuffer) throws
38+
func encode(_ value: Output, into buffer: inout ByteBuffer) throws
3739
}
3840

3941
package struct VoidEncoder: LambdaOutputEncoder {
40-
package func encode<Output>(_ value: Output, into buffer: inout NIOCore.ByteBuffer) throws where Output: Encodable {
42+
package typealias Output = Void
4143

42-
}
44+
@inlinable
45+
package func encode(_ value: Void, into buffer: inout NIOCore.ByteBuffer) throws {}
4346
}
4447

4548
/// Adapts a ``NewLambdaHandler`` conforming handler to conform to ``LambdaWithBackgroundProcessingHandler``.
@@ -48,10 +51,11 @@ package struct LambdaHandlerAdapter<
4851
Output,
4952
Handler: NewLambdaHandler
5053
>: LambdaWithBackgroundProcessingHandler where Handler.Event == Event, Handler.Output == Output {
51-
let handler: Handler
54+
@usableFromInline let handler: Handler
5255

5356
/// Initializes an instance given a concrete handler.
5457
/// - Parameter handler: The ``NewLambdaHandler`` conforming handler that is to be adapted to ``LambdaWithBackgroundProcessingHandler``.
58+
@inlinable
5559
package init(handler: Handler) {
5660
self.handler = handler
5761
}
@@ -62,9 +66,10 @@ package struct LambdaHandlerAdapter<
6266
/// - event: The received event.
6367
/// - outputWriter: The writer to write the computed response to.
6468
/// - context: The ``NewLambdaContext`` containing the invocation's metadata.
69+
@inlinable
6570
package func handle(
6671
_ event: Event,
67-
outputWriter: consuming some LambdaResponseWriter<Output>,
72+
outputWriter: some LambdaResponseWriter<Output>,
6873
context: NewLambdaContext
6974
) async throws {
7075
let output = try await self.handler.handle(event, context: context)
@@ -79,17 +84,18 @@ package struct LambdaCodableAdapter<
7984
Output,
8085
Decoder: LambdaEventDecoder,
8186
Encoder: LambdaOutputEncoder
82-
>: StreamingLambdaHandler where Handler.Event == Event, Handler.Output == Output {
83-
let handler: Handler
84-
let encoder: Encoder
85-
let decoder: Decoder
86-
private var byteBuffer: ByteBuffer = .init()
87+
>: StreamingLambdaHandler where Handler.Event == Event, Handler.Output == Output, Encoder.Output == Output {
88+
@usableFromInline let handler: Handler
89+
@usableFromInline let encoder: Encoder
90+
@usableFromInline let decoder: Decoder
91+
/*private*/ @usableFromInline var byteBuffer: ByteBuffer = .init()
8792

8893
/// Initializes an instance given an encoder, decoder, and a handler with a non-`Void` output.
8994
/// - Parameters:
9095
/// - encoder: The encoder object that will be used to encode the generic ``Output`` obtained from the `handler`'s `outputWriter` into a ``ByteBuffer``.
9196
/// - decoder: The decoder object that will be used to decode the received ``ByteBuffer`` event into the generic ``Event`` type served to the `handler`.
9297
/// - handler: The handler object.
98+
@inlinable
9399
package init(encoder: Encoder, decoder: Decoder, handler: Handler) where Output: Encodable {
94100
self.encoder = encoder
95101
self.decoder = decoder
@@ -100,6 +106,7 @@ package struct LambdaCodableAdapter<
100106
/// - Parameters:
101107
/// - decoder: The decoder object that will be used to decode the received ``ByteBuffer`` event into the generic ``Event`` type served to the `handler`.
102108
/// - handler: The handler object.
109+
@inlinable
103110
package init(decoder: Decoder, handler: Handler) where Output == Void, Encoder == VoidEncoder {
104111
self.encoder = VoidEncoder()
105112
self.decoder = decoder
@@ -111,43 +118,43 @@ package struct LambdaCodableAdapter<
111118
/// - event: The received event.
112119
/// - outputWriter: The writer to write the computed response to.
113120
/// - context: The ``NewLambdaContext`` containing the invocation's metadata.
114-
package mutating func handle(
121+
@inlinable
122+
package mutating func handle<Writer: LambdaResponseStreamWriter>(
115123
_ request: ByteBuffer,
116-
responseWriter: some LambdaResponseStreamWriter,
124+
responseWriter: Writer,
117125
context: NewLambdaContext
118126
) async throws {
119127
let event = try self.decoder.decode(Event.self, from: request)
120128

121-
let writer = LambdaCodableResponseWriter<Output>(encoder: self.encoder, streamWriter: responseWriter)
129+
let writer = LambdaCodableResponseWriter<Output, Encoder, Writer>(
130+
encoder: self.encoder,
131+
streamWriter: responseWriter
132+
)
122133
try await self.handler.handle(event, outputWriter: writer, context: context)
123134
}
124135
}
125136

126137
/// A ``LambdaResponseStreamWriter`` wrapper that conforms to ``LambdaResponseWriter``.
127-
package struct LambdaCodableResponseWriter<Output>: LambdaResponseWriter {
128-
@usableFromInline let underlyingStreamWriter: LambdaResponseStreamWriter
129-
@usableFromInline let encoder: LambdaOutputEncoder
138+
package struct LambdaCodableResponseWriter<Output, Encoder: LambdaOutputEncoder, Base: LambdaResponseStreamWriter>:
139+
LambdaResponseWriter
140+
where Output == Encoder.Output {
141+
@usableFromInline let underlyingStreamWriter: Base
142+
@usableFromInline let encoder: Encoder
130143

131144
/// Initializes an instance given an encoder and an underlying ``LambdaResponseStreamWriter``.
132145
/// - Parameters:
133146
/// - encoder: The encoder object that will be used to encode the generic ``Output`` into a ``ByteBuffer``, which will then be passed to `streamWriter`.
134147
/// - streamWriter: The underlying ``LambdaResponseStreamWriter`` that will be wrapped.
135148
@inlinable
136-
package init(encoder: LambdaOutputEncoder, streamWriter: LambdaResponseStreamWriter) {
149+
package init(encoder: Encoder, streamWriter: Base) {
137150
self.encoder = encoder
138151
self.underlyingStreamWriter = streamWriter
139152
}
140153

141-
/// Passes the `output` argument to ``LambdaResponseStreamWriter/writeAndFinish(_:)``.
142-
/// - Parameter output: The generic ``Output`` object that will be passed to ``LambdaResponseStreamWriter/writeAndFinish(_:)``.
143154
@inlinable
144155
package func write(_ output: Output) async throws {
145-
if Output.self == Void.self {
146-
try await self.underlyingStreamWriter.finish()
147-
} else if let output = output as? Encodable {
148-
var outputBuffer = ByteBuffer()
149-
try self.encoder.encode(output, into: &outputBuffer)
150-
try await self.underlyingStreamWriter.writeAndFinish(outputBuffer)
151-
}
156+
var outputBuffer = ByteBuffer()
157+
try self.encoder.encode(output, into: &outputBuffer)
158+
try await self.underlyingStreamWriter.writeAndFinish(outputBuffer)
152159
}
153160
}

Sources/AWSLambdaRuntimeCore/NewLambdaHandlers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ package protocol LambdaWithBackgroundProcessingHandler {
109109
/// Used with ``LambdaWithBackgroundProcessingHandler``.
110110
/// A mechanism to "return" an output from ``LambdaWithBackgroundProcessingHandler/handle(_:outputWriter:context:)`` without the function needing to
111111
/// have a return type and exit at that point. This allows for background work to be executed _after_ a response has been sent to the AWS Lambda response endpoint.
112-
package protocol LambdaResponseWriter<Output>: ~Copyable {
112+
package protocol LambdaResponseWriter<Output> {
113113
associatedtype Output
114114
/// Sends the generic ``Output`` object (representing the computed result of the handler)
115115
/// to the AWS Lambda response endpoint.

0 commit comments

Comments
 (0)