@@ -16,11 +16,27 @@ pub enum SetOptions {
1616 AlreadyExists ,
1717}
1818
19+ ///
20+ /// backward compatibility convector for RedisJSON 1.x clients
21+ ///
22+ fn backward_path ( mut path : String ) -> String {
23+ if !path. starts_with ( "$" ) {
24+ if path == "." {
25+ path. replace_range ( ..1 , "$" ) ;
26+ } else if path. starts_with ( "." ) {
27+ path. insert ( 0 , '$' ) ;
28+ } else {
29+ path. insert_str ( 0 , "$." ) ;
30+ }
31+ }
32+ return path;
33+ }
34+
1935fn json_del ( ctx : & Context , args : Vec < String > ) -> RedisResult {
2036 let mut args = args. into_iter ( ) . skip ( 1 ) ;
2137
2238 let key = args. next_string ( ) ?;
23- let path = args. next_string ( ) ?;
39+ let path = backward_path ( args. next_string ( ) ?) ;
2440
2541 let key = ctx. open_key_writable ( & key) ;
2642 let deleted = match key. get_value :: < RedisJSON > ( & REDIS_JSON_TYPE ) ? {
@@ -34,7 +50,7 @@ fn json_set(ctx: &Context, args: Vec<String>) -> RedisResult {
3450 let mut args = args. into_iter ( ) . skip ( 1 ) ;
3551
3652 let key = args. next_string ( ) ?;
37- let path = args. next_string ( ) ?;
53+ let path = backward_path ( args. next_string ( ) ?) ;
3854 let value = args. next_string ( ) ?;
3955
4056 let set_option = args
@@ -76,19 +92,14 @@ fn json_get(ctx: &Context, args: Vec<String>) -> RedisResult {
7692 } ;
7793
7894 match arg. as_str ( ) {
79- "INDENT" => args. next ( ) , // TODO add support
80- "NEWLINE" => args. next ( ) , // TODO add support
81- "SPACE" => args. next ( ) , // TODO add support
82- "NOESCAPE" => continue , // TODO add support
83- "." => break String :: from ( "$" ) , // backward compatibility support
95+ "INDENT" => args. next ( ) , // TODO add support
96+ "NEWLINE" => args. next ( ) , // TODO add support
97+ "SPACE" => args. next ( ) , // TODO add support
98+ "NOESCAPE" => continue , // TODO add support
8499 _ => break arg,
85100 } ;
86101 } ;
87-
88- if path. starts_with ( "." ) {
89- // backward compatibility
90- path. insert ( 0 , '$' ) ;
91- }
102+ path = backward_path ( path) ;
92103
93104 let key = ctx. open_key_writable ( & key) ;
94105
@@ -105,11 +116,7 @@ fn json_mget(ctx: &Context, args: Vec<String>) -> RedisResult {
105116 return Err ( RedisError :: WrongArity ) ;
106117 }
107118 if let Some ( path) = args. last ( ) {
108- let mut path = path. clone ( ) ;
109- if path. starts_with ( "." ) {
110- // backward compatibility
111- path. insert ( 0 , '$' ) ;
112- }
119+ let path = backward_path ( path. to_string ( ) ) ;
113120 let mut results: Vec < String > = Vec :: with_capacity ( args. len ( ) - 2 ) ;
114121 for key in & args[ 1 ..args. len ( ) - 1 ] {
115122 let redis_key = ctx. open_key_writable ( & key) ;
@@ -130,7 +137,7 @@ fn json_mget(ctx: &Context, args: Vec<String>) -> RedisResult {
130137fn json_str_len ( ctx : & Context , args : Vec < String > ) -> RedisResult {
131138 let mut args = args. into_iter ( ) . skip ( 1 ) ;
132139 let key = args. next_string ( ) ?;
133- let path = args. next_string ( ) ?;
140+ let path = backward_path ( args. next_string ( ) ?) ;
134141
135142 let key = ctx. open_key_writable ( & key) ;
136143
@@ -145,7 +152,7 @@ fn json_str_len(ctx: &Context, args: Vec<String>) -> RedisResult {
145152fn json_type ( ctx : & Context , args : Vec < String > ) -> RedisResult {
146153 let mut args = args. into_iter ( ) . skip ( 1 ) ;
147154 let key = args. next_string ( ) ?;
148- let path = args. next_string ( ) ?;
155+ let path = backward_path ( args. next_string ( ) ?) ;
149156
150157 let key = ctx. open_key_writable ( & key) ;
151158
@@ -170,7 +177,9 @@ fn json_num_powby(ctx: &Context, args: Vec<String>) -> RedisResult {
170177}
171178
172179fn json_num_op < F > ( ctx : & Context , args : Vec < String > , fun : F ) -> RedisResult
173- where F : Fn ( f64 , f64 ) -> f64 {
180+ where
181+ F : Fn ( f64 , f64 ) -> f64 ,
182+ {
174183 let mut args = args. into_iter ( ) . skip ( 1 ) ;
175184
176185 let key = args. next_string ( ) ?;
0 commit comments