Skip to content
This repository was archived by the owner on Apr 20, 2020. It is now read-only.

Commit 18b45c2

Browse files
authored
Merge pull request #22 from RedisLabsModules/backport-compatibility
add backward compatibility with RedisJSON
2 parents 4713f57 + b9080f3 commit 18b45c2

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

src/lib.rs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#[macro_use]
22
extern crate redismodule;
33

4-
use redismodule::{Context, RedisResult, NextArg};
4+
use redismodule::{Context, RedisResult, NextArg, REDIS_OK};
55
use redismodule::native_types::RedisType;
66

77
mod redisjson;
@@ -11,10 +11,10 @@ use crate::redisjson::RedisJSON;
1111
static REDIS_JSON_TYPE: RedisType = RedisType::new("RedisJSON");
1212

1313
fn json_set(ctx: &Context, args: Vec<String>) -> RedisResult {
14-
1514
let mut args = args.into_iter().skip(1);
1615

1716
let key = args.next_string()?;
17+
let _path = args.next_string()?; // TODO handle this path
1818
let value = args.next_string()?;
1919

2020
let key = ctx.open_key_writable(&key);
@@ -29,13 +29,33 @@ fn json_set(ctx: &Context, args: Vec<String>) -> RedisResult {
2929
}
3030
}
3131

32-
Ok(().into())
32+
REDIS_OK
3333
}
3434

3535
fn json_get(ctx: &Context, args: Vec<String>) -> RedisResult {
3636
let mut args = args.into_iter().skip(1);
37+
3738
let key = args.next_string()?;
38-
let path = args.next_string()?;
39+
40+
let mut path = loop {
41+
let arg = match args.next_string() {
42+
Ok(s) => s.to_uppercase(),
43+
Err(_) => "$".to_owned() // path is optional
44+
};
45+
46+
match arg.as_str() {
47+
"INDENT" => args.next(), // TODO add support
48+
"NEWLINE" => args.next(), // TODO add support
49+
"SPACE" => args.next(), // TODO add support
50+
"NOESCAPE" => continue, // TODO add support
51+
"." => break String::from("$"), // backward compatibility suuport
52+
_ => break arg
53+
};
54+
};
55+
56+
if path.starts_with(".") { // backward compatibility
57+
path.insert(0, '$');
58+
}
3959

4060
let key = ctx.open_key_writable(&key);
4161

@@ -51,9 +71,9 @@ fn json_strlen(ctx: &Context, args: Vec<String>) -> RedisResult {
5171
let mut args = args.into_iter().skip(1);
5272
let key = args.next_string()?;
5373
let path = args.next_string()?;
54-
55-
let key = ctx.open_key_writable(&key);
56-
74+
75+
let key = ctx.open_key_writable(&key);
76+
5777
let length = match key.get_value::<RedisJSON>(&REDIS_JSON_TYPE)? {
5878
Some(doc) => doc.str_len(&path)?.into(),
5979
None => ().into()
@@ -66,7 +86,7 @@ fn json_type(ctx: &Context, args: Vec<String>) -> RedisResult {
6686
let mut args = args.into_iter().skip(1);
6787
let key = args.next_string()?;
6888
let path = args.next_string()?;
69-
89+
7090
let key = ctx.open_key_writable(&key);
7191

7292
let value = match key.get_value::<RedisJSON>(&REDIS_JSON_TYPE)? {

0 commit comments

Comments
 (0)