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

Commit ca3605d

Browse files
authored
Merge pull request #27 from RedisLabsModules/del_replace
Del replace
2 parents 42c0a36 + bd64266 commit ca3605d

File tree

4 files changed

+54
-33
lines changed

4 files changed

+54
-33
lines changed

Cargo.lock

Lines changed: 16 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ crate-type = ["cdylib"]
1010
[dependencies]
1111
serde_json = "1.0"
1212
libc = "0.2"
13-
jsonpath_lib = "0.2.0"
14-
redismodule = { path = "redismodule-rs", version = "0.1.0" }
13+
jsonpath_lib = "0.2.3"
14+
redismodule = { path = "redismodule-rs", version = "0.1.0" }

src/lib.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,25 @@ pub enum SetOptions {
1616
AlreadyExists,
1717
}
1818

19+
fn json_del(ctx: &Context, args: Vec<String>) -> RedisResult {
20+
let mut args = args.into_iter().skip(1);
21+
22+
let key = args.next_string()?;
23+
let path = args.next_string()?;
24+
25+
let key = ctx.open_key_writable(&key);
26+
let deleted = match key.get_value::<RedisJSON>(&REDIS_JSON_TYPE)? {
27+
Some(doc) => doc.delete_path(&path)?,
28+
None => 0
29+
};
30+
Ok(deleted.into())
31+
}
32+
1933
fn json_set(ctx: &Context, args: Vec<String>) -> RedisResult {
2034
let mut args = args.into_iter().skip(1);
2135

2236
let key = args.next_string()?;
23-
let _path = args.next_string()?; // TODO handle this path
37+
let path = args.next_string()?;
2438
let value = args.next_string()?;
2539

2640
let set_option = args.next()
@@ -39,7 +53,7 @@ fn json_set(ctx: &Context, args: Vec<String>) -> RedisResult {
3953
match (current, set_option) {
4054
(Some(_), Some(SetOptions::NotExists)) => Ok(().into()),
4155
(Some(ref mut doc), _) => {
42-
doc.set_value(&value)?;
56+
doc.set_value(&value, &path)?;
4357
REDIS_OK
4458
}
4559
(None, Some(SetOptions::AlreadyExists)) => Ok(().into()),
@@ -112,8 +126,6 @@ fn json_mget(ctx: &Context, args: Vec<String>) -> RedisResult {
112126
} else {
113127
Err(RedisError::WrongArity)
114128
}
115-
116-
117129
}
118130

119131

@@ -157,6 +169,7 @@ redis_module! {
157169
],
158170
commands: [
159171
["json.set", json_set, "write"],
172+
["json.del", json_del, "write"],
160173
["json.get", json_get, ""],
161174
["json.mget", json_mget, ""],
162175
["json.strlen", json_strlen, ""],

src/redisjson.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Translate between JSON and tree of Redis objects:
44
// User-provided JSON is converted to a tree. This tree is stored transparently in Redis.
55
// It can be operated on (e.g. INCR) and serialized back to JSON.
6-
6+
use std::mem;
77
use serde_json::Value;
88
use jsonpath_lib::{JsonPathError};
99

@@ -43,28 +43,37 @@ pub struct RedisJSON {
4343

4444
impl RedisJSON {
4545
pub fn from_str(data: &str) -> Result<Self, Error> {
46-
eprintln!("Parsing JSON from input '{}'", data);
47-
4846
// Parse the string of data into serde_json::Value.
4947
let v: Value = serde_json::from_str(data)?;
5048

5149
Ok(Self { data: v })
5250
}
5351

54-
pub fn set_value(&mut self, data: &str) -> Result<(), Error> {
55-
eprintln!("Parsing JSON from input '{}'", data);
56-
52+
pub fn set_value(&mut self, data: &str, path: &str) -> Result<(), Error> {
5753
// Parse the string of data into serde_json::Value.
58-
let v: Value = serde_json::from_str(data)?;
54+
let json: Value = serde_json::from_str(data)?;
5955

60-
self.data = v;
56+
let current_data = mem::replace(&mut self.data, Value::Null);
57+
let new_data = jsonpath_lib::replace_with(current_data, path, &mut |_v| {
58+
json.clone()
59+
})?;
60+
self.data = new_data;
6161

6262
Ok(())
6363
}
6464

65-
pub fn to_string(&self, path: &str) -> Result<String, Error> {
66-
eprintln!("Serializing back to JSON");
65+
pub fn delete_path(&mut self, path: &str) -> Result<usize, Error> {
66+
let current_value = mem::replace(&mut self.data, Value::Null);
67+
self.data = jsonpath_lib::delete(current_value, path)?;
6768

69+
let res : usize = match self.data {
70+
Value::Null => 0,
71+
_ => 1
72+
};
73+
Ok(res)
74+
}
75+
76+
pub fn to_string(&self, path: &str) -> Result<String, Error> {
6877
let results = self.get_doc(path)?;
6978
Ok(serde_json::to_string(&results)?)
7079
}

0 commit comments

Comments
 (0)