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

Commit 4774168

Browse files
committed
add del and set by path
1 parent 79d48d7 commit 4774168

File tree

4 files changed

+50
-31
lines changed

4 files changed

+50
-31
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 : usize = 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()),
@@ -117,8 +131,6 @@ fn json_mget(ctx: &Context, args: Vec<String>) -> RedisResult {
117131
} else {
118132
Err(RedisError::WrongArity)
119133
}
120-
121-
122134
}
123135

124136

@@ -162,6 +174,7 @@ redis_module! {
162174
],
163175
commands: [
164176
["json.set", json_set, "write"],
177+
["json.del", json_del, "write"],
165178
["json.get", json_get, ""],
166179
["json.mget", json_mget, ""],
167180
["json.strlen", json_strlen, ""],

src/redisjson.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,28 +43,35 @@ 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

56+
let v = jsonpath_lib::replace_with(self.data.clone(), path, &mut |_v| {
57+
json.clone()
58+
})?;
6059
self.data = v;
6160

6261
Ok(())
6362
}
6463

65-
pub fn to_string(&self, path: &str) -> Result<String, Error> {
66-
eprintln!("Serializing back to JSON");
64+
pub fn delete_path(&mut self, path: &str) -> Result<usize, Error> {
65+
self.data = jsonpath_lib::delete(self.data.clone(), path)?;
6766

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

0 commit comments

Comments
 (0)