-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The PR adds Resp3 support for both, return result from module command and getting result as resp3 on `ctx.call_ext` (when resp3 option is used). Tests was added to verify both new functionalities. The PR also refactor the `CallReply` API introduced on #290. Instead of having 2 types of `CallReply` we now have one type such that the root call reply created with `'static` lifetime indicating that the user can hold it for as long as it want. Internal `CallReply` can outlive its father. As part of the refactoring, the `CallReply` was changed to be more rust friendly. `CallReply` was renamed to `CallResult = Result<CallReply, ErrorCallReply>`, `CallReply` is an enum which allows to check the `CallReply` type using a `match` statement.
- Loading branch information
Showing
15 changed files
with
1,129 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#[macro_use] | ||
extern crate redis_module; | ||
|
||
use redis_module::{ | ||
redisvalue::RedisValueKey, Context, NextArg, RedisError, RedisResult, RedisString, RedisValue, | ||
}; | ||
use std::collections::{HashMap, HashSet}; | ||
|
||
fn map_mget(ctx: &Context, args: Vec<RedisString>) -> RedisResult { | ||
if args.len() < 2 { | ||
return Err(RedisError::WrongArity); | ||
} | ||
|
||
let mut args = args.into_iter().skip(1); | ||
let key_name = args.next_arg()?; | ||
|
||
let fields: Vec<RedisString> = args.collect(); | ||
|
||
let key = ctx.open_key(&key_name); | ||
let values = key.hash_get_multi(&fields)?; | ||
let res = match values { | ||
None => RedisValue::Null, | ||
Some(values) => { | ||
let mut map: HashMap<RedisValueKey, RedisValue> = HashMap::with_capacity(fields.len()); | ||
for (field, value) in values.into_iter() { | ||
map.insert( | ||
RedisValueKey::BulkRedisString(field), | ||
RedisValue::BulkRedisString(value), | ||
); | ||
} | ||
RedisValue::Map(map) | ||
} | ||
}; | ||
|
||
Ok(res) | ||
} | ||
|
||
fn map_unique(ctx: &Context, args: Vec<RedisString>) -> RedisResult { | ||
if args.len() < 2 { | ||
return Err(RedisError::WrongArity); | ||
} | ||
|
||
let mut args = args.into_iter().skip(1); | ||
let key_name = args.next_arg()?; | ||
|
||
let fields: Vec<RedisString> = args.collect(); | ||
|
||
let key = ctx.open_key(&key_name); | ||
let values = key.hash_get_multi(&fields)?; | ||
let res = match values { | ||
None => RedisValue::Null, | ||
Some(values) => { | ||
let mut set: HashSet<RedisValueKey> = HashSet::new(); | ||
for (_, value) in values.into_iter() { | ||
set.insert(RedisValueKey::BulkRedisString(value)); | ||
} | ||
RedisValue::Set(set) | ||
} | ||
}; | ||
|
||
Ok(res) | ||
} | ||
|
||
////////////////////////////////////////////////////// | ||
|
||
redis_module! { | ||
name: "response", | ||
version: 1, | ||
data_types: [], | ||
commands: [ | ||
["map.mget", map_mget, "readonly", 1, 1, 1], | ||
["map.unique", map_unique, "readonly", 1, 1, 1], | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.