Skip to content

Reusable JSONCoders #50

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
Mar 27, 2020
Merged
Changes from 1 commit
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
17 changes: 11 additions & 6 deletions Sources/AWSLambdaRuntime/Lambda+Codable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,25 +88,30 @@ internal struct CodableVoidLambdaClosureWrapper<In: Decodable>: LambdaHandler {
public extension EventLoopLambdaHandler where In: Decodable, Out: Encodable {
func encode(allocator: ByteBufferAllocator, value: Out) throws -> ByteBuffer? {
// nio will resize the buffer if necessary
// FIXME: reusable JSONEncoder and buffer
var buffer = allocator.buffer(capacity: 1024)
try JSONEncoder().encode(value, into: &buffer)
try Lambda.defaultJSONEncoder.encode(value, into: &buffer)
return buffer
}

func decode(buffer: ByteBuffer) throws -> In {
// FIXME: reusable JSONDecoder
try JSONDecoder().decode(In.self, from: buffer)
try Lambda.defaultJSONDecoder.decode(In.self, from: buffer)
}
}

private extension Lambda {
/// the default json encoder used in `EventLoopLambdaHandler` if Out == Encodable
static let defaultJSONEncoder = JSONEncoder()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one step further could be to introduce a protocol so one can inject a custom encoder/decoder

Copy link
Member Author

@fabianfett fabianfett Mar 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure but if we add them to EventLoopLambdaHandler we would add them to a protocol that doesn't NEED the coders in any way. See StringLambdaClosureWrapper as an example. I'm not sure if I would be a fan of adding this if it isn't needed a 100% of the time.

@tomerd wdyt?

Copy link
Contributor

@tomerd tomerd Mar 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wdyt about:

/// Implementation of  a`ByteBuffer` to `In` decoding
public extension EventLoopLambdaHandler where In: Decodable{
    func decode(buffer: ByteBuffer) throws -> In {
        try self.decoder.decode(In.self, from: buffer)
    }
}

/// 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
    }
}

/// Implementation of  `Out` to `Void` encoding
public extension EventLoopLambdaHandler where Out == Void {
    func encode(allocator: ByteBufferAllocator, value: Void) throws -> ByteBuffer? {
        nil
    }
}

/// Default `ByteBuffer` to `In` decoder using Foundation's JSONDecoder
/// Advanced users that want to inject their own codec can do it by overriding these functions.
public extension EventLoopLambdaHandler where In: Decodable {
    var decoder: LambdaDecoder {
        Lambda.defaultJSONDecoder
    }
}

/// Default `Out` to `ByteBuffer` encoder using Foundation's JSONEncoder
/// Advanced users that want to inject their own codec can do it by overriding these functions.
public extension EventLoopLambdaHandler where Out: Encodable {
    var encoder: LambdaEncoder {
        Lambda.defaultJSONEncoder
    }
}

public protocol LambdaDecoder {
    func decode<T: Decodable>(_ type: T.Type, from buffer: ByteBuffer) throws -> T
}

public protocol LambdaEncoder {
    func encode<T: Encodable>(_ value: T, into buffer: inout ByteBuffer) throws
}

private extension Lambda {
    static let defaultJSONDecoder = JSONDecoder()
    static let defaultJSONEncoder = JSONEncoder()
}

extension JSONDecoder: LambdaDecoder {}
extension JSONEncoder: LambdaEncoder {}

maybe LambdaCoddableDecoder/LambdaCodableEncoder instead of LambdaDecoder/LambdaEncoder

Copy link
Contributor

@tomerd tomerd Mar 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a suggested commit: 44e425c feel free to remove if you dont like the direction


/// the default json decoder used in `EventLoopLambdaHandler` if In == Decodable
static let defaultJSONDecoder = JSONDecoder()
}

public extension EventLoopLambdaHandler where In: Decodable, Out == Void {
func encode(allocator: ByteBufferAllocator, value: Void) throws -> ByteBuffer? {
nil
}

func decode(buffer: ByteBuffer) throws -> In {
// FIXME: reusable JSONDecoder
try JSONDecoder().decode(In.self, from: buffer)
try Lambda.defaultJSONDecoder.decode(In.self, from: buffer)
}
}