RedisLabsModules / redismodule-rs

Rust API for Redis Modules API
BSD 3-Clause "New" or "Revised" License
265 stars 62 forks source link

Incorrect Return Type from `Context::call()` #234

Open phartleyp opened 2 years ago

phartleyp commented 2 years ago

The following Redis Module command simply wraps the Redis "GET" command and returns the value of the simple string key with the name "key". It contains a bug for some string values.

pub fn cmd_example(ctx: &Context, _args: Vec<RedisString>) -> RedisResult {
    ctx.call("GET", &["key"])
}

Root cause is that Context::call() always returns strings as RedisValue::SimpleString. But strings containing \r\n should be RedisValue::BulkString.

phartleyp commented 2 years ago

This is fixed by pull request https://github.com/RedisLabsModules/redismodule-rs/pull/93.

There seems to be some hesitation by maintainers to implement the fix. Until then, here is a workaround that can be applied to the output of Context::call() that will work around this issue:

/// Remap `Context::call()` results to convert `SimpleString` into `BulkString`.
/// All other types are left alone.
pub fn fix_call_reply(result: RedisValue) -> RedisValue {
    match result {
        RedisValue::SimpleString(v) => RedisValue::BulkString(v),
        RedisValue::Array(a) => {
            RedisValue::Array(a.into_iter().map(|v| fix_call_reply(v)).collect())
        }
        v @ _ => v,
    }
}
oshadmi commented 2 years ago

Related #200