@@ -19,21 +19,16 @@ extension RedisCommandExecutor {
19
19
/// - Parameter item: The element to look in the set for, stored as a `bulkString`.
20
20
public func sismember( _ key: String , item: RESPValueConvertible ) -> EventLoopFuture < Bool > {
21
21
return send ( command: " SISMEMBER " , with: [ key, item] )
22
- . flatMapThrowing {
23
- guard let result = $0. int else { throw RedisError . respConversion ( to: Int . self) }
24
- return result == 1
25
- }
22
+ . mapFromRESP ( to: Int . self)
23
+ . map { return $0 == 1 }
26
24
}
27
25
28
26
/// Returns the total count of elements in the set stored at key.
29
27
///
30
28
/// [https://redis.io/commands/scard](https://redis.io/commands/scard)
31
29
public func scard( _ key: String ) -> EventLoopFuture < Int > {
32
30
return send ( command: " SCARD " , with: [ key] )
33
- . flatMapThrowing {
34
- guard let count = $0. int else { throw RedisError . respConversion ( to: Int . self) }
35
- return count
36
- }
31
+ . mapFromRESP ( )
37
32
}
38
33
39
34
/// Adds the provided items to the set stored at key, returning the count of items added.
@@ -44,10 +39,7 @@ extension RedisCommandExecutor {
44
39
assert ( items. count > 0 , " There must be at least 1 item to add. " )
45
40
46
41
return send ( command: " SADD " , with: [ key] + items)
47
- . flatMapThrowing {
48
- guard let result = $0. int else { throw RedisError . respConversion ( to: Int . self) }
49
- return result
50
- }
42
+ . mapFromRESP ( )
51
43
}
52
44
53
45
/// Removes the provided items from the set stored at key, returning the count of items removed.
@@ -58,10 +50,7 @@ extension RedisCommandExecutor {
58
50
assert ( items. count > 0 , " There must be at least 1 item listed to remove. " )
59
51
60
52
return send ( command: " SREM " , with: [ key] + items)
61
- . flatMapThrowing {
62
- guard let result = $0. int else { throw RedisError . respConversion ( to: Int . self) }
63
- return result
64
- }
53
+ . mapFromRESP ( )
65
54
}
66
55
67
56
/// Randomly selects an item from the set stored at key, and removes it.
@@ -90,10 +79,7 @@ extension RedisCommandExecutor {
90
79
/// [https://redis.io/commands/sdiff](https://redis.io/commands/sdiff)
91
80
public func sdiff( _ keys: String ... ) -> EventLoopFuture < [ RESPValue ] > {
92
81
return send ( command: " SDIFF " , with: keys)
93
- . flatMapThrowing {
94
- guard let elements = $0. array else { throw RedisError . respConversion ( to: Array< RESPValue> . self ) }
95
- return elements
96
- }
82
+ . mapFromRESP ( )
97
83
}
98
84
99
85
/// Functionally equivalent to `sdiff`, but instead stores the resulting set at the `destination` key
@@ -103,21 +89,15 @@ extension RedisCommandExecutor {
103
89
/// - Important: If the `destination` key already exists, it is overwritten.
104
90
public func sdiffstore( destination dest: String , _ keys: String ... ) -> EventLoopFuture < Int > {
105
91
return send ( command: " SDIFFSTORE " , with: [ dest] + keys)
106
- . flatMapThrowing {
107
- guard let count = $0. int else { throw RedisError . respConversion ( to: Int . self) }
108
- return count
109
- }
92
+ . mapFromRESP ( )
110
93
}
111
94
112
95
/// Returns the members of the set resulting from the intersection of all the given sets.
113
96
///
114
97
/// [https://redis.io/commands/sinter](https://redis.io/commands/sinter)
115
98
public func sinter( _ keys: String ... ) -> EventLoopFuture < [ RESPValue ] > {
116
99
return send ( command: " SINTER " , with: keys)
117
- . flatMapThrowing {
118
- guard let elements = $0. array else { throw RedisError . respConversion ( to: Array< RESPValue> . self ) }
119
- return elements
120
- }
100
+ . mapFromRESP ( )
121
101
}
122
102
123
103
/// Functionally equivalent to `sinter`, but instead stores the resulting set at the `destination` key
@@ -127,10 +107,7 @@ extension RedisCommandExecutor {
127
107
/// - Important: If the `destination` key already exists, it is overwritten.
128
108
public func sinterstore( destination dest: String , _ keys: String ... ) -> EventLoopFuture < Int > {
129
109
return send ( command: " SINTERSTORE " , with: [ dest] + keys)
130
- . flatMapThrowing {
131
- guard let count = $0. int else { throw RedisError . respConversion ( to: Int . self) }
132
- return count
133
- }
110
+ . mapFromRESP ( )
134
111
}
135
112
136
113
/// Moves the `item` from the source key to the destination key.
@@ -139,21 +116,16 @@ extension RedisCommandExecutor {
139
116
/// - Important: This will resolve to `true` as long as it was successfully removed from the `source` key.
140
117
public func smove( item: RESPValueConvertible , fromKey source: String , toKey dest: String ) -> EventLoopFuture < Bool > {
141
118
return send ( command: " SMOVE " , with: [ source, dest, item] )
142
- . flatMapThrowing {
143
- guard let result = $0. int else { throw RedisError . respConversion ( to: Int . self) }
144
- return result == 1
145
- }
119
+ . mapFromRESP ( )
120
+ . map { return $0 == 1 }
146
121
}
147
122
148
123
/// Returns the members of the set resulting from the union of all the given keys.
149
124
///
150
125
/// [https://redis.io/commands/sunion](https://redis.io/commands/sunion)
151
126
public func sunion( _ keys: String ... ) -> EventLoopFuture < [ RESPValue ] > {
152
127
return send ( command: " SUNION " , with: keys)
153
- . flatMapThrowing {
154
- guard let elements = $0. array else { throw RedisError . respConversion ( to: Array< RESPValue> . self ) }
155
- return elements
156
- }
128
+ . mapFromRESP ( )
157
129
}
158
130
159
131
/// Functionally equivalent to `sunion`, but instead stores the resulting set at the `destination` key
@@ -163,10 +135,7 @@ extension RedisCommandExecutor {
163
135
/// - Important: If the `destination` key already exists, it is overwritten.
164
136
public func sunionstore( destination dest: String , _ keys: String ... ) -> EventLoopFuture < Int > {
165
137
return send ( command: " SUNIONSTORE " , with: [ dest] + keys)
166
- . flatMapThrowing {
167
- guard let count = $0. int else { throw RedisError . respConversion ( to: Int . self) }
168
- return count
169
- }
138
+ . mapFromRESP ( )
170
139
}
171
140
172
141
/// Incrementally iterates over a set, returning a cursor position for additional calls with a limited collection
@@ -193,16 +162,18 @@ extension RedisCommandExecutor {
193
162
args. append ( c)
194
163
}
195
164
196
- return send ( command: " SSCAN " , with: args)
197
- . flatMapThrowing {
198
- guard let response = $0. array else { throw RedisError . respConversion ( to: Array< RESPValue> . self ) }
199
- guard
200
- let position = response [ 0 ] . string,
201
- let newPosition = Int ( position)
202
- else { throw RedisError . respConversion ( to: Int . self) }
203
- guard let elements = response [ 1 ] . array else { throw RedisError . respConversion ( to: Array< RESPValue> . self ) }
204
-
205
- return ( newPosition, elements)
206
- }
165
+ let response = send ( command: " SSCAN " , with: args) . mapFromRESP ( to: [ RESPValue ] . self)
166
+ let position = response. flatMapThrowing { result -> Int in
167
+ guard
168
+ let value = result [ 0 ] . string,
169
+ let position = Int ( value)
170
+ else { throw RedisError ( identifier: #function, reason: " Unexpected value in response: \( result [ 0 ] ) " ) }
171
+ return position
172
+ }
173
+ let elements = response
174
+ . map { return $0 [ 1 ] }
175
+ . mapFromRESP ( to: [ RESPValue ] . self)
176
+
177
+ return position. and ( elements)
207
178
}
208
179
}
0 commit comments