Skip to content

Commit 35a4fa0

Browse files
authored
Merge pull request #16 from Mordil/general-commands
General commands
2 parents 5c32b97 + 9f505be commit 35a4fa0

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

Sources/NIORedis/Commands/BasicCommands.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,40 @@ extension RedisConnection {
7070
return command("SET", arguments: [RESPValue(bulk: key), RESPValue(bulk: value)])
7171
.map { _ in return () }
7272
}
73+
74+
/// Echos the provided message through the Redis instance.
75+
/// - Parameter message: The message to echo.
76+
/// - Returns: The message sent with the command.
77+
public func echo(_ message: String) -> EventLoopFuture<String> {
78+
return send(command: "ECHO", with: [message])
79+
.flatMapThrowing {
80+
guard let response = $0.string else { throw RedisError.respConversion(to: String.self) }
81+
return response
82+
}
83+
}
84+
85+
/// Pings the server, which will respond with a message.
86+
/// - Parameter with: The optional message that the server should respond with.
87+
/// - Returns: The provided message or Redis' default response of `"PONG"`.
88+
public func ping(with message: String? = nil) -> EventLoopFuture<String> {
89+
let arg = message != nil ? [message] : []
90+
return send(command: "PING", with: arg)
91+
.flatMapThrowing {
92+
guard let response = $0.string else { throw RedisError.respConversion(to: String.self) }
93+
return response
94+
}
95+
}
96+
97+
/// Swaps the data of two Redis database by their index ID.
98+
/// - Parameters:
99+
/// - firstIndex: The index of the first database.
100+
/// - secondIndex: The index of the second database.
101+
/// - Returns: `true` if the swap was successful.
102+
public func swapdb(firstIndex: Int, secondIndex: Int) -> EventLoopFuture<Bool> {
103+
return send(command: "SWAPDB", with: [firstIndex, secondIndex])
104+
.flatMapThrowing {
105+
guard let response = $0.string else { throw RedisError.respConversion(to: String.self) }
106+
return response == "OK"
107+
}
108+
}
73109
}

Sources/NIORedis/RESP/RESPValueConvertible.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,20 @@ extension Array: RESPValueConvertible where Element: RESPValueConvertible {
121121
return RESPValue.array(elements)
122122
}
123123
}
124+
125+
extension Optional: RESPValueConvertible where Wrapped: RESPValueConvertible {
126+
public init?(_ value: RESPValue) {
127+
guard !value.isNull else { return nil }
128+
guard let wrapped = Wrapped(value) else { return nil }
129+
130+
self = .some(wrapped)
131+
}
132+
133+
/// See `RESPValueConvertible.convertedToRESPValue()`.
134+
public func convertedToRESPValue() -> RESPValue {
135+
switch self {
136+
case .none: return .null
137+
case let .some(value): return value.convertedToRESPValue()
138+
}
139+
}
140+
}

Tests/NIORedisTests/Commands/BasicCommandsTests.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,50 @@ final class BasicCommandsTests: XCTestCase {
6363
XCTAssertNil(after)
6464
}
6565

66+
func test_ping() throws {
67+
let first = try connection?.ping().wait()
68+
XCTAssertEqual(first, "PONG")
69+
70+
let second = try connection?.ping(with: "My message").wait()
71+
XCTAssertEqual(second, "My message")
72+
}
73+
74+
func test_echo() throws {
75+
let response = try connection?.echo("FIZZ_BUZZ").wait()
76+
XCTAssertEqual(response, "FIZZ_BUZZ")
77+
}
78+
79+
func test_swapdb() throws {
80+
try connection?.set("first", to: "3").wait()
81+
var first = try connection?.get("first").wait()
82+
XCTAssertEqual(first, "3")
83+
84+
try connection?.select(1).wait()
85+
var second = try connection?.get("first").wait()
86+
XCTAssertEqual(second, nil)
87+
88+
try connection?.set("second", to: "100").wait()
89+
second = try connection?.get("second").wait()
90+
XCTAssertEqual(second, "100")
91+
92+
let success = try connection?.swapdb(firstIndex: 0, secondIndex: 1).wait()
93+
XCTAssertEqual(success, true)
94+
95+
second = try connection?.get("first").wait()
96+
XCTAssertEqual(second, "3")
97+
98+
try connection?.select(0).wait()
99+
first = try connection?.get("second").wait()
100+
XCTAssertEqual(first, "100")
101+
}
102+
66103
static var allTests = [
67104
("test_select", test_select),
68105
("test_set", test_set),
69106
("test_get", test_get),
70107
("test_expire", test_expire),
71108
("test_delete", test_delete),
109+
("test_ping", test_ping),
110+
("test_echo", test_echo),
72111
]
73112
}

0 commit comments

Comments
 (0)