From b280af596dbe368b8f97a985effb9ca05f2c329b Mon Sep 17 00:00:00 2001 From: Ondrej Rafaj Date: Tue, 25 Jun 2019 19:05:48 +0000 Subject: [PATCH] Adding append --- Sources/RedisNIO/Commands/StringCommands.swift | 14 ++++++++++++++ .../Commands/StringCommandsTests.swift | 16 ++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/Sources/RedisNIO/Commands/StringCommands.swift b/Sources/RedisNIO/Commands/StringCommands.swift index 2f1317a..96cec98 100644 --- a/Sources/RedisNIO/Commands/StringCommands.swift +++ b/Sources/RedisNIO/Commands/StringCommands.swift @@ -89,6 +89,20 @@ extension RedisClient { .map { return $0 == 1 } } + /// Append a value to the end of an existing entry + /// - Note: If the key does not exist it is created and set as an empty string, so APPEND will be similar to SET in this special case. + /// + /// [https://redis.io/commands/append](https://redis.io/commands/append) + /// - Parameters: + /// - key: The key to use to uniquely identify this value. + /// - value: The value to append the key to. + /// - Returns: Integer with the new length of the value + @inlinable + public func append(_ key: String, to value: RESPValueConvertible) -> EventLoopFuture { + return send(command: "APPEND", with: [key, value]) + .convertFromRESPValue() + } + @usableFromInline func _mset( command: String, diff --git a/Tests/RedisNIOTests/Commands/StringCommandsTests.swift b/Tests/RedisNIOTests/Commands/StringCommandsTests.swift index b1d316e..61c72eb 100644 --- a/Tests/RedisNIOTests/Commands/StringCommandsTests.swift +++ b/Tests/RedisNIOTests/Commands/StringCommandsTests.swift @@ -53,10 +53,21 @@ final class StringCommandsTests: XCTestCase { XCTAssertEqual(try connection.mget(["empty", #function]).wait().count, 2) } - func test_set() { + func test_set() throws { XCTAssertNoThrow(try connection.set(#function, to: "value").wait()) + let val = try connection.get(#function).wait() + XCTAssertEqual(val, "value") } - + + func test_append() throws { + let result = "value appended" + XCTAssertNoThrow(try connection.append(#function, to: "value").wait()) + let length = try connection.append(#function, to: " appended").wait() + XCTAssertEqual(length, result.count) + let val = try connection.get(#function).wait() + XCTAssertEqual(val, result) + } + func test_mset() throws { let data = [ "first": 1, @@ -137,6 +148,7 @@ final class StringCommandsTests: XCTestCase { ("test_get", test_get), ("test_mget", test_mget), ("test_set", test_set), + ("test_append", test_append), ("test_mset", test_mset), ("test_msetnx", test_msetnx), ("test_increment", test_increment),