1
+ use crate :: error:: Error ;
1
2
use crate :: formatter:: RedisJsonFormatter ;
3
+ use crate :: manager:: err_msg_json_path_doesnt_exist_with_param;
4
+ use crate :: manager:: { err_msg_json_expected, err_msg_json_path_doesnt_exist_with_param_or} ;
2
5
use crate :: manager:: { AddUpdateInfo , Manager , ReadHolder , SetUpdateInfo , UpdateInfo , WriteHolder } ;
6
+ use crate :: nodevisitor:: { StaticPathElement , StaticPathParser , VisitStatus } ;
3
7
use crate :: redisjson:: { normalize_arr_indices, Format , Path } ;
4
8
use jsonpath_lib:: select:: select_value:: { SelectValue , SelectValueType } ;
9
+ use jsonpath_lib:: select:: Selector ;
5
10
use redis_module:: { Context , RedisValue } ;
6
11
use redis_module:: { NextArg , RedisError , RedisResult , RedisString , REDIS_OK } ;
7
12
use std:: cmp:: Ordering ;
8
13
9
- use jsonpath_lib:: select:: Selector ;
10
-
11
- use crate :: nodevisitor:: { StaticPathElement , StaticPathParser , VisitStatus } ;
12
-
13
- use crate :: error:: Error ;
14
-
15
14
use crate :: redisjson:: SetOptions ;
16
15
17
16
use serde_json:: { Number , Value } ;
@@ -87,7 +86,9 @@ impl<'a, V: SelectValue> KeyValue<'a, V> {
87
86
let results = self . get_values ( path) ?;
88
87
match results. first ( ) {
89
88
Some ( s) => Ok ( s) ,
90
- None => Err ( "ERR path does not exist" . into ( ) ) ,
89
+ None => Err ( err_msg_json_path_doesnt_exist_with_param ( path)
90
+ . as_str ( )
91
+ . into ( ) ) ,
91
92
}
92
93
}
93
94
@@ -104,10 +105,9 @@ impl<'a, V: SelectValue> KeyValue<'a, V> {
104
105
. collect :: < Vec < RedisValue > > ( )
105
106
. into ( ) )
106
107
} else {
107
- Err ( RedisError :: String ( format ! (
108
- "Path '{}' does not exist" ,
109
- path. get_path( )
110
- ) ) )
108
+ Err ( RedisError :: String (
109
+ err_msg_json_path_doesnt_exist_with_param ( path. get_original ( ) ) ,
110
+ ) )
111
111
}
112
112
}
113
113
}
@@ -204,7 +204,7 @@ impl<'a, V: SelectValue> KeyValue<'a, V> {
204
204
acc
205
205
} ) ;
206
206
if let Some ( p) = missing_path {
207
- return Err ( format ! ( "ERR path {} does not exist" , p ) . into ( ) ) ;
207
+ return Err ( err_msg_json_path_doesnt_exist_with_param ( p . as_str ( ) ) . into ( ) ) ;
208
208
}
209
209
Ok ( Self :: serialize_object ( & temp_doc, indent, newline, space) . into ( ) )
210
210
}
@@ -233,7 +233,7 @@ impl<'a, V: SelectValue> KeyValue<'a, V> {
233
233
format : Format ,
234
234
) -> Result < RedisValue , Error > {
235
235
if format == Format :: BSON {
236
- return Err ( "Soon to come..." . into ( ) ) ;
236
+ return Err ( "ERR Soon to come..." . into ( ) ) ;
237
237
}
238
238
let is_legacy = !paths. iter ( ) . any ( |p| !p. is_legacy ( ) ) ;
239
239
if paths. len ( ) > 1 {
@@ -291,9 +291,9 @@ impl<'a, V: SelectValue> KeyValue<'a, V> {
291
291
// if we reach here with array path we must be out of range
292
292
// otherwise the path would be valid to be set and we would not
293
293
// have reached here!!
294
- Err ( "array index out of range" . into ( ) )
294
+ Err ( "ERR array index out of range" . into ( ) )
295
295
} else {
296
- Err ( "path not an object or array" . into ( ) )
296
+ Err ( "ERR path not an object or array" . into ( ) )
297
297
}
298
298
}
299
299
@@ -325,7 +325,7 @@ impl<'a, V: SelectValue> KeyValue<'a, V> {
325
325
pub fn serialize ( results : & V , format : Format ) -> Result < String , Error > {
326
326
let res = match format {
327
327
Format :: JSON => serde_json:: to_string ( results) ?,
328
- Format :: BSON => return Err ( "Soon to come..." . into ( ) ) , //results.into() as Bson,
328
+ Format :: BSON => return Err ( "ERR Soon to come..." . into ( ) ) , //results.into() as Bson,
329
329
} ;
330
330
Ok ( res)
331
331
}
@@ -378,23 +378,35 @@ impl<'a, V: SelectValue> KeyValue<'a, V> {
378
378
let first = self . get_first ( path) ?;
379
379
match first. get_type ( ) {
380
380
SelectValueType :: String => Ok ( first. get_str ( ) . len ( ) ) ,
381
- _ => Err ( "ERR wrong type of path value" . into ( ) ) ,
381
+ _ => Err (
382
+ err_msg_json_expected ( "string" , self . get_type ( path) . unwrap ( ) . as_str ( ) )
383
+ . as_str ( )
384
+ . into ( ) ,
385
+ ) ,
382
386
}
383
387
}
384
388
385
389
pub fn arr_len ( & self , path : & str ) -> Result < usize , Error > {
386
390
let first = self . get_first ( path) ?;
387
391
match first. get_type ( ) {
388
392
SelectValueType :: Array => Ok ( first. len ( ) . unwrap ( ) ) ,
389
- _ => Err ( "ERR wrong type of path value" . into ( ) ) ,
393
+ _ => Err (
394
+ err_msg_json_expected ( "array" , self . get_type ( path) . unwrap ( ) . as_str ( ) )
395
+ . as_str ( )
396
+ . into ( ) ,
397
+ ) ,
390
398
}
391
399
}
392
400
393
401
pub fn obj_len ( & self , path : & str ) -> Result < usize , Error > {
394
402
let first = self . get_first ( path) ?;
395
403
match first. get_type ( ) {
396
404
SelectValueType :: Object => Ok ( first. len ( ) . unwrap ( ) ) ,
397
- _ => Err ( "ERR wrong type of path value" . into ( ) ) ,
405
+ _ => Err (
406
+ err_msg_json_expected ( "object" , self . get_type ( path) . unwrap ( ) . as_str ( ) )
407
+ . as_str ( )
408
+ . into ( ) ,
409
+ ) ,
398
410
}
399
411
}
400
412
@@ -502,9 +514,11 @@ impl<'a, V: SelectValue> KeyValue<'a, V> {
502
514
}
503
515
504
516
pub fn obj_keys ( & self , path : & str ) -> Result < Box < dyn Iterator < Item = & ' _ str > + ' _ > , Error > {
505
- self . get_first ( path) ?
506
- . keys ( )
507
- . ok_or_else ( || "ERR wrong type of path value" . into ( ) )
517
+ self . get_first ( path) ?. keys ( ) . ok_or_else ( || {
518
+ err_msg_json_expected ( "object" , self . get_type ( path) . unwrap ( ) . as_str ( ) )
519
+ . as_str ( )
520
+ . into ( )
521
+ } )
508
522
}
509
523
}
510
524
@@ -708,10 +722,9 @@ where
708
722
let res = get_all_values_and_paths ( path, doc) ?;
709
723
match res. is_empty ( ) {
710
724
false => Ok ( filter_paths ( res, f) ) ,
711
- _ => Err ( RedisError :: String ( format ! (
712
- "Path '{}' does not exist" ,
713
- path
714
- ) ) ) ,
725
+ _ => Err ( RedisError :: String (
726
+ err_msg_json_path_doesnt_exist_with_param ( path) ,
727
+ ) ) ,
715
728
}
716
729
}
717
730
@@ -726,10 +739,9 @@ where
726
739
let res = get_all_values_and_paths ( path, doc) ?;
727
740
match res. is_empty ( ) {
728
741
false => Ok ( filter_values ( res, f) ) ,
729
- _ => Err ( RedisError :: String ( format ! (
730
- "Path '{}' does not exist" ,
731
- path
732
- ) ) ) ,
742
+ _ => Err ( RedisError :: String (
743
+ err_msg_json_path_doesnt_exist_with_param ( path) ,
744
+ ) ) ,
733
745
}
734
746
}
735
747
@@ -1016,10 +1028,9 @@ where
1016
1028
redis_key. apply_changes ( ctx, cmd) ?;
1017
1029
Ok ( res. unwrap ( ) . to_string ( ) . into ( ) )
1018
1030
} else {
1019
- Err ( RedisError :: String ( format ! (
1020
- "Path '{}' does not exist or does not contains a number" ,
1021
- path
1022
- ) ) )
1031
+ Err ( RedisError :: String (
1032
+ err_msg_json_path_doesnt_exist_with_param_or ( path, "does not contains a number" ) ,
1033
+ ) )
1023
1034
}
1024
1035
}
1025
1036
@@ -1109,10 +1120,9 @@ where
1109
1120
redis_key. apply_changes ( ctx, "json.toggle" ) ?;
1110
1121
Ok ( res. to_string ( ) . into ( ) )
1111
1122
} else {
1112
- Err ( RedisError :: String ( format ! (
1113
- "Path '{}' does not exist or not a bool" ,
1114
- path
1115
- ) ) )
1123
+ Err ( RedisError :: String (
1124
+ err_msg_json_path_doesnt_exist_with_param_or ( path, "not a bool" ) ,
1125
+ ) )
1116
1126
}
1117
1127
}
1118
1128
@@ -1201,10 +1211,9 @@ where
1201
1211
redis_key. apply_changes ( ctx, "json.strappend" ) ?;
1202
1212
Ok ( res. unwrap ( ) . into ( ) )
1203
1213
} else {
1204
- Err ( RedisError :: String ( format ! (
1205
- "Path '{}' does not exist or not a string" ,
1206
- path
1207
- ) ) )
1214
+ Err ( RedisError :: String (
1215
+ err_msg_json_path_doesnt_exist_with_param_or ( path, "not a string" ) ,
1216
+ ) )
1208
1217
}
1209
1218
}
1210
1219
@@ -1281,14 +1290,14 @@ pub fn command_json_arr_append<M: Manager>(
1281
1290
if !path. is_legacy ( ) {
1282
1291
json_arr_append :: < M > ( & mut redis_key, ctx, path. get_path ( ) , args)
1283
1292
} else {
1284
- json_arr_append_legacy :: < M > ( & mut redis_key, ctx, path. get_path ( ) , args)
1293
+ json_arr_append_legacy :: < M > ( & mut redis_key, ctx, & path, args)
1285
1294
}
1286
1295
}
1287
1296
1288
1297
fn json_arr_append_legacy < M > (
1289
1298
redis_key : & mut M :: WriteHolder ,
1290
1299
ctx : & Context ,
1291
- path : & str ,
1300
+ path : & Path ,
1292
1301
args : Vec < M :: O > ,
1293
1302
) -> RedisResult
1294
1303
where
@@ -1297,12 +1306,13 @@ where
1297
1306
let root = redis_key
1298
1307
. get_value ( ) ?
1299
1308
. ok_or_else ( RedisError :: nonexistent_key) ?;
1300
- let mut paths = find_paths ( path, root, |v| v. get_type ( ) == SelectValueType :: Array ) ?;
1309
+ let mut paths = find_paths ( path. get_path ( ) , root, |v| {
1310
+ v. get_type ( ) == SelectValueType :: Array
1311
+ } ) ?;
1301
1312
if paths. is_empty ( ) {
1302
- Err ( RedisError :: String ( format ! (
1303
- "Path '{}' does not exist" ,
1304
- path
1305
- ) ) )
1313
+ Err ( RedisError :: String (
1314
+ err_msg_json_path_doesnt_exist_with_param_or ( path. get_original ( ) , "not an array" ) ,
1315
+ ) )
1306
1316
} else if paths. len ( ) == 1 {
1307
1317
let res = redis_key. arr_append ( paths. pop ( ) . unwrap ( ) , args) ?;
1308
1318
redis_key. apply_changes ( ctx, "json.arrappend" ) ?;
@@ -1384,9 +1394,9 @@ pub fn command_json_arr_index<M: Manager>(
1384
1394
let is_legacy = path. is_legacy ( ) ;
1385
1395
let scalar_value: Value = serde_json:: from_str ( json_scalar) ?;
1386
1396
if !is_legacy && ( scalar_value. is_array ( ) || scalar_value. is_object ( ) ) {
1387
- return Err ( RedisError :: String ( format ! (
1388
- "ERR expected scalar but found {} " ,
1389
- json_scalar
1397
+ return Err ( RedisError :: String ( err_msg_json_expected (
1398
+ "scalar" ,
1399
+ json_scalar,
1390
1400
) ) ) ;
1391
1401
}
1392
1402
@@ -1489,10 +1499,9 @@ where
1489
1499
redis_key. apply_changes ( ctx, "json.arrinsert" ) ?;
1490
1500
Ok ( res. unwrap ( ) . into ( ) )
1491
1501
} else {
1492
- Err ( RedisError :: String ( format ! (
1493
- "Path '{}' does not exist or not an array" ,
1494
- path
1495
- ) ) )
1502
+ Err ( RedisError :: String (
1503
+ err_msg_json_path_doesnt_exist_with_param_or ( path, "not an array" ) ,
1504
+ ) )
1496
1505
}
1497
1506
}
1498
1507
@@ -1624,10 +1633,9 @@ where
1624
1633
None => Ok ( ( ) . into ( ) ) ,
1625
1634
}
1626
1635
} else {
1627
- Err ( RedisError :: String ( format ! (
1628
- "Path '{}' does not exist or not an array" ,
1629
- path
1630
- ) ) )
1636
+ Err ( RedisError :: String (
1637
+ err_msg_json_path_doesnt_exist_with_param_or ( path, "not an array" ) ,
1638
+ ) )
1631
1639
}
1632
1640
}
1633
1641
@@ -1706,10 +1714,9 @@ where
1706
1714
redis_key. apply_changes ( ctx, "json.arrtrim" ) ?;
1707
1715
Ok ( res. unwrap ( ) . into ( ) )
1708
1716
} else {
1709
- Err ( RedisError :: String ( format ! (
1710
- "Path '{}' does not exist or not an array" ,
1711
- path
1712
- ) ) )
1717
+ Err ( RedisError :: String (
1718
+ err_msg_json_path_doesnt_exist_with_param_or ( path, "not an array" ) ,
1719
+ ) )
1713
1720
}
1714
1721
}
1715
1722
0 commit comments