Skip to content

Commit c1ba671

Browse files
committed
Reduce RESPValue initialization complexities and simplify RedisKey implementation
Motivation: `RESPValue` exposes a fair amount of complexity on how to intialize a single instance with the various overloads. This aims to simplify the complexity for developers by providing a single initializer and relying on `RESPValueConvertible` to handle the complexities. In addition, the Swift complier synthesizes a lot of default conformances that `RedisKey` has manually written, which is just unnecessary code. Modifications: - Rename: `RESPValue.init(_:)` to `RESPValue.init(from:)` - Change: `RESPValue.init` `String?` and `FixedWidthInteger` overloads from `public` to `internal` - Remove: Unnecessary code for various protocol conformances for `RedisKey` Result: Developers should have a direct and guided way of making instances of `RESPValue`
1 parent 3e1a3d5 commit c1ba671

8 files changed

+107
-151
lines changed

Sources/RediStack/Commands/BasicCommands.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ extension RedisClient {
111111
/// - Returns: `true` if the expiration was set.
112112
public func expire(_ key: RedisKey, after timeout: TimeAmount) -> EventLoopFuture<Bool> {
113113
let args: [RESPValue] = [
114-
.init(bulk: key),
114+
.init(from: key),
115115
.init(bulk: timeout.seconds)
116116
]
117117
return send(command: "EXPIRE", with: args)
@@ -154,7 +154,7 @@ extension RedisClient {
154154
var args: [RESPValue] = [.init(bulk: pos)]
155155

156156
if let k = key {
157-
args.insert(.init(bulk: k), at: 0)
157+
args.insert(.init(from: k), at: 0)
158158
}
159159

160160
if let m = match {

Sources/RediStack/Commands/HashCommands.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ extension RedisClient {
5252
public func hdel(_ fields: [String], from key: RedisKey) -> EventLoopFuture<Int> {
5353
guard fields.count > 0 else { return self.eventLoop.makeSucceededFuture(0) }
5454

55-
var args: [RESPValue] = [.init(bulk: key)]
55+
var args: [RESPValue] = [.init(from: key)]
5656
args.append(convertingContentsOf: fields)
5757

5858
return send(command: "HDEL", with: args)
@@ -79,7 +79,7 @@ extension RedisClient {
7979
/// - Returns: `true` if the hash contains the field, `false` if either the key or field do not exist.
8080
public func hexists(_ field: String, in key: RedisKey) -> EventLoopFuture<Bool> {
8181
let args: [RESPValue] = [
82-
.init(bulk: key),
82+
.init(from: key),
8383
.init(bulk: field)
8484
]
8585
return send(command: "HEXISTS", with: args)
@@ -93,7 +93,7 @@ extension RedisClient {
9393
/// - Parameter key: The key of the hash to get field count of.
9494
/// - Returns: The number of fields in the hash, or `0` if the key doesn't exist.
9595
public func hlen(of key: RedisKey) -> EventLoopFuture<Int> {
96-
let args = [RESPValue(bulk: key)]
96+
let args = [RESPValue(from: key)]
9797
return send(command: "HLEN", with: args)
9898
.map()
9999
}
@@ -107,7 +107,7 @@ extension RedisClient {
107107
/// - Returns: The string length of the hash field's value, or `0` if the field or hash do not exist.
108108
public func hstrlen(of field: String, in key: RedisKey) -> EventLoopFuture<Int> {
109109
let args: [RESPValue] = [
110-
.init(bulk: key),
110+
.init(from: key),
111111
.init(bulk: field)
112112
]
113113
return send(command: "HSTRLEN", with: args)
@@ -120,7 +120,7 @@ extension RedisClient {
120120
/// - Parameter key: The key of the hash.
121121
/// - Returns: A list of field names stored within the hash.
122122
public func hkeys(in key: RedisKey) -> EventLoopFuture<[String]> {
123-
let args = [RESPValue(bulk: key)]
123+
let args = [RESPValue(from: key)]
124124
return send(command: "HKEYS", with: args)
125125
.map()
126126
}
@@ -131,7 +131,7 @@ extension RedisClient {
131131
/// - Parameter key: The key of the hash.
132132
/// - Returns: A list of all values stored in a hash.
133133
public func hvals(in key: RedisKey) -> EventLoopFuture<[RESPValue]> {
134-
let args = [RESPValue(bulk: key)]
134+
let args = [RESPValue(from: key)]
135135
return send(command: "HVALS", with: args)
136136
.map()
137137
}
@@ -216,7 +216,7 @@ extension RedisClient {
216216
in key: RedisKey
217217
) -> EventLoopFuture<Bool> {
218218
let args: [RESPValue] = [
219-
.init(bulk: key),
219+
.init(from: key),
220220
.init(bulk: field),
221221
value.convertedToRESPValue()
222222
]
@@ -241,7 +241,7 @@ extension RedisClient {
241241
in key: RedisKey
242242
) -> EventLoopFuture<Bool> {
243243
let args: [RESPValue] = [
244-
.init(bulk: key),
244+
.init(from: key),
245245
.init(bulk: field),
246246
value.convertedToRESPValue()
247247
]
@@ -264,7 +264,7 @@ extension RedisClient {
264264
) -> EventLoopFuture<Void> {
265265
assert(fields.count > 0, "At least 1 key-value pair should be specified")
266266

267-
var args: [RESPValue] = [.init(bulk: key)]
267+
var args: [RESPValue] = [.init(from: key)]
268268
args.add(contentsOf: fields, overestimatedCountBeingAdded: fields.count * 2) { (array, element) in
269269
array.append(.init(bulk: element.key))
270270
array.append(element.value.convertedToRESPValue())
@@ -287,7 +287,7 @@ extension RedisClient {
287287
/// - Returns: The value of the hash field. If the key or field does not exist, it will be `.null`.
288288
public func hget(_ field: String, from key: RedisKey) -> EventLoopFuture<RESPValue> {
289289
let args: [RESPValue] = [
290-
.init(bulk: key),
290+
.init(from: key),
291291
.init(bulk: field)
292292
]
293293
return send(command: "HGET", with: args)
@@ -321,7 +321,7 @@ extension RedisClient {
321321
public func hmget(_ fields: [String], from key: RedisKey) -> EventLoopFuture<[RESPValue]> {
322322
guard fields.count > 0 else { return self.eventLoop.makeSucceededFuture([]) }
323323

324-
var args: [RESPValue] = [.init(bulk: key)]
324+
var args: [RESPValue] = [.init(from: key)]
325325
args.append(convertingContentsOf: fields)
326326

327327
return send(command: "HMGET", with: args)
@@ -380,7 +380,7 @@ extension RedisClient {
380380
/// - Parameter key: The key of the hash to pull from.
381381
/// - Returns: A key-value pair list of fields and their values.
382382
public func hgetall(from key: RedisKey) -> EventLoopFuture<[String: RESPValue]> {
383-
let args = [RESPValue(bulk: key)]
383+
let args = [RESPValue(from: key)]
384384
return send(command: "HGETALL", with: args)
385385
.map(to: [RESPValue].self)
386386
.flatMapThrowing(Self._mapHashResponse)
@@ -448,7 +448,7 @@ extension RedisClient {
448448
_ key: RedisKey
449449
) -> EventLoopFuture<Value> {
450450
let args: [RESPValue] = [
451-
.init(bulk: key),
451+
.init(from: key),
452452
.init(bulk: field),
453453
amount.convertedToRESPValue()
454454
]

Sources/RediStack/Commands/ListCommands.swift

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ extension RedisClient {
2323
/// - Parameter key: The key of the list.
2424
/// - Returns: The number of elements in the list.
2525
public func llen(of key: RedisKey) -> EventLoopFuture<Int> {
26-
let args = [RESPValue(bulk: key)]
26+
let args = [RESPValue(from: key)]
2727
return send(command: "LLEN", with: args)
2828
.map()
2929
}
@@ -37,7 +37,7 @@ extension RedisClient {
3737
/// - Returns: The element stored at index, or `.null` if out of bounds.
3838
public func lindex(_ index: Int, from key: RedisKey) -> EventLoopFuture<RESPValue> {
3939
let args: [RESPValue] = [
40-
.init(bulk: key),
40+
.init(from: key),
4141
.init(bulk: index)
4242
]
4343
return send(command: "LINDEX", with: args)
@@ -76,7 +76,7 @@ extension RedisClient {
7676
in key: RedisKey
7777
) -> EventLoopFuture<Void> {
7878
let args: [RESPValue] = [
79-
.init(bulk: key),
79+
.init(from: key),
8080
.init(bulk: index),
8181
value.convertedToRESPValue()
8282
]
@@ -99,7 +99,7 @@ extension RedisClient {
9999
count: Int = 0
100100
) -> EventLoopFuture<Int> {
101101
let args: [RESPValue] = [
102-
.init(bulk: key),
102+
.init(from: key),
103103
.init(bulk: count),
104104
value.convertedToRESPValue()
105105
]
@@ -121,7 +121,7 @@ extension RedisClient {
121121
/// - Returns: A `NIO.EventLoopFuture` that resolves when the operation has succeeded, or fails with a `RedisError`.
122122
public func ltrim(_ key: RedisKey, before start: Int, after stop: Int) -> EventLoopFuture<Void> {
123123
let args: [RESPValue] = [
124-
.init(bulk: key),
124+
.init(from: key),
125125
.init(bulk: start),
126126
.init(bulk: stop)
127127
]
@@ -261,7 +261,7 @@ extension RedisClient {
261261
/// - Returns: An array of elements found within the range specified.
262262
public func lrange(from key: RedisKey, firstIndex: Int, lastIndex: Int) -> EventLoopFuture<[RESPValue]> {
263263
let args: [RESPValue] = [
264-
.init(bulk: key),
264+
.init(from: key),
265265
.init(bulk: firstIndex),
266266
.init(bulk: lastIndex)
267267
]
@@ -533,8 +533,8 @@ extension RedisClient {
533533
/// - Returns: The element that was moved.
534534
public func rpoplpush(from source: RedisKey, to dest: RedisKey) -> EventLoopFuture<RESPValue> {
535535
let args: [RESPValue] = [
536-
.init(bulk: source),
537-
.init(bulk: dest)
536+
.init(from: source),
537+
.init(from: dest)
538538
]
539539
return send(command: "RPOPLPUSH", with: args)
540540
}
@@ -579,9 +579,9 @@ extension RedisClient {
579579
timeout: TimeAmount = .seconds(0)
580580
) -> EventLoopFuture<RESPValue> {
581581
let args: [RESPValue] = [
582-
.init(bulk: source),
583-
.init(bulk: dest),
584-
.init(bulk: timeout.seconds)
582+
.init(from: source),
583+
.init(from: dest),
584+
.init(from: timeout.seconds)
585585
]
586586
return send(command: "BRPOPLPUSH", with: args)
587587
}
@@ -662,7 +662,7 @@ extension RedisClient {
662662
_ pivot: Value
663663
) -> EventLoopFuture<Int> {
664664
let args: [RESPValue] = [
665-
.init(bulk: key),
665+
.init(from: key),
666666
.init(bulk: pivotKeyword),
667667
pivot.convertedToRESPValue(),
668668
element.convertedToRESPValue()
@@ -681,7 +681,7 @@ extension RedisClient {
681681
/// - Parameter key: The key of the list to pop from.
682682
/// - Returns: The element that was popped from the list, or `.null`.
683683
public func lpop(from key: RedisKey) -> EventLoopFuture<RESPValue> {
684-
let args = [RESPValue(bulk: key)]
684+
let args = [RESPValue(from: key)]
685685
return send(command: "LPOP", with: args)
686686
}
687687

@@ -710,7 +710,7 @@ extension RedisClient {
710710
public func lpush<Value: RESPValueConvertible>(_ elements: [Value], into key: RedisKey) -> EventLoopFuture<Int> {
711711
assert(elements.count > 0, "At least 1 element should be provided.")
712712

713-
var args: [RESPValue] = [.init(bulk: key)]
713+
var args: [RESPValue] = [.init(from: key)]
714714
args.append(convertingContentsOf: elements)
715715

716716
return send(command: "LPUSH", with: args)
@@ -741,7 +741,7 @@ extension RedisClient {
741741
@inlinable
742742
public func lpushx<Value: RESPValueConvertible>(_ element: Value, into key: RedisKey) -> EventLoopFuture<Int> {
743743
let args: [RESPValue] = [
744-
.init(bulk: key),
744+
.init(from: key),
745745
element.convertedToRESPValue()
746746
]
747747
return send(command: "LPUSHX", with: args)
@@ -758,7 +758,7 @@ extension RedisClient {
758758
/// - Parameter key: The key of the list to pop from.
759759
/// - Returns: The element that was popped from the list, else `.null`.
760760
public func rpop(from key: RedisKey) -> EventLoopFuture<RESPValue> {
761-
let args = [RESPValue(bulk: key)]
761+
let args = [RESPValue(from: key)]
762762
return send(command: "RPOP", with: args)
763763
}
764764

@@ -784,7 +784,7 @@ extension RedisClient {
784784
public func rpush<Value: RESPValueConvertible>(_ elements: [Value], into key: RedisKey) -> EventLoopFuture<Int> {
785785
assert(elements.count > 0, "At least 1 element should be provided.")
786786

787-
var args: [RESPValue] = [.init(bulk: key)]
787+
var args: [RESPValue] = [.init(from: key)]
788788
args.append(convertingContentsOf: elements)
789789

790790
return send(command: "RPUSH", with: args)
@@ -814,7 +814,7 @@ extension RedisClient {
814814
@inlinable
815815
public func rpushx<Value: RESPValueConvertible>(_ element: Value, into key: RedisKey) -> EventLoopFuture<Int> {
816816
let args: [RESPValue] = [
817-
.init(bulk: key),
817+
.init(from: key),
818818
element.convertedToRESPValue()
819819
]
820820
return send(command: "RPUSHX", with: args)

Sources/RediStack/Commands/SetCommands.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ extension RedisClient {
2626
/// - Parameter key: The key of the set.
2727
/// - Returns: A list of elements found within the set.
2828
public func smembers(of key: RedisKey) -> EventLoopFuture<[RESPValue]> {
29-
let args = [RESPValue(bulk: key)]
29+
let args = [RESPValue(from: key)]
3030
return send(command: "SMEMBERS", with: args)
3131
.map()
3232
}
@@ -57,7 +57,7 @@ extension RedisClient {
5757
@inlinable
5858
public func sismember<Value: RESPValueConvertible>(_ element: Value, of key: RedisKey) -> EventLoopFuture<Bool> {
5959
let args: [RESPValue] = [
60-
.init(bulk: key),
60+
.init(from: key),
6161
element.convertedToRESPValue()
6262
]
6363
return send(command: "SISMEMBER", with: args)
@@ -71,7 +71,7 @@ extension RedisClient {
7171
/// - Parameter key: The key of the set.
7272
/// - Returns: The total count of elements in the set.
7373
public func scard(of key: RedisKey) -> EventLoopFuture<Int> {
74-
let args = [RESPValue(bulk: key)]
74+
let args = [RESPValue(from: key)]
7575
return send(command: "SCARD", with: args)
7676
.map()
7777
}
@@ -87,7 +87,7 @@ extension RedisClient {
8787
public func sadd<Value: RESPValueConvertible>(_ elements: [Value], to key: RedisKey) -> EventLoopFuture<Int> {
8888
guard elements.count > 0 else { return self.eventLoop.makeSucceededFuture(0) }
8989

90-
var args: [RESPValue] = [.init(bulk: key)]
90+
var args: [RESPValue] = [.init(from: key)]
9191
args.append(convertingContentsOf: elements)
9292

9393
return send(command: "SADD", with: args)
@@ -117,7 +117,7 @@ extension RedisClient {
117117
public func srem<Value: RESPValueConvertible>(_ elements: [Value], from key: RedisKey) -> EventLoopFuture<Int> {
118118
guard elements.count > 0 else { return self.eventLoop.makeSucceededFuture(0) }
119119

120-
var args: [RESPValue] = [.init(bulk: key)]
120+
var args: [RESPValue] = [.init(from: key)]
121121
args.append(convertingContentsOf: elements)
122122

123123
return send(command: "SREM", with: args)
@@ -149,7 +149,7 @@ extension RedisClient {
149149
guard count > 0 else { return self.eventLoop.makeSucceededFuture([]) }
150150

151151
let args: [RESPValue] = [
152-
.init(bulk: key),
152+
.init(from: key),
153153
.init(bulk: count)
154154
]
155155
return send(command: "SPOP", with: args)
@@ -189,7 +189,7 @@ extension RedisClient {
189189
guard count != 0 else { return self.eventLoop.makeSucceededFuture([]) }
190190

191191
let args: [RESPValue] = [
192-
.init(bulk: key),
192+
.init(from: key),
193193
.init(bulk: count)
194194
]
195195
return send(command: "SRANDMEMBER", with: args)
@@ -231,8 +231,8 @@ extension RedisClient {
231231
guard sourceKey != destKey else { return self.eventLoop.makeSucceededFuture(true) }
232232

233233
let args: [RESPValue] = [
234-
.init(bulk: sourceKey),
235-
.init(bulk: destKey),
234+
.init(from: sourceKey),
235+
.init(from: destKey),
236236
element.convertedToRESPValue()
237237
]
238238
return send(command: "SMOVE", with: args)
@@ -345,7 +345,7 @@ extension RedisClient {
345345
public func sdiffstore(as destination: RedisKey, sources keys: [RedisKey]) -> EventLoopFuture<Int> {
346346
assert(keys.count > 0, "At least 1 key should be provided.")
347347

348-
var args: [RESPValue] = [.init(bulk: destination)]
348+
var args: [RESPValue] = [.init(from: destination)]
349349
args.append(convertingContentsOf: keys)
350350

351351
return send(command: "SDIFFSTORE", with: args)
@@ -414,7 +414,7 @@ extension RedisClient {
414414
public func sinterstore(as destination: RedisKey, sources keys: [RedisKey]) -> EventLoopFuture<Int> {
415415
assert(keys.count > 0, "At least 1 key should be provided.")
416416

417-
var args: [RESPValue] = [.init(bulk: destination)]
417+
var args: [RESPValue] = [.init(from: destination)]
418418
args.append(convertingContentsOf: keys)
419419

420420
return send(command: "SINTERSTORE", with: args)
@@ -483,7 +483,7 @@ extension RedisClient {
483483
public func sunionstore(as destination: RedisKey, sources keys: [RedisKey]) -> EventLoopFuture<Int> {
484484
assert(keys.count > 0, "At least 1 key should be provided.")
485485

486-
var args: [RESPValue] = [.init(bulk: destination)]
486+
var args: [RESPValue] = [.init(from: destination)]
487487
args.append(convertingContentsOf: keys)
488488

489489
return send(command: "SUNIONSTORE", with: args)

0 commit comments

Comments
 (0)