Skip to content

Commit 41f9377

Browse files
committed
Refine Redis Command API
Motivation: It was noticed that many of the commands are cumbersome to use with boilerplate type casting for each use that can be simplified within the library by doing type conversion before returning the value to an end user. Modifications: Many APIs that return a `RESPValue` now have overloads to provide a `RESPValueConvertible` type that the value will be turned into before being returned. For a few APIs that returned `RESPValue`, they did so as an Optional. Those APIs have been changed to always provide a `RESPValue` and return `.null` in cases where `nil` was returned. In addition, the `@inlinable` attribute has been removed from any non-generic command API. Result: Developers should have less code boilerplate for turning values from `RESPValue` to their desired type with many commands.
1 parent 86f2eb6 commit 41f9377

File tree

10 files changed

+788
-202
lines changed

10 files changed

+788
-202
lines changed

Sources/RediStack/Commands/BasicCommands.swift

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ extension RedisClient {
2020
/// See [https://redis.io/commands/echo](https://redis.io/commands/echo)
2121
/// - Parameter message: The message to echo.
2222
/// - Returns: The message sent with the command.
23-
@inlinable
2423
public func echo(_ message: String) -> EventLoopFuture<String> {
2524
let args = [RESPValue(bulk: message)]
2625
return send(command: "ECHO", with: args)
@@ -32,7 +31,6 @@ extension RedisClient {
3231
/// See [https://redis.io/commands/ping](https://redis.io/commands/ping)
3332
/// - Parameter message: The optional message that the server should respond with.
3433
/// - Returns: The provided message or Redis' default response of `"PONG"`.
35-
@inlinable
3634
public func ping(with message: String? = nil) -> EventLoopFuture<String> {
3735
let args: [RESPValue] = message != nil
3836
? [.init(bulk: message!)] // safe because we did a nil pre-check
@@ -47,7 +45,6 @@ extension RedisClient {
4745
/// [https://redis.io/commands/select](https://redis.io/commands/select)
4846
/// - Parameter index: The 0-based index of the database that will receive later commands.
4947
/// - Returns: An `EventLoopFuture` that resolves when the operation has succeeded, or fails with a `RedisError`.
50-
@inlinable
5148
public func select(database index: Int) -> EventLoopFuture<Void> {
5249
let args = [RESPValue(bulk: index)]
5350
return send(command: "SELECT", with: args)
@@ -61,7 +58,6 @@ extension RedisClient {
6158
/// - first: The index of the first database.
6259
/// - second: The index of the second database.
6360
/// - Returns: `true` if the swap was successful.
64-
@inlinable
6561
public func swapDatabase(_ first: Int, with second: Int) -> EventLoopFuture<Bool> {
6662
let args: [RESPValue] = [
6763
.init(bulk: first),
@@ -77,7 +73,6 @@ extension RedisClient {
7773
/// [https://redis.io/commands/auth](https://redis.io/commands/auth)
7874
/// - Parameter password: The password to authenticate with.
7975
/// - Returns: A `NIO.EventLoopFuture` that resolves if the password was accepted, otherwise it fails.
80-
@inlinable
8176
public func authorize(with password: String) -> EventLoopFuture<Void> {
8277
let args = [RESPValue(bulk: password)]
8378
return send(command: "AUTH", with: args)
@@ -89,7 +84,6 @@ extension RedisClient {
8984
/// [https://redis.io/commands/del](https://redis.io/commands/del)
9085
/// - Parameter keys: A list of keys to delete from the database.
9186
/// - Returns: The number of keys deleted from the database.
92-
@inlinable
9387
public func delete(_ keys: [RedisKey]) -> EventLoopFuture<Int> {
9488
guard keys.count > 0 else { return self.eventLoop.makeSucceededFuture(0) }
9589

@@ -103,7 +97,6 @@ extension RedisClient {
10397
/// [https://redis.io/commands/del](https://redis.io/commands/del)
10498
/// - Parameter keys: A list of keys to delete from the database.
10599
/// - Returns: The number of keys deleted from the database.
106-
@inlinable
107100
public func delete(_ keys: RedisKey...) -> EventLoopFuture<Int> {
108101
return self.delete(keys)
109102
}
@@ -116,7 +109,6 @@ extension RedisClient {
116109
/// - key: The key to set the expiration on.
117110
/// - timeout: The time from now the key will expire at.
118111
/// - Returns: `true` if the expiration was set.
119-
@inlinable
120112
public func expire(_ key: RedisKey, after timeout: TimeAmount) -> EventLoopFuture<Bool> {
121113
let args: [RESPValue] = [
122114
.init(bulk: key),
@@ -136,16 +128,15 @@ extension RedisClient {
136128
/// [https://redis.io/commands/scan](https://redis.io/commands/scan)
137129
/// - Parameters:
138130
/// - position: The cursor position to start from.
139-
/// - count: The number of elements to advance by. Redis default is 10.
140131
/// - match: A glob-style pattern to filter values to be selected from the result set.
132+
/// - count: The number of elements to advance by. Redis default is 10.
141133
/// - Returns: A cursor position for additional invocations with a limited collection of keys found in the database.
142-
@inlinable
143134
public func scan(
144135
startingFrom position: Int = 0,
145-
count: Int? = nil,
146-
matching match: String? = nil
136+
matching match: String? = nil,
137+
count: Int? = nil
147138
) -> EventLoopFuture<(Int, [String])> {
148-
return _scan(command: "SCAN", nil, position, count, match)
139+
return _scan(command: "SCAN", nil, position, match, count)
149140
}
150141

151142
@usableFromInline
@@ -154,8 +145,8 @@ extension RedisClient {
154145
resultType: T.Type = T.self,
155146
_ key: RedisKey?,
156147
_ pos: Int,
157-
_ count: Int?,
158-
_ match: String?
148+
_ match: String?,
149+
_ count: Int?
159150
) -> EventLoopFuture<(Int, T)>
160151
where
161152
T: RESPValueConvertible

0 commit comments

Comments
 (0)