@@ -16,11 +16,27 @@ pub enum SetOptions {
16
16
AlreadyExists ,
17
17
}
18
18
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
+
19
35
fn json_del ( ctx : & Context , args : Vec < String > ) -> RedisResult {
20
36
let mut args = args. into_iter ( ) . skip ( 1 ) ;
21
37
22
38
let key = args. next_string ( ) ?;
23
- let path = args. next_string ( ) ?;
39
+ let path = backward_path ( args. next_string ( ) ?) ;
24
40
25
41
let key = ctx. open_key_writable ( & key) ;
26
42
let deleted = match key. get_value :: < RedisJSON > ( & REDIS_JSON_TYPE ) ? {
@@ -34,7 +50,7 @@ fn json_set(ctx: &Context, args: Vec<String>) -> RedisResult {
34
50
let mut args = args. into_iter ( ) . skip ( 1 ) ;
35
51
36
52
let key = args. next_string ( ) ?;
37
- let path = args. next_string ( ) ?;
53
+ let path = backward_path ( args. next_string ( ) ?) ;
38
54
let value = args. next_string ( ) ?;
39
55
40
56
let set_option = args
@@ -76,19 +92,14 @@ fn json_get(ctx: &Context, args: Vec<String>) -> RedisResult {
76
92
} ;
77
93
78
94
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
84
99
_ => break arg,
85
100
} ;
86
101
} ;
87
-
88
- if path. starts_with ( "." ) {
89
- // backward compatibility
90
- path. insert ( 0 , '$' ) ;
91
- }
102
+ path = backward_path ( path) ;
92
103
93
104
let key = ctx. open_key_writable ( & key) ;
94
105
@@ -105,11 +116,7 @@ fn json_mget(ctx: &Context, args: Vec<String>) -> RedisResult {
105
116
return Err ( RedisError :: WrongArity ) ;
106
117
}
107
118
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 ( ) ) ;
113
120
let mut results: Vec < String > = Vec :: with_capacity ( args. len ( ) - 2 ) ;
114
121
for key in & args[ 1 ..args. len ( ) - 1 ] {
115
122
let redis_key = ctx. open_key_writable ( & key) ;
@@ -130,7 +137,7 @@ fn json_mget(ctx: &Context, args: Vec<String>) -> RedisResult {
130
137
fn json_str_len ( ctx : & Context , args : Vec < String > ) -> RedisResult {
131
138
let mut args = args. into_iter ( ) . skip ( 1 ) ;
132
139
let key = args. next_string ( ) ?;
133
- let path = args. next_string ( ) ?;
140
+ let path = backward_path ( args. next_string ( ) ?) ;
134
141
135
142
let key = ctx. open_key_writable ( & key) ;
136
143
@@ -145,7 +152,7 @@ fn json_str_len(ctx: &Context, args: Vec<String>) -> RedisResult {
145
152
fn json_type ( ctx : & Context , args : Vec < String > ) -> RedisResult {
146
153
let mut args = args. into_iter ( ) . skip ( 1 ) ;
147
154
let key = args. next_string ( ) ?;
148
- let path = args. next_string ( ) ?;
155
+ let path = backward_path ( args. next_string ( ) ?) ;
149
156
150
157
let key = ctx. open_key_writable ( & key) ;
151
158
@@ -170,7 +177,9 @@ fn json_num_powby(ctx: &Context, args: Vec<String>) -> RedisResult {
170
177
}
171
178
172
179
fn 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
+ {
174
183
let mut args = args. into_iter ( ) . skip ( 1 ) ;
175
184
176
185
let key = args. next_string ( ) ?;
0 commit comments