@@ -41,8 +41,7 @@ extension UInt8 {
41
41
extension RedisDataDecoder {
42
42
enum _RedisDataDecodingState {
43
43
case notYetParsed
44
- #warning("parsed needs to be implemented to include RedisData!")
45
- case parsed
44
+ case parsed( RedisData )
46
45
}
47
46
48
47
func _parse( at position: inout Int , from buffer: inout ByteBuffer ) throws -> _RedisDataDecodingState {
@@ -53,18 +52,22 @@ extension RedisDataDecoder {
53
52
switch token {
54
53
case . plus:
55
54
guard let string = try _parseSimpleString ( at: & position, from: & buffer) else { return . notYetParsed }
56
- return . parsed
55
+ return . parsed( . basicString( string) )
56
+
57
57
case . colon:
58
58
guard let number = try _parseInteger ( at: & position, from: & buffer) else { return . notYetParsed }
59
- return . parsed
59
+ return . parsed( . integer( number) )
60
+
60
61
case . dollar:
61
62
return try _parseBulkString ( at: & position, from: & buffer)
63
+
62
64
case . asterisk:
63
65
return try _parseArray ( at: & position, from: & buffer)
66
+
64
67
case . hyphen:
65
68
guard let string = try _parseSimpleString ( at: & position, from: & buffer) else { return . notYetParsed }
66
- let error = RedisError ( identifier: " serverSide " , reason: string)
67
- return . parsed
69
+ return . parsed ( . error( RedisError ( identifier: " serverSide " , reason: string) ) )
70
+
68
71
default :
69
72
throw RedisError (
70
73
identifier: " invalidTokenType " ,
@@ -122,19 +125,17 @@ extension RedisDataDecoder {
122
125
func _parseBulkString( at position: inout Int , from buffer: inout ByteBuffer ) throws -> _RedisDataDecodingState {
123
126
guard let size = try _parseInteger ( at: & position, from: & buffer) else { return . notYetParsed }
124
127
125
- #warning("TODO: Return null data if null is sent from Redis")
126
128
// Redis sends '-1' to represent a null string
127
- guard size > - 1 else { return . parsed }
129
+ guard size > - 1 else { return . parsed( . null ) }
128
130
129
131
// Redis can hold empty bulk strings, and represents it with a 0 size
130
132
// so return an empty string
131
- #warning("TODO: Return an empty bulk string")
132
133
guard size > 0 else {
133
134
// Move the tip of the message position
134
135
// since size = 0, and we successfully parsed the size
135
136
// the beginning of the next message should be 2 further (the final \r\n - $0\r\n\r\n)
136
137
position += 2
137
- return . parsed
138
+ return . parsed( . bulkString ( " " . convertedToData ( ) ) )
138
139
}
139
140
140
141
// verify that we have at least our expected bulk string message
@@ -149,16 +150,16 @@ extension RedisDataDecoder {
149
150
// of the bulk string content
150
151
position += expectedRemainingMessageSize
151
152
152
- return . parsed // bulkString(Data(bytes[ ..<(size - 1) ]))
153
+ return . parsed(
154
+ . bulkString( Data ( bytes [ ..< size ] ) )
155
+ )
153
156
}
154
157
155
158
/// See https://redis.io/topics/protocol#resp-arrays
156
159
func _parseArray( at position: inout Int , from buffer: inout ByteBuffer ) throws -> _RedisDataDecodingState {
157
160
guard let arraySize = try _parseInteger ( at: & position, from: & buffer) else { return . notYetParsed }
158
- #warning("TODO: return null array")
159
- guard arraySize > - 1 else { return . parsed }
160
- #warning("TODO: return empty array")
161
- guard arraySize > 0 else { return . parsed }
161
+ guard arraySize > - 1 else { return . parsed( . null) }
162
+ guard arraySize > 0 else { return . parsed( . array( [ ] ) ) }
162
163
163
164
var array = [ _RedisDataDecodingState] ( repeating: . notYetParsed, count: arraySize)
164
165
for index in 0 ..< arraySize {
@@ -173,8 +174,16 @@ extension RedisDataDecoder {
173
174
}
174
175
}
175
176
176
- #warning("TODO: Mapping to data and return the array of values")
177
- return . parsed
177
+ let values = try array. map { state -> RedisData in
178
+ guard case . parsed( let value) = state else {
179
+ throw RedisError (
180
+ identifier: " parseArray " ,
181
+ reason: " Unexpected error while parsing Redis RESP. "
182
+ )
183
+ }
184
+ return value
185
+ }
186
+ return . parsed( . array( values) )
178
187
}
179
188
}
180
189
0 commit comments