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;
1010
1111static REDIS_JSON_TYPE : RedisType = RedisType :: new ( "RedisJSON" ) ;
1212
13+ #[ derive( Debug , PartialEq ) ]
14+ pub enum SetOptions {
15+ NotExists ,
16+ AlreadyExists ,
17+ None
18+ }
19+
1320fn 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
3555fn json_get ( ctx : & Context , args : Vec < String > ) -> RedisResult {
You can’t perform that action at this time.
0 commit comments