Skip to content

Commit

Permalink
Fix set with args method signature #159
Browse files Browse the repository at this point in the history
Signature of the set method with SetArgs returned the value instead of the string status response. This mismatch gets obvious when using custom codes with value types other than String.

This commit changes the API to fix the mismatch.
  • Loading branch information
mp911de committed Dec 10, 2015
1 parent 7af7c9d commit 5a5c1cb
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ public RedisFuture<String> set(K key, V value) {
}

@Override
public RedisFuture<V> set(K key, V value, SetArgs setArgs) {
public RedisFuture<String> set(K key, V value, SetArgs setArgs) {
return dispatch(commandBuilder.set(key, value, setArgs));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ public Observable<String> set(K key, V value) {
}

@Override
public Observable<V> set(K key, V value, SetArgs setArgs) {
public Observable<String> set(K key, V value, SetArgs setArgs) {
return createObservable(() -> commandBuilder.set(key, value, setArgs));
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/lambdaworks/redis/RedisCommandBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -804,10 +804,10 @@ public Command<K, V, String> set(K key, V value) {
return createCommand(SET, new StatusOutput<K, V>(codec), key, value);
}

public Command<K, V, V> set(K key, V value, SetArgs setArgs) {
public Command<K, V, String> set(K key, V value, SetArgs setArgs) {
CommandArgs<K, V> args = new CommandArgs<K, V>(codec).addKey(key).addValue(value);
setArgs.build(args);
return createCommand(SET, new ValueOutput<K, V>(codec), args);
return createCommand(SET, new StatusOutput<K, V>(codec), args);
}

public Command<K, V, Long> setbit(K key, long offset, int value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public interface RedisStringsAsyncConnection<K, V> {
*
* @return RedisFuture&lt;V&gt; simple-string-reply {@code OK} if {@code SET} was executed correctly.
*/
RedisFuture<V> set(K key, V value, SetArgs setArgs);
RedisFuture<String> set(K key, V value, SetArgs setArgs);

/**
* Sets or clears the bit at offset in the string value stored at key.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,9 @@ public interface RedisStringsConnection<K, V> {
* @param value the value
* @param setArgs the setArgs
*
* @return V simple-string-reply {@code OK} if {@code SET} was executed correctly.
* @return String simple-string-reply {@code OK} if {@code SET} was executed correctly.
*/
V set(K key, V value, SetArgs setArgs);
String set(K key, V value, SetArgs setArgs);

/**
* Sets or clears the bit at offset in the string value stored at key.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,9 @@ public interface RedisStringAsyncCommands<K, V> {
* @param value the value
* @param setArgs the setArgs
*
* @return V simple-string-reply {@code OK} if {@code SET} was executed correctly.
* @return String simple-string-reply {@code OK} if {@code SET} was executed correctly.
*/
RedisFuture<V> set(K key, V value, SetArgs setArgs);
RedisFuture<String> set(K key, V value, SetArgs setArgs);

/**
* Sets or clears the bit at offset in the string value stored at key.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,9 @@ public interface RedisStringReactiveCommands<K, V> {
* @param value the value
* @param setArgs the setArgs
*
* @return V simple-string-reply {@code OK} if {@code SET} was executed correctly.
* @return String simple-string-reply {@code OK} if {@code SET} was executed correctly.
*/
Observable<V> set(K key, V value, SetArgs setArgs);
Observable<String> set(K key, V value, SetArgs setArgs);

/**
* Sets or clears the bit at offset in the string value stored at key.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,9 @@ public interface RedisStringCommands<K, V> {
* @param value the value
* @param setArgs the setArgs
*
* @return V simple-string-reply {@code OK} if {@code SET} was executed correctly.
* @return String simple-string-reply {@code OK} if {@code SET} was executed correctly.
*/
V set(K key, V value, SetArgs setArgs);
String set(K key, V value, SetArgs setArgs);

/**
* Sets or clears the bit at offset in the string value stored at key.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,9 @@ public interface NodeSelectionStringAsyncCommands<K, V> {
* @param value the value
* @param setArgs the setArgs
*
* @return V simple-string-reply {@code OK} if {@code SET} was executed correctly.
* @return String simple-string-reply {@code OK} if {@code SET} was executed correctly.
*/
AsyncExecutions<V> set(K key, V value, SetArgs setArgs);
AsyncExecutions<String> set(K key, V value, SetArgs setArgs);

/**
* Sets or clears the bit at offset in the string value stored at key.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,9 @@ public interface NodeSelectionStringCommands<K, V> {
* @param value the value
* @param setArgs the setArgs
*
* @return V simple-string-reply {@code OK} if {@code SET} was executed correctly.
* @return String simple-string-reply {@code OK} if {@code SET} was executed correctly.
*/
Executions<V> set(K key, V value, SetArgs setArgs);
Executions<String> set(K key, V value, SetArgs setArgs);

/**
* Sets or clears the bit at offset in the string value stored at key.
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/lambdaworks/redis/output/CommandOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ public void complete(int depth) {
}

protected String decodeAscii(ByteBuffer bytes) {
if(bytes == null) {
return null;
}

char[] chars = new char[bytes.remaining()];
for (int i = 0; i < chars.length; i++) {
chars[i] = (char) bytes.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,9 @@ public interface RedisStringCommands<K, V> {
* @param value the value
* @param setArgs the setArgs
*
* @return V simple-string-reply {@code OK} if {@code SET} was executed correctly.
* @return String simple-string-reply {@code OK} if {@code SET} was executed correctly.
*/
V set(K key, V value, SetArgs setArgs);
String set(K key, V value, SetArgs setArgs);

/**
* Sets or clears the bit at offset in the string value stored at key.
Expand Down
32 changes: 26 additions & 6 deletions src/test/java/com/lambdaworks/redis/CustomCodecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,46 @@
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.List;
import java.util.concurrent.TimeUnit;

import com.lambdaworks.redis.api.StatefulRedisConnection;
import com.lambdaworks.redis.api.sync.RedisCommands;
import com.lambdaworks.redis.protocol.RedisCommand;
import org.junit.Test;

import com.lambdaworks.redis.codec.ByteArrayCodec;
import com.lambdaworks.redis.codec.CompressionCodec;
import com.lambdaworks.redis.codec.RedisCodec;
import rx.observers.TestSubscriber;

public class CustomCodecTest extends AbstractRedisClientTest {

@Test
public void test() throws Exception {
public void testJavaSerializer() throws Exception {
StatefulRedisConnection<String, Object> redisConnection = client.connect(new SerializedObjectCodec());
RedisCommands<String, Object> sync = redisConnection.sync();
List<String> list = list("one", "two");
sync.set(key, list);

assertThat(sync.get(key)).isEqualTo(list);
assertThat(sync.set(key, list)).isEqualTo("OK");
assertThat(sync.set(key, list, SetArgs.Builder.ex(1))).isEqualTo("OK");

RedisCommands<String, Object> connection = client.connect(new SerializedObjectCodec()).sync();
redisConnection.close();
}

@Test
public void testJavaSerializerRx() throws Exception {
StatefulRedisConnection<String, Object> redisConnection = client.connect(new SerializedObjectCodec());
List<String> list = list("one", "two");
connection.set(key, list);
assertThat(connection.get(key)).isEqualTo(list);

connection.close();
TestSubscriber<String> subscriber = TestSubscriber.create();

redisConnection.reactive().set(key, list, SetArgs.Builder.ex(1)).subscribe(subscriber);
subscriber.awaitTerminalEvent(1, TimeUnit.SECONDS);
subscriber.assertCompleted();
subscriber.assertValue("OK");

redisConnection.close();
}

@Test
Expand Down

0 comments on commit 5a5c1cb

Please sign in to comment.