Skip to content

Commit a5f9d76

Browse files
committed
Merge branch 'typed-keys' into 'master'
Add type-safe representation of Redis keys See merge request Mordil/swift-redi-stack!93
2 parents 5d6c3d3 + ea6f427 commit a5f9d76

14 files changed

+303
-192
lines changed

Sources/RediStack/Commands/BasicCommands.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ extension RedisClient {
9090
/// - Parameter keys: A list of keys to delete from the database.
9191
/// - Returns: The number of keys deleted from the database.
9292
@inlinable
93-
public func delete(_ keys: [String]) -> EventLoopFuture<Int> {
93+
public func delete(_ keys: [RedisKey]) -> EventLoopFuture<Int> {
9494
guard keys.count > 0 else { return self.eventLoop.makeSucceededFuture(0) }
9595

9696
let args = keys.map(RESPValue.init)
@@ -104,7 +104,7 @@ extension RedisClient {
104104
/// - Parameter keys: A list of keys to delete from the database.
105105
/// - Returns: The number of keys deleted from the database.
106106
@inlinable
107-
public func delete(_ keys: String...) -> EventLoopFuture<Int> {
107+
public func delete(_ keys: RedisKey...) -> EventLoopFuture<Int> {
108108
return self.delete(keys)
109109
}
110110

@@ -117,7 +117,7 @@ extension RedisClient {
117117
/// - timeout: The time from now the key will expire at.
118118
/// - Returns: `true` if the expiration was set.
119119
@inlinable
120-
public func expire(_ key: String, after timeout: TimeAmount) -> EventLoopFuture<Bool> {
120+
public func expire(_ key: RedisKey, after timeout: TimeAmount) -> EventLoopFuture<Bool> {
121121
let amount = timeout.nanoseconds / 1_000_000_000
122122
let args: [RESPValue] = [
123123
.init(bulk: key),
@@ -153,7 +153,7 @@ extension RedisClient {
153153
internal func _scan<T>(
154154
command: String,
155155
resultType: T.Type = T.self,
156-
_ key: String?,
156+
_ key: RedisKey?,
157157
_ pos: Int,
158158
_ count: Int?,
159159
_ match: String?

Sources/RediStack/Commands/HashCommands.swift

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ extension RedisClient {
4646
/// - key: The key of the hash to delete from.
4747
/// - Returns: The number of fields that were deleted.
4848
@inlinable
49-
public func hdel(_ fields: [String], from key: String) -> EventLoopFuture<Int> {
49+
public func hdel(_ fields: [String], from key: RedisKey) -> EventLoopFuture<Int> {
5050
guard fields.count > 0 else { return self.eventLoop.makeSucceededFuture(0) }
5151

5252
var args: [RESPValue] = [.init(bulk: key)]
@@ -64,7 +64,7 @@ extension RedisClient {
6464
/// - key: The key of the hash to delete from.
6565
/// - Returns: The number of fields that were deleted.
6666
@inlinable
67-
public func hdel(_ fields: String..., from key: String) -> EventLoopFuture<Int> {
67+
public func hdel(_ fields: String..., from key: RedisKey) -> EventLoopFuture<Int> {
6868
return self.hdel(fields, from: key)
6969
}
7070

@@ -76,7 +76,7 @@ extension RedisClient {
7676
/// - key: The key of the hash to look within.
7777
/// - Returns: `true` if the hash contains the field, `false` if either the key or field do not exist.
7878
@inlinable
79-
public func hexists(_ field: String, in key: String) -> EventLoopFuture<Bool> {
79+
public func hexists(_ field: String, in key: RedisKey) -> EventLoopFuture<Bool> {
8080
let args: [RESPValue] = [
8181
.init(bulk: key),
8282
.init(bulk: field)
@@ -92,7 +92,7 @@ extension RedisClient {
9292
/// - Parameter key: The key of the hash to get field count of.
9393
/// - Returns: The number of fields in the hash, or `0` if the key doesn't exist.
9494
@inlinable
95-
public func hlen(of key: String) -> EventLoopFuture<Int> {
95+
public func hlen(of key: RedisKey) -> EventLoopFuture<Int> {
9696
let args = [RESPValue(bulk: key)]
9797
return send(command: "HLEN", with: args)
9898
.convertFromRESPValue()
@@ -106,7 +106,7 @@ extension RedisClient {
106106
/// - key: The key of the hash.
107107
/// - Returns: The string length of the hash field's value, or `0` if the field or hash do not exist.
108108
@inlinable
109-
public func hstrlen(of field: String, in key: String) -> EventLoopFuture<Int> {
109+
public func hstrlen(of field: String, in key: RedisKey) -> EventLoopFuture<Int> {
110110
let args: [RESPValue] = [
111111
.init(bulk: key),
112112
.init(bulk: field)
@@ -121,7 +121,7 @@ extension RedisClient {
121121
/// - Parameter key: The key of the hash.
122122
/// - Returns: A list of field names stored within the hash.
123123
@inlinable
124-
public func hkeys(in key: String) -> EventLoopFuture<[String]> {
124+
public func hkeys(in key: RedisKey) -> EventLoopFuture<[String]> {
125125
let args = [RESPValue(bulk: key)]
126126
return send(command: "HKEYS", with: args)
127127
.convertFromRESPValue()
@@ -133,7 +133,7 @@ extension RedisClient {
133133
/// - Parameter key: The key of the hash.
134134
/// - Returns: A list of all values stored in a hash.
135135
@inlinable
136-
public func hvals(in key: String) -> EventLoopFuture<[RESPValue]> {
136+
public func hvals(in key: RedisKey) -> EventLoopFuture<[RESPValue]> {
137137
let args = [RESPValue(bulk: key)]
138138
return send(command: "HVALS", with: args)
139139
.convertFromRESPValue()
@@ -150,7 +150,7 @@ extension RedisClient {
150150
/// - Returns: A cursor position for additional invocations with a limited collection of found fields and their values.
151151
@inlinable
152152
public func hscan(
153-
_ key: String,
153+
_ key: RedisKey,
154154
startingFrom position: Int = 0,
155155
count: Int? = nil,
156156
matching match: String? = nil
@@ -179,7 +179,7 @@ extension RedisClient {
179179
public func hset<Value: RESPValueConvertible>(
180180
_ field: String,
181181
to value: Value,
182-
in key: String
182+
in key: RedisKey
183183
) -> EventLoopFuture<Bool> {
184184
let args: [RESPValue] = [
185185
.init(bulk: key),
@@ -204,7 +204,7 @@ extension RedisClient {
204204
public func hsetnx<Value: RESPValueConvertible>(
205205
_ field: String,
206206
to value: Value,
207-
in key: String
207+
in key: RedisKey
208208
) -> EventLoopFuture<Bool> {
209209
let args: [RESPValue] = [
210210
.init(bulk: key),
@@ -226,7 +226,7 @@ extension RedisClient {
226226
@inlinable
227227
public func hmset<Value: RESPValueConvertible>(
228228
_ fields: [String: Value],
229-
in key: String
229+
in key: RedisKey
230230
) -> EventLoopFuture<Void> {
231231
assert(fields.count > 0, "At least 1 key-value pair should be specified")
232232

@@ -252,7 +252,7 @@ extension RedisClient {
252252
/// - key: The key of the hash being accessed.
253253
/// - Returns: The value of the hash field, or `nil` if either the key or field does not exist.
254254
@inlinable
255-
public func hget(_ field: String, from key: String) -> EventLoopFuture<String?> {
255+
public func hget(_ field: String, from key: RedisKey) -> EventLoopFuture<String?> {
256256
let args: [RESPValue] = [
257257
.init(bulk: key),
258258
.init(bulk: field)
@@ -269,7 +269,7 @@ extension RedisClient {
269269
/// - key: The key of the hash being accessed.
270270
/// - Returns: A list of values in the same order as the `fields` argument. Non-existent fields return `nil` values.
271271
@inlinable
272-
public func hmget(_ fields: [String], from key: String) -> EventLoopFuture<[String?]> {
272+
public func hmget(_ fields: [String], from key: RedisKey) -> EventLoopFuture<[String?]> {
273273
guard fields.count > 0 else { return self.eventLoop.makeSucceededFuture([]) }
274274

275275
var args: [RESPValue] = [.init(bulk: key)]
@@ -288,7 +288,7 @@ extension RedisClient {
288288
/// - key: The key of the hash being accessed.
289289
/// - Returns: A list of values in the same order as the `fields` argument. Non-existent fields return `nil` values.
290290
@inlinable
291-
public func hmget(_ fields: String..., from key: String) -> EventLoopFuture<[String?]> {
291+
public func hmget(_ fields: String..., from key: RedisKey) -> EventLoopFuture<[String?]> {
292292
return self.hmget(fields, from: key)
293293
}
294294

@@ -298,7 +298,7 @@ extension RedisClient {
298298
/// - Parameter key: The key of the hash to pull from.
299299
/// - Returns: A key-value pair list of fields and their values.
300300
@inlinable
301-
public func hgetall(from key: String) -> EventLoopFuture<[String: String]> {
301+
public func hgetall(from key: RedisKey) -> EventLoopFuture<[String: String]> {
302302
let args = [RESPValue(bulk: key)]
303303
return send(command: "HGETALL", with: args)
304304
.convertFromRESPValue(to: [String].self)
@@ -318,7 +318,7 @@ extension RedisClient {
318318
/// - key: The key of the hash the field is stored in.
319319
/// - Returns: The new value of the hash field.
320320
@inlinable
321-
public func hincrby(_ amount: Int, field: String, in key: String) -> EventLoopFuture<Int> {
321+
public func hincrby(_ amount: Int, field: String, in key: RedisKey) -> EventLoopFuture<Int> {
322322
return _hincr(command: "HINCRBY", amount, field, key)
323323
}
324324

@@ -331,7 +331,7 @@ extension RedisClient {
331331
/// - key: The key of the hash the field is stored in.
332332
/// - Returns: The new value of the hash field.
333333
@inlinable
334-
public func hincrbyfloat<Value>(_ amount: Value, field: String, in key: String) -> EventLoopFuture<Value>
334+
public func hincrbyfloat<Value>(_ amount: Value, field: String, in key: RedisKey) -> EventLoopFuture<Value>
335335
where
336336
Value: BinaryFloatingPoint,
337337
Value: RESPValueConvertible
@@ -344,7 +344,7 @@ extension RedisClient {
344344
command: String,
345345
_ amount: Value,
346346
_ field: String,
347-
_ key: String
347+
_ key: RedisKey
348348
) -> EventLoopFuture<Value> {
349349
let args: [RESPValue] = [
350350
.init(bulk: key),

0 commit comments

Comments
 (0)