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

Reusable JSONCoders #50

merged 2 commits into from
Mar 27, 2020

Conversation

fabianfett
Copy link
Member

@fabianfett fabianfett commented Mar 26, 2020

Motivation

This shall fix #36. JSONCoders should be reused during multiple invocations, thus improving performance.

Changes

  • Introduced protocols (LambdaCodableDecoder and LambdaCodableEncoder)
  • JSONEncoder conforms to LambdaCodableEncoder
  • JSONDecoder conforms to LambdaCodableDecoder
  • Added properties encoder and decoder on conditional conformances (In == Decodable and Out == Encodable) of EventLoopLambdaHandler
  • Added private static defaultJSONEncoder and defaultJSONDecoder on Lambda to be used as default implementations.

@fabianfett fabianfett requested a review from tomerd March 26, 2020 20:08
}
}

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

@fabianfett
Copy link
Member Author

fabianfett commented Mar 27, 2020

@tomerd Your change looks really good to me. We can merge this!

@tomerd tomerd merged commit 6f71032 into master Mar 27, 2020
@tomerd tomerd deleted the reuse-jsoncoders branch March 27, 2020 16:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

improve JSON performance
2 participants