@@ -82,36 +82,51 @@ internal struct CodableVoidLambdaClosureWrapper<In: Decodable>: LambdaHandler {
82
82
}
83
83
}
84
84
85
- /// Implementation of a`ByteBuffer` to `In` and `Out` to `ByteBuffer` codec
86
- /// Using Foundation's JSONEncoder and JSONDecoder
87
- /// Advanced users that want to inject their own codec can do it by overriding these functions.
88
- public extension EventLoopLambdaHandler where In: Decodable , Out: Encodable {
85
+ /// Implementation of a`ByteBuffer` to `In` decoding
86
+ public extension EventLoopLambdaHandler where In: Decodable {
87
+ func decode( buffer: ByteBuffer ) throws -> In {
88
+ try self . decoder. decode ( In . self, from: buffer)
89
+ }
90
+ }
91
+
92
+ /// Implementation of `Out` to `ByteBuffer` encoding
93
+ public extension EventLoopLambdaHandler where Out: Encodable {
89
94
func encode( allocator: ByteBufferAllocator , value: Out ) throws -> ByteBuffer ? {
90
95
// nio will resize the buffer if necessary
91
96
var buffer = allocator. buffer ( capacity: 1024 )
92
- try Lambda . defaultJSONEncoder . encode ( value, into: & buffer)
97
+ try self . encoder . encode ( value, into: & buffer)
93
98
return buffer
94
99
}
100
+ }
95
101
96
- func decode( buffer: ByteBuffer ) throws -> In {
97
- try Lambda . defaultJSONDecoder. decode ( In . self, from: buffer)
102
+ /// Default `ByteBuffer` to `In` decoder using Foundation's JSONDecoder
103
+ /// Advanced users that want to inject their own codec can do it by overriding these functions.
104
+ public extension EventLoopLambdaHandler where In: Decodable {
105
+ var decoder : LambdaCodableDecoder {
106
+ Lambda . defaultJSONDecoder
98
107
}
99
108
}
100
109
101
- private extension Lambda {
102
- /// the default json encoder used in `EventLoopLambdaHandler` if Out == Encodable
103
- static let defaultJSONEncoder = JSONEncoder ( )
110
+ /// Default `Out` to `ByteBuffer` encoder using Foundation's JSONEncoder
111
+ /// Advanced users that want to inject their own codec can do it by overriding these functions.
112
+ public extension EventLoopLambdaHandler where Out: Encodable {
113
+ var encoder : LambdaCodableEncoder {
114
+ Lambda . defaultJSONEncoder
115
+ }
116
+ }
104
117
105
- /// the default json decoder used in `EventLoopLambdaHandler` if In == Decodable
106
- static let defaultJSONDecoder = JSONDecoder ( )
118
+ public protocol LambdaCodableDecoder {
119
+ func decode < T : Decodable > ( _ type : T . Type , from buffer : ByteBuffer ) throws -> T
107
120
}
108
121
109
- public extension EventLoopLambdaHandler where In: Decodable , Out == Void {
110
- func encode( allocator: ByteBufferAllocator , value: Void ) throws -> ByteBuffer ? {
111
- nil
112
- }
122
+ public protocol LambdaCodableEncoder {
123
+ func encode< T: Encodable > ( _ value: T , into buffer: inout ByteBuffer ) throws
124
+ }
113
125
114
- func decode ( buffer : ByteBuffer ) throws -> In {
115
- try Lambda . defaultJSONDecoder. decode ( In . self , from : buffer )
116
- }
126
+ private extension Lambda {
127
+ static let defaultJSONDecoder = JSONDecoder ( )
128
+ static let defaultJSONEncoder = JSONEncoder ( )
117
129
}
130
+
131
+ extension JSONDecoder : LambdaCodableDecoder { }
132
+ extension JSONEncoder : LambdaCodableEncoder { }
0 commit comments