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

Commit 4713f57

Browse files
authored
Merge pull request #20 from RedisLabsModules/strlen
Add support for str_len
2 parents 4f9df61 + 7988d5a commit 4713f57

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

src/lib.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn json_set(ctx: &Context, args: Vec<String>) -> RedisResult {
1515
let mut args = args.into_iter().skip(1);
1616

1717
let key = args.next_string()?;
18-
let value = args.next_string()?;
18+
let value = args.next_string()?;
1919

2020
let key = ctx.open_key_writable(&key);
2121

@@ -40,22 +40,37 @@ fn json_get(ctx: &Context, args: Vec<String>) -> RedisResult {
4040
let key = ctx.open_key_writable(&key);
4141

4242
let value = match key.get_value::<RedisJSON>(&REDIS_JSON_TYPE)? {
43-
Some(doc) => { doc.to_string(&path)?.into() }
43+
Some(doc) => doc.to_string(&path)?.into(),
4444
None => ().into()
4545
};
4646

4747
Ok(value)
4848
}
4949

50-
fn json_type(ctx: &Context, args: Vec<String>) -> RedisResult {
50+
fn json_strlen(ctx: &Context, args: Vec<String>) -> RedisResult {
5151
let mut args = args.into_iter().skip(1);
5252
let key = args.next_string()?;
5353
let path = args.next_string()?;
54+
55+
let key = ctx.open_key_writable(&key);
56+
57+
let length = match key.get_value::<RedisJSON>(&REDIS_JSON_TYPE)? {
58+
Some(doc) => doc.str_len(&path)?.into(),
59+
None => ().into()
60+
};
61+
62+
Ok(length)
63+
}
5464

65+
fn json_type(ctx: &Context, args: Vec<String>) -> RedisResult {
66+
let mut args = args.into_iter().skip(1);
67+
let key = args.next_string()?;
68+
let path = args.next_string()?;
69+
5570
let key = ctx.open_key_writable(&key);
5671

5772
let value = match key.get_value::<RedisJSON>(&REDIS_JSON_TYPE)? {
58-
Some(doc) => { doc.get_type(&path)?.into() }
73+
Some(doc) => doc.get_type(&path)?.into(),
5974
None => ().into()
6075
};
6176

@@ -73,6 +88,7 @@ redis_module! {
7388
commands: [
7489
["json.set", json_set, "write"],
7590
["json.get", json_get, ""],
91+
["json.strlen", json_strlen, ""],
7692
["json.type", json_type, ""],
7793
],
7894
}

src/redisjson.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,24 @@ impl RedisJSON {
5252
pub fn to_string(&self, path: &str) -> Result<String, Error> {
5353
eprintln!("Serializing back to JSON");
5454

55-
// Create a JSONPath selector
56-
let selector = Selector::new(path).map_err(|e| Error {
57-
msg: format!("{}", e),
58-
})?;
59-
60-
let s = match selector.find(&self.data).next() {
55+
let s = match self.get_doc(path)? {
6156
Some(doc) => serde_json::to_string(&doc)?,
6257
None => String::new()
6358
};
6459

65-
return Ok(s)
60+
Ok(s)
61+
}
62+
63+
pub fn str_len(&self, path: &str) -> Result<usize, Error> {
64+
match self.get_doc(path)? {
65+
Some(doc) => {
66+
match doc.as_str() {
67+
Some(s) => Ok(s.len()),
68+
None => Err(Error{msg: "ERR wrong type of path value".to_string()})
69+
}
70+
}
71+
None => Ok(0) // path not found
72+
}
6673
}
6774

6875
pub fn get_type(&self, path: &str) -> Result<String, Error> {

0 commit comments

Comments
 (0)