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

Commit 02f86d5

Browse files
committed
K
1 parent 18b45c2 commit 02f86d5

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

src/lib.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,46 @@ use crate::redisjson::RedisJSON;
1010

1111
static REDIS_JSON_TYPE: RedisType = RedisType::new("RedisJSON");
1212

13+
#[derive(Debug, PartialEq)]
14+
pub enum SetOptions {
15+
NotExists,
16+
AlreadyExists,
17+
None
18+
}
19+
1320
fn json_set(ctx: &Context, args: Vec<String>) -> RedisResult {
1421
let mut args = args.into_iter().skip(1);
1522

1623
let key = args.next_string()?;
1724
let _path = args.next_string()?; // TODO handle this path
1825
let value = args.next_string()?;
26+
let option = match args.next() {
27+
Some(op) => {
28+
match op.as_str() {
29+
"NX" => SetOptions::NotExists,
30+
"XX" => SetOptions::AlreadyExists,
31+
_ => return Err("ERR syntax error".into())
32+
}
33+
}
34+
None => {
35+
SetOptions::None
36+
}
37+
};
1938

2039
let key = ctx.open_key_writable(&key);
2140

2241
match key.get_value::<RedisJSON>(&REDIS_JSON_TYPE)? {
23-
Some(doc) => {
42+
Some(ref mut doc) if option != SetOptions::NotExists => {
2443
doc.set_value(&value)?;
44+
REDIS_OK
2545
}
26-
None => {
46+
None if option != SetOptions::AlreadyExists => {
2747
let doc = RedisJSON::from_str(&value)?;
2848
key.set_value(&REDIS_JSON_TYPE, doc)?;
49+
REDIS_OK
2950
}
51+
_ => Ok(().into())
3052
}
31-
32-
REDIS_OK
3353
}
3454

3555
fn json_get(ctx: &Context, args: Vec<String>) -> RedisResult {

0 commit comments

Comments
 (0)