-
Notifications
You must be signed in to change notification settings - Fork 113
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
Conversation
b611090
to
af9ea5a
Compare
} | ||
} | ||
|
||
private extension Lambda { | ||
/// the default json encoder used in `EventLoopLambdaHandler` if Out == Encodable | ||
static let defaultJSONEncoder = JSONEncoder() |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
2e03e11
to
316f138
Compare
@tomerd Your change looks really good to me. We can merge this! |
Motivation
This shall fix #36. JSONCoders should be reused during multiple invocations, thus improving performance.
Changes
LambdaCodableDecoder
andLambdaCodableEncoder
)JSONEncoder
conforms toLambdaCodableEncoder
JSONDecoder
conforms toLambdaCodableDecoder
encoder
anddecoder
on conditional conformances (In == Decodable and Out == Encodable) ofEventLoopLambdaHandler
defaultJSONEncoder
anddefaultJSONDecoder
on Lambda to be used as default implementations.