Skip to content

Commit cad203b

Browse files
committed
Update command extensions
1 parent 65d0afd commit cad203b

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

Sources/NIORedis/Commands/BasicCommands.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
import Foundation
22
import NIO
33

4-
extension NIORedisConnection {
4+
extension RedisConnection {
55
/// Select the Redis logical database having the specified zero-based numeric index.
66
/// New connections always use the database 0.
77
///
88
/// https://redis.io/commands/select
99
public func select(_ id: Int) -> EventLoopFuture<Void> {
10-
return command("SELECT", [RESPValue(bulk: id.description)])
10+
return command("SELECT", arguments: [RESPValue(bulk: id.description)])
1111
.map { _ in return () }
1212
}
1313

1414
/// Request for authentication in a password-protected Redis server.
1515
///
1616
/// https://redis.io/commands/auth
1717
public func authorize(with password: String) -> EventLoopFuture<Void> {
18-
return command("AUTH", [RESPValue(bulk: password)]).map { _ in return () }
18+
return command("AUTH", arguments: [RESPValue(bulk: password)])
19+
.map { _ in return () }
1920
}
2021

2122
/// Removes the specified keys. A key is ignored if it does not exist.
@@ -24,7 +25,7 @@ extension NIORedisConnection {
2425
/// - Returns: A future number of keys that were removed.
2526
public func delete(_ keys: String...) -> EventLoopFuture<Int> {
2627
let keyArgs = keys.map { RESPValue(bulk: $0) }
27-
return command("DEL", keyArgs)
28+
return command("DEL", arguments: keyArgs)
2829
.thenThrowing { res in
2930
guard let count = res.int else {
3031
throw RedisError(identifier: "delete", reason: "Unexpected response: \(res)")
@@ -41,7 +42,7 @@ extension NIORedisConnection {
4142
/// - after: The lifetime (in seconds) the key will expirate at.
4243
/// - Returns: A future bool indicating if the expiration was set or not.
4344
public func expire(_ key: String, after deadline: Int) -> EventLoopFuture<Bool> {
44-
return command("EXPIRE", [RESPValue(bulk: key), RESPValue(bulk: deadline.description)])
45+
return command("EXPIRE", arguments: [RESPValue(bulk: key), RESPValue(bulk: deadline.description)])
4546
.thenThrowing { res in
4647
guard let value = res.int else {
4748
throw RedisError(identifier: "expire", reason: "Unexpected response: \(res)")
@@ -56,7 +57,7 @@ extension NIORedisConnection {
5657
///
5758
/// https://redis.io/commands/get
5859
public func get(_ key: String) -> EventLoopFuture<String?> {
59-
return command("GET", [RESPValue(bulk: key)])
60+
return command("GET", arguments: [RESPValue(bulk: key)])
6061
.map { return $0.string }
6162
}
6263

@@ -66,7 +67,7 @@ extension NIORedisConnection {
6667
///
6768
/// https://redis.io/commands/set
6869
public func set(_ key: String, to value: String) -> EventLoopFuture<Void> {
69-
return command("SET", [RESPValue(bulk: key), RESPValue(bulk: value)])
70+
return command("SET", arguments: [RESPValue(bulk: key), RESPValue(bulk: value)])
7071
.map { _ in return () }
7172
}
7273
}

Sources/NIORedis/RedisConnection.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,25 @@ public final class RedisConnection {
4141
/// - command: The command to execute.
4242
/// - arguments: The arguments to be sent with the command.
4343
/// - Returns: An `EventLoopFuture` that will resolve with the Redis command response.
44-
public func send(command: String, arguments: [RESPConvertible] = []) throws -> EventLoopFuture<RESPValue> {
44+
public func send(command: String, with arguments: [RESPConvertible] = []) throws -> EventLoopFuture<RESPValue> {
45+
let args = try arguments.map { try $0.convertToRESP() }
46+
return self.command(command, arguments: args)
47+
}
48+
49+
/// Invokes a command against Redis with the provided arguments.
50+
/// - Important: Arguments should be stored as `.bulkString`.
51+
/// - Parameters:
52+
/// - command: The command to execute.
53+
/// - arguments: The arguments to be sent with the command.
54+
/// - Returns: An `EventLoopFuture` that will resolve with the Redis command response.
55+
public func command(_ command: String, arguments: [RESPValue] = []) -> EventLoopFuture<RESPValue> {
4556
guard !isClosed.load() else {
4657
return channel.eventLoop.makeFailedFuture(error: RedisError.connectionClosed)
4758
}
4859

4960
let promise = channel.eventLoop.makePromise(of: RESPValue.self)
50-
let args = try arguments.map { try $0.convertToRESP() }
5161
let context = RedisCommandContext(
52-
command: .array([RESPValue(bulk: command)] + args),
62+
command: .array([RESPValue(bulk: command)] + arguments),
5363
promise: promise
5464
)
5565

Sources/NIORedis/RedisDriver.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,7 @@ public final class RedisDriver {
6666
return self.eventLoopGroup.next().makeSucceededFuture(result: connection)
6767
}
6868

69-
#warning("TODO - Fix command extensions")
70-
return self.eventLoopGroup.next().makeSucceededFuture(result: connection)
71-
// return connection.authorize(with: pw).map { _ in return connection }
69+
return connection.authorize(with: pw).map { _ in return connection }
7270
}
7371
}
7472
}

0 commit comments

Comments
 (0)