'Converting a lua table to a java `Map` with Spring Data Redis

I am trying to run a lua script which eventually returns a lua table, then convert it as java Map with Spring Data Redis. When the result is array style lua table, such as {"a","b","c"}, it is possible to convert it to a List<String>. However, when the lua table result has key as {"a":"A", "b":"B"}, it is not possible to convert it to Map<String, String>.

for both cases I am using RedisTemplate#execute(RedisScript<T> script, List<K> keys, Object... args), and DefaultRedisScript<> as the first parameter.

lua table(array style) -> List.class - working

lua script (simplified)

local result = {}

for i=1, 3, 1 do
    result[i]="test"
end

return result

java code

@Test
public void luaTableArrayStyleConvert2ListTest(){
    List<String> keys = ...some keys...;
    DefaultRedisScript<List> script = new DefaultRedisScript<>();
    script.setResultType(List.class)
    script.setScriptTest(...lua script above...)
    List<String> result = redisTemplate.execute(script, keys); //{"test","test","test"}
}

lua table(key-value) -> Map.class - not working!

lua script (simplified)

local result = {}

for i=1, 3, 1 do
    result["test"]="test"
end

return result

java code

@Test
public void luaTableArrayStyleConvert2ListTest(){
    List<String> keys = ...some keys...;
    DefaultRedisScript<Map> script = new DefaultRedisScript<>();
    script.setResultType(Map.class)
    script.setScriptTest(...lua script above...)
    Map<String,String> result = redisTemplate.execute(script, keys); //null!!
}

I understand that org.springframework.data.redis.connection.ReturnType clearly shows that Map.class is not considered as a return type of Redis scripting command. However I hope if some one has experience of converting a lua table to a java Map.

Thanks.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source