@@ -7,26 +7,30 @@ pub(crate) struct OrderByBuilder<'a> {
7
7
column_name : & ' a str ,
8
8
}
9
9
10
+ fn str_to_order ( order : & str ) -> anyhow:: Result < Order > {
11
+ match order {
12
+ "asc" | "ASC" => Ok ( Order :: Asc ) ,
13
+ "desc" | "DESC" => Ok ( Order :: Desc ) ,
14
+ _ => anyhow:: bail!( "Invalid `order_by`. Please refer to examples in the documentation for correct `order_by` syntax" ) ,
15
+ }
16
+ }
17
+
10
18
fn build_recursive_access ( key : & str , value : & serde_json:: Value ) -> anyhow:: Result < ( String , Order ) > {
11
19
if value. is_object ( ) {
12
20
let ( new_key, new_value) = value
13
21
. as_object ( )
14
22
. unwrap ( )
15
23
. iter ( )
16
24
. next ( )
17
- . context ( "Invalid order by " ) ?;
25
+ . context ( "Invalid `order_by`. Please refer to examples in the documentation for correct `order_by` syntax " ) ?;
18
26
let ( path, order) = build_recursive_access ( new_key, new_value) ?;
19
27
let path = format ! ( "{},{}" , key, path) ;
20
28
Ok ( ( path, order) )
21
29
} else if value. is_string ( ) {
22
- let order = match value. as_str ( ) . unwrap ( ) {
23
- "asc" | "ASC" => Order :: Asc ,
24
- "desc" | "DESC" => Order :: Desc ,
25
- _ => return Err ( anyhow:: anyhow!( "Invalid order by" ) ) ,
26
- } ;
30
+ let order = str_to_order ( value. as_str ( ) . unwrap ( ) ) ?;
27
31
Ok ( ( key. to_string ( ) , order) )
28
32
} else {
29
- Err ( anyhow:: anyhow!( "Invalid order by " ) )
33
+ Err ( anyhow:: anyhow!( "Invalid `order_by`. Please refer to examples in the documentation for correct `order_by` syntax " ) )
30
34
}
31
35
}
32
36
@@ -42,17 +46,22 @@ impl<'a> OrderByBuilder<'a> {
42
46
pub fn build ( self ) -> anyhow:: Result < Vec < ( SimpleExpr , Order ) > > {
43
47
self . filter
44
48
. as_object ( )
45
- . context ( "Invalid order by " ) ?
49
+ . context ( "`order_by` must be an object " ) ?
46
50
. iter ( )
47
51
. map ( |( k, v) | {
48
- if let Ok ( ( path, order) ) = build_recursive_access ( k, v) {
52
+ if k. starts_with ( "COLUMN_" ) {
53
+ Ok ( (
54
+ Expr :: cust ( k. replace ( "COLUMN_" , "" ) ) ,
55
+ str_to_order ( v. as_str ( ) . context ( "Invalid `order_by`. Please refer to examples in the documentation for correct `order_by` syntax" ) ?) ?,
56
+ ) )
57
+ } else if let Ok ( ( path, order) ) = build_recursive_access ( k, v) {
49
58
let expr = Expr :: cust ( format ! (
50
59
"\" {}\" .\" {}\" #>'{{{}}}'" ,
51
60
self . table_name, self . column_name, path
52
61
) ) ;
53
62
Ok ( ( expr, order) )
54
63
} else {
55
- Err ( anyhow:: anyhow!( "Invalid order by " ) )
64
+ Err ( anyhow:: anyhow!( "Invalid `order_by`. Please refer to examples in the documentation for correct `order_by` syntax " ) )
56
65
}
57
66
} )
58
67
. collect ( )
0 commit comments