Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add SafeEncoder.encode() method for complete encoding #2214

Merged
merged 6 commits into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/main/java/redis/clients/jedis/util/SafeEncoder.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package redis.clients.jedis.util;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import redis.clients.jedis.Protocol;
import redis.clients.jedis.exceptions.JedisDataException;
Expand Down Expand Up @@ -40,4 +42,28 @@ public static String encode(final byte[] data) {
throw new JedisException(e);
}
}

/**
* This method takes an object and will convert all bytes[] and list of byte[]
* and will encode the object in a recursive way.
* @param dataToEncode
* @return the object fully encoded
*/
public static Object encodeObject(Object dataToEncode) {
if (dataToEncode instanceof byte[]) {
return SafeEncoder.encode((byte[]) dataToEncode);
}

if (dataToEncode instanceof List) {
List arrayToDecode = (List)dataToEncode;
List returnValueArray = new ArrayList(arrayToDecode.size());
for (Object arrayEntry : arrayToDecode) {
// recursive call and add to list
returnValueArray.add(encodeObject(arrayEntry));
}
return returnValueArray;
}

return dataToEncode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import static redis.clients.jedis.Protocol.Command.HGETALL;
import static redis.clients.jedis.Protocol.Command.GET;
import static redis.clients.jedis.Protocol.Command.LRANGE;
import static redis.clients.jedis.Protocol.Command.PING;
import static redis.clients.jedis.Protocol.Command.RPUSH;
import static redis.clients.jedis.Protocol.Command.SET;
import static redis.clients.jedis.Protocol.Command.XINFO;
import static redis.clients.jedis.ScanParams.SCAN_POINTER_START;
import static redis.clients.jedis.ScanParams.SCAN_POINTER_START_BINARY;
import static redis.clients.jedis.params.SetParams.setParams;
Expand All @@ -31,6 +33,7 @@
import redis.clients.jedis.Protocol.Keyword;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;
import redis.clients.jedis.StreamEntryID;
import redis.clients.jedis.util.SafeEncoder;
import redis.clients.jedis.exceptions.JedisDataException;

Expand Down Expand Up @@ -868,4 +871,37 @@ public void sendCommandTest(){
assertEquals("PONG", SafeEncoder.encode((byte[]) jedis.sendCommand(PING)));
}


@Test
public void encodeCompleteResponse(){
HashMap<String,String> entry = new HashMap<>();
entry.put("foo", "bar");
jedis.xadd( "mystream", StreamEntryID.NEW_ENTRY, entry );
String status = jedis.xgroupCreate("mystream", "mygroup", null, false);

Object obj = jedis.sendCommand(XINFO, "STREAM", "mystream");
List encodeObj = (List)SafeEncoder.encodeObject(obj);

assertEquals( 14, encodeObj.size() );
assertEquals( "length", encodeObj.get(0) );
assertEquals( 1L, encodeObj.get(1) );

List entryAsList = new ArrayList(2);
entryAsList.add("foo");
entryAsList.add("bar");

assertEquals( entryAsList, ((List)encodeObj.get(11)).get(1) );

assertEquals("PONG", SafeEncoder.encodeObject(jedis.sendCommand(PING)));

entry.put("foo2", "bar2");
jedis.hset("hash:test:encode", entry);
encodeObj = (List)SafeEncoder.encodeObject(jedis.sendCommand(HGETALL, "hash:test:encode"));

assertEquals( 4, encodeObj.size() );
assertTrue(encodeObj.contains("foo"));
assertTrue(encodeObj.contains("foo2"));

}

}