@@ -7,26 +7,30 @@ pub(crate) struct OrderByBuilder<'a> {
77column_name : & ' a str ,
88}
99
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+
1018fn build_recursive_access ( key : & str , value : & serde_json:: Value ) -> anyhow:: Result < ( String , Order ) > {
1119if value. is_object ( ) {
1220let ( new_key, new_value) = value
1321. as_object ( )
1422. unwrap ( )
1523. iter ( )
1624. next ( )
17- . context ( "Invalidorder by " ) ?;
25+ . context ( "Invalid`order_by`. Please refer to examples in the documentation for correct `order_by` syntax " ) ?;
1826let ( path, order) =build_recursive_access ( new_key, new_value) ?;
1927let path =format ! ( "{},{}" , key, path) ;
2028Ok ( ( path, order) )
2129} 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 ( ) ) ?;
2731Ok ( ( key. to_string ( ) , order) )
2832} else {
29- Err ( anyhow:: anyhow!( "Invalidorder by " ) )
33+ Err ( anyhow:: anyhow!( "Invalid`order_by`. Please refer to examples in the documentation for correct `order_by` syntax " ) )
3034}
3135}
3236
@@ -42,17 +46,22 @@ impl<'a> OrderByBuilder<'a> {
4246pub fn build ( self ) -> anyhow:: Result < Vec < ( SimpleExpr , Order ) > > {
4347self . filter
4448. as_object ( )
45- . context ( "Invalid order by " ) ?
49+ . context ( "`order_by` must be an object " ) ?
4650. iter ( )
4751. 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) {
4958let expr =Expr :: cust ( format ! (
5059"\" {}\" .\" {}\" #>'{{{}}}'" ,
5160self . table_name, self . column_name, path
5261) ) ;
5362Ok ( ( expr, order) )
5463} else {
55- Err ( anyhow:: anyhow!( "Invalidorder by " ) )
64+ Err ( anyhow:: anyhow!( "Invalid`order_by`. Please refer to examples in the documentation for correct `order_by` syntax " ) )
5665}
5766} )
5867. collect ( )