This repository was archived by the owner on Apr 20, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +24
-4
lines changed Expand file tree Collapse file tree 1 file changed +24
-4
lines changed Original file line number Diff line number Diff line change @@ -10,26 +10,46 @@ use crate::redisjson::RedisJSON;
10
10
11
11
static REDIS_JSON_TYPE : RedisType = RedisType :: new ( "RedisJSON" ) ;
12
12
13
+ #[ derive( Debug , PartialEq ) ]
14
+ pub enum SetOptions {
15
+ NotExists ,
16
+ AlreadyExists ,
17
+ None
18
+ }
19
+
13
20
fn json_set ( ctx : & Context , args : Vec < String > ) -> RedisResult {
14
21
let mut args = args. into_iter ( ) . skip ( 1 ) ;
15
22
16
23
let key = args. next_string ( ) ?;
17
24
let _path = args. next_string ( ) ?; // TODO handle this path
18
25
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
+ } ;
19
38
20
39
let key = ctx. open_key_writable ( & key) ;
21
40
22
41
match key. get_value :: < RedisJSON > ( & REDIS_JSON_TYPE ) ? {
23
- Some ( doc) => {
42
+ Some ( ref mut doc) if option != SetOptions :: NotExists => {
24
43
doc. set_value ( & value) ?;
44
+ REDIS_OK
25
45
}
26
- None => {
46
+ None if option != SetOptions :: AlreadyExists => {
27
47
let doc = RedisJSON :: from_str ( & value) ?;
28
48
key. set_value ( & REDIS_JSON_TYPE , doc) ?;
49
+ REDIS_OK
29
50
}
51
+ _ => Ok ( ( ) . into ( ) )
30
52
}
31
-
32
- REDIS_OK
33
53
}
34
54
35
55
fn json_get ( ctx : & Context , args : Vec < String > ) -> RedisResult {
You can’t perform that action at this time.
0 commit comments