1
1
#[ macro_use]
2
2
extern crate redismodule;
3
3
4
- use redismodule:: { Context , RedisResult , NextArg } ;
4
+ use redismodule:: { Context , RedisResult , NextArg , REDIS_OK } ;
5
5
use redismodule:: native_types:: RedisType ;
6
6
7
7
mod redisjson;
@@ -11,10 +11,10 @@ use crate::redisjson::RedisJSON;
11
11
static REDIS_JSON_TYPE : RedisType = RedisType :: new ( "RedisJSON" ) ;
12
12
13
13
fn json_set ( ctx : & Context , args : Vec < String > ) -> RedisResult {
14
-
15
14
let mut args = args. into_iter ( ) . skip ( 1 ) ;
16
15
17
16
let key = args. next_string ( ) ?;
17
+ let _path = args. next_string ( ) ?; // TODO handle this path
18
18
let value = args. next_string ( ) ?;
19
19
20
20
let key = ctx. open_key_writable ( & key) ;
@@ -29,13 +29,33 @@ fn json_set(ctx: &Context, args: Vec<String>) -> RedisResult {
29
29
}
30
30
}
31
31
32
- Ok ( ( ) . into ( ) )
32
+ REDIS_OK
33
33
}
34
34
35
35
fn json_get ( ctx : & Context , args : Vec < String > ) -> RedisResult {
36
36
let mut args = args. into_iter ( ) . skip ( 1 ) ;
37
+
37
38
let key = args. next_string ( ) ?;
38
- let path = args. next_string ( ) ?;
39
+
40
+ let mut path = loop {
41
+ let arg = match args. next_string ( ) {
42
+ Ok ( s) => s. to_uppercase ( ) ,
43
+ Err ( _) => "$" . to_owned ( ) // path is optional
44
+ } ;
45
+
46
+ match arg. as_str ( ) {
47
+ "INDENT" => args. next ( ) , // TODO add support
48
+ "NEWLINE" => args. next ( ) , // TODO add support
49
+ "SPACE" => args. next ( ) , // TODO add support
50
+ "NOESCAPE" => continue , // TODO add support
51
+ "." => break String :: from ( "$" ) , // backward compatibility suuport
52
+ _ => break arg
53
+ } ;
54
+ } ;
55
+
56
+ if path. starts_with ( "." ) { // backward compatibility
57
+ path. insert ( 0 , '$' ) ;
58
+ }
39
59
40
60
let key = ctx. open_key_writable ( & key) ;
41
61
@@ -51,9 +71,9 @@ fn json_strlen(ctx: &Context, args: Vec<String>) -> RedisResult {
51
71
let mut args = args. into_iter ( ) . skip ( 1 ) ;
52
72
let key = args. next_string ( ) ?;
53
73
let path = args. next_string ( ) ?;
54
-
55
- let key = ctx. open_key_writable ( & key) ;
56
-
74
+
75
+ let key = ctx. open_key_writable ( & key) ;
76
+
57
77
let length = match key. get_value :: < RedisJSON > ( & REDIS_JSON_TYPE ) ? {
58
78
Some ( doc) => doc. str_len ( & path) ?. into ( ) ,
59
79
None => ( ) . into ( )
@@ -66,7 +86,7 @@ fn json_type(ctx: &Context, args: Vec<String>) -> RedisResult {
66
86
let mut args = args. into_iter ( ) . skip ( 1 ) ;
67
87
let key = args. next_string ( ) ?;
68
88
let path = args. next_string ( ) ?;
69
-
89
+
70
90
let key = ctx. open_key_writable ( & key) ;
71
91
72
92
let value = match key. get_value :: < RedisJSON > ( & REDIS_JSON_TYPE ) ? {
0 commit comments