11#[ macro_use]
22extern crate redismodule;
33
4- use redismodule:: { Context , RedisResult , NextArg } ;
4+ use redismodule:: { Context , RedisResult , NextArg , RedisValue } ;
55use redismodule:: native_types:: RedisType ;
66
77mod redisjson;
@@ -15,7 +15,7 @@ fn json_set(ctx: &Context, args: Vec<String>) -> RedisResult {
1515 let mut args = args. into_iter ( ) . skip ( 1 ) ;
1616
1717 let key = args. next_string ( ) ?;
18- let value = args. next_string ( ) ?;
18+ let value = args. next_string ( ) ?;
1919
2020 let key = ctx. open_key_writable ( & key) ;
2121
@@ -40,13 +40,28 @@ fn json_get(ctx: &Context, args: Vec<String>) -> RedisResult {
4040 let key = ctx. open_key_writable ( & key) ;
4141
4242 let value = match key. get_value :: < RedisJSON > ( & REDIS_JSON_TYPE ) ? {
43- Some ( doc) => { doc. to_string ( & path) ?. into ( ) }
43+ Some ( doc) => doc. to_string ( & path) ?. into ( ) ,
4444 None => ( ) . into ( )
4545 } ;
4646
4747 Ok ( value)
4848}
4949
50+ fn json_strlen ( ctx : & Context , args : Vec < String > ) -> RedisResult {
51+ let mut args = args. into_iter ( ) . skip ( 1 ) ;
52+ let key = args. next_string ( ) ?;
53+ let path = args. next_string ( ) ?;
54+
55+ let key = ctx. open_key_writable ( & key) ;
56+
57+ let length = match key. get_value :: < RedisJSON > ( & REDIS_JSON_TYPE ) ? {
58+ Some ( doc) => RedisValue :: Integer ( doc. str_len ( & path) ? as i64 ) ,
59+ None => ( ) . into ( )
60+ } ;
61+
62+ Ok ( length)
63+ }
64+
5065//////////////////////////////////////////////////////
5166
5267redis_module ! {
@@ -58,5 +73,6 @@ redis_module! {
5873 commands: [
5974 [ "json.set" , json_set, "write" ] ,
6075 [ "json.get" , json_get, "" ] ,
76+ [ "json.strlen" , json_strlen, "" ] ,
6177 ] ,
6278}
0 commit comments