Skip to content

Commit 2bbe154

Browse files
authored
backward for json.get multi path (RedisJSON#68)
* backward for json.get multi path * add Path:new * update to redismodule-rs 0.5.0
1 parent c1ea9f6 commit 2bbe154

File tree

8 files changed

+68
-44
lines changed

8 files changed

+68
-44
lines changed

Cargo.lock

+20-20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "redisjson"
3-
version = "0.7.1"
3+
version = "0.8.0"
44
authors = ["Gavrie Philipson <gavrie@redislabs.com>"]
55
edition = "2018"
66

@@ -13,11 +13,11 @@ serde_json = "1.0"
1313
libc = "0.2"
1414
jsonpath_lib = { git="https://github.com/gkorland/jsonpath.git", branch="reaplce_with_ownership_parser" }
1515

16-
[dependencies.redismodule]
16+
[dependencies.redis-module]
1717
git = "https://github.com/redislabsmodules/redismodule-rs.git"
18-
branch = "safe_context_rm_call"
18+
tag = "v0.5.0"
1919
features = ["experimental-api"]
2020

2121
[dependencies.redisearch_api]
2222
git = "https://github.com/RediSearch/redisearch-api-rs.git"
23-
branch = "safe_context_rm_call"
23+
tag = "v0.2.0"

src/backward.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use redismodule::raw;
1+
use redis_module::raw;
22
use serde_json::map::Map;
33
use serde_json::Number;
44
use serde_json::Value;

src/commands/index.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::thread;
22

33
use serde_json::{Map, Value};
44

5-
use redismodule::{Context, NextArg, RedisError, RedisResult, RedisValue, REDIS_OK};
5+
use redis_module::{Context, NextArg, RedisError, RedisResult, RedisValue, REDIS_OK};
66

77
use redisearch_api::{Document, FieldType};
88

src/error.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ impl From<JsonPathError> for Error {
3131
}
3232
}
3333

34-
impl From<Error> for redismodule::RedisError {
34+
impl From<Error> for redis_module::RedisError {
3535
fn from(e: Error) -> Self {
36-
redismodule::RedisError::String(e.msg)
36+
redis_module::RedisError::String(e.msg)
3737
}
3838
}

src/lib.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#[macro_use]
2-
extern crate redismodule;
2+
extern crate redis_module;
33

4-
use redismodule::native_types::RedisType;
5-
use redismodule::raw::RedisModuleTypeMethods;
6-
use redismodule::{raw as rawmod, NextArg};
7-
use redismodule::{Context, RedisError, RedisResult, RedisValue, REDIS_OK};
4+
use redis_module::native_types::RedisType;
5+
use redis_module::raw::RedisModuleTypeMethods;
6+
use redis_module::{raw as rawmod, NextArg};
7+
use redis_module::{Context, RedisError, RedisResult, RedisValue, REDIS_OK};
88
use serde_json::{Number, Value};
99

1010
use std::{i64, usize};
@@ -20,13 +20,13 @@ mod schema; // TODO: Remove
2020
use crate::array_index::ArrayIndex;
2121
use crate::commands::index;
2222
use crate::error::Error;
23-
use crate::redisjson::{Format, RedisJSON, SetOptions};
23+
use crate::redisjson::{Format, Path, RedisJSON, SetOptions};
2424

2525
static REDIS_JSON_TYPE: RedisType = RedisType::new(
2626
"ReJSON-RL",
2727
2,
2828
RedisModuleTypeMethods {
29-
version: redismodule::TYPE_METHOD_VERSION,
29+
version: redis_module::TYPE_METHOD_VERSION,
3030

3131
rdb_load: Some(redisjson::type_methods::rdb_load),
3232
rdb_save: Some(redisjson::type_methods::rdb_save),
@@ -165,7 +165,7 @@ fn json_get(ctx: &Context, args: Vec<String>) -> RedisResult {
165165
let mut args = args.into_iter().skip(1);
166166
let key = args.next_string()?;
167167

168-
let mut paths: Vec<String> = vec![];
168+
let mut paths: Vec<Path> = vec![];
169169
let mut first_loop = true;
170170
let mut format = Format::JSON;
171171
loop {
@@ -174,7 +174,7 @@ fn json_get(ctx: &Context, args: Vec<String>) -> RedisResult {
174174
Err(_) => {
175175
// path is optional -> no path found on the first loop we use root "$"
176176
if first_loop {
177-
paths.push("$".to_owned());
177+
paths.push(Path::new("$".to_string()));
178178
}
179179
break;
180180
}
@@ -198,15 +198,15 @@ fn json_get(ctx: &Context, args: Vec<String>) -> RedisResult {
198198
format = Format::from_str(args.next_string()?.as_str())?;
199199
}
200200
_ => {
201-
paths.push(backwards_compat_path(arg));
201+
paths.push(Path::new(arg));
202202
}
203203
};
204204
}
205205

206206
let key = ctx.open_key_writable(&key);
207207
let value = match key.get_value::<RedisJSON>(&REDIS_JSON_TYPE)? {
208208
Some(doc) => if paths.len() == 1 {
209-
doc.to_string(&paths[0], format)?
209+
doc.to_string(&paths[0].fixed, format)?
210210
} else {
211211
// can't be smaller than 1
212212
doc.to_json(&mut paths)?

src/redisjson.rs

+28-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::nodevisitor::NodeVisitorImpl;
1010

1111
use bson::decode_document;
1212
use jsonpath_lib::SelectorMut;
13-
use redismodule::raw;
13+
use redis_module::raw;
1414
use serde_json::Value;
1515
use std::io::Cursor;
1616
use std::mem;
@@ -38,6 +38,30 @@ impl Format {
3838
}
3939
}
4040

41+
///
42+
/// Backwards compatibility convertor for RedisJSON 1.x clients
43+
///
44+
pub struct Path {
45+
pub path: String,
46+
pub fixed: String,
47+
}
48+
49+
impl Path {
50+
pub fn new(path: String) -> Path {
51+
let mut fixed = path.clone();
52+
if !fixed.starts_with("$") {
53+
if fixed == "." {
54+
fixed.replace_range(..1, "$");
55+
} else if fixed.starts_with(".") {
56+
fixed.insert(0, '$');
57+
} else {
58+
fixed.insert_str(0, "$.");
59+
}
60+
}
61+
Path{path, fixed}
62+
}
63+
}
64+
4165
#[derive(Debug)]
4266
pub struct RedisJSON {
4367
data: Value,
@@ -173,18 +197,18 @@ impl RedisJSON {
173197

174198
// FIXME: Implement this by manipulating serde_json::Value values,
175199
// and then using serde to serialize to JSON instead of doing it ourselves with strings.
176-
pub fn to_json(&self, paths: &mut Vec<String>) -> Result<String, Error> {
200+
pub fn to_json(&self, paths: &mut Vec<Path>) -> Result<String, Error> {
177201
let mut selector = jsonpath_lib::selector(&self.data);
178202
let mut result = paths.drain(..).fold(String::from("{"), |mut acc, path| {
179-
let value = match selector(&path) {
203+
let value = match selector(&path.fixed) {
180204
Ok(s) => match s.first() {
181205
Some(v) => v,
182206
None => &Value::Null,
183207
},
184208
Err(_) => &Value::Null,
185209
};
186210
acc.push('\"');
187-
acc.push_str(&path);
211+
acc.push_str(&path.path);
188212
acc.push_str("\":");
189213
acc.push_str(value.to_string().as_str());
190214
acc.push(',');

src/schema.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::os::raw::{c_int, c_void};
22

33
use redisearch_api::Index;
4-
use redismodule::raw;
4+
use redis_module::raw;
55

66
use crate::error::Error;
77
use std::collections::HashMap;

0 commit comments

Comments
 (0)