Skip to content

LambdaCodableEncoder should be less Foundation biased #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 27, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions Sources/AWSLambdaRuntime/Lambda+Codable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,7 @@ public extension EventLoopLambdaHandler where In: Decodable {
/// Implementation of `Out` to `ByteBuffer` encoding
public extension EventLoopLambdaHandler where Out: Encodable {
func encode(allocator: ByteBufferAllocator, value: Out) throws -> ByteBuffer? {
// nio will resize the buffer if necessary
var buffer = allocator.buffer(capacity: 1024)
try self.encoder.encode(value, into: &buffer)
return buffer
try self.encoder.encode(value, using: allocator)
}
}

Expand All @@ -120,7 +117,7 @@ public protocol LambdaCodableDecoder {
}

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

private extension Lambda {
Expand All @@ -129,4 +126,11 @@ private extension Lambda {
}

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