@@ -82,31 +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
- // FIXME: reusable JSONEncoder and buffer
92
96
var buffer = allocator. buffer ( capacity: 1024 )
93
- try JSONEncoder ( ) . encode ( value, into: & buffer)
97
+ try self . encoder . encode ( value, into: & buffer)
94
98
return buffer
95
99
}
100
+ }
96
101
97
- func decode( buffer: ByteBuffer ) throws -> In {
98
- // FIXME: reusable JSONDecoder
99
- try JSONDecoder ( ) . 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
100
107
}
101
108
}
102
109
103
- public extension EventLoopLambdaHandler where In: Decodable , Out == Void {
104
- func encode( allocator: ByteBufferAllocator , value: Void ) throws -> ByteBuffer ? {
105
- nil
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
106
115
}
116
+ }
107
117
108
- func decode( buffer: ByteBuffer ) throws -> In {
109
- // FIXME: reusable JSONDecoder
110
- try JSONDecoder ( ) . decode ( In . self, from: buffer)
111
- }
118
+ public protocol LambdaCodableDecoder {
119
+ func decode< T: Decodable > ( _ type: T . Type , from buffer: ByteBuffer ) throws -> T
120
+ }
121
+
122
+ public protocol LambdaCodableEncoder {
123
+ func encode< T: Encodable > ( _ value: T , into buffer: inout ByteBuffer ) throws
124
+ }
125
+
126
+ private extension Lambda {
127
+ static let defaultJSONDecoder = JSONDecoder ( )
128
+ static let defaultJSONEncoder = JSONEncoder ( )
112
129
}
130
+
131
+ extension JSONDecoder : LambdaCodableDecoder { }
132
+ extension JSONEncoder : LambdaCodableEncoder { }
0 commit comments