Skip to content

Commit 2297929

Browse files
authored
LambdaCodableEncoder should be less Foundation biased (#66)
1 parent bfad186 commit 2297929

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

Sources/AWSLambdaRuntime/Lambda+Codable.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,7 @@ public extension EventLoopLambdaHandler where In: Decodable {
9292
/// Implementation of `Out` to `ByteBuffer` encoding
9393
public extension EventLoopLambdaHandler where Out: Encodable {
9494
func encode(allocator: ByteBufferAllocator, value: Out) throws -> ByteBuffer? {
95-
// nio will resize the buffer if necessary
96-
var buffer = allocator.buffer(capacity: 1024)
97-
try self.encoder.encode(value, into: &buffer)
98-
return buffer
95+
try self.encoder.encode(value, using: allocator)
9996
}
10097
}
10198

@@ -120,7 +117,7 @@ public protocol LambdaCodableDecoder {
120117
}
121118

122119
public protocol LambdaCodableEncoder {
123-
func encode<T: Encodable>(_ value: T, into buffer: inout ByteBuffer) throws
120+
func encode<T: Encodable>(_ value: T, using allocator: ByteBufferAllocator) throws -> ByteBuffer
124121
}
125122

126123
private extension Lambda {
@@ -129,4 +126,11 @@ private extension Lambda {
129126
}
130127

131128
extension JSONDecoder: LambdaCodableDecoder {}
132-
extension JSONEncoder: LambdaCodableEncoder {}
129+
extension JSONEncoder: LambdaCodableEncoder {
130+
public func encode<T>(_ value: T, using allocator: ByteBufferAllocator) throws -> ByteBuffer where T: Encodable {
131+
// nio will resize the buffer if necessary
132+
var buffer = allocator.buffer(capacity: 1024)
133+
try self.encode(value, into: &buffer)
134+
return buffer
135+
}
136+
}

0 commit comments

Comments
 (0)