@@ -13,22 +13,24 @@ namespace ByteAether.QueryLink;
1313/// </summary>
1414public static class QueryStringExtensions
1515{
16- private static readonly ( FilterOperator Operator , string StringValue ) [ ] _filterOperatorPairs
16+ private static readonly Dictionary < FilterOperator , string > _operatorToStringMap
1717 = ( ( FilterOperator [ ] ) Enum . GetValues ( typeof ( FilterOperator ) ) )
18- . Select ( x => (
19- Operator : x ,
20- StringValue : typeof ( FilterOperator )
21- . GetField ( x . ToString ( ) ) ?
22- . GetCustomAttribute < DescriptionAttribute > ( ) ?
23- . Description ?? x . ToString ( )
24- ) )
25- . ToArray ( ) ;
18+ . ToDictionary (
19+ x => x ,
20+ x => typeof ( FilterOperator )
21+ . GetField ( x . ToString ( ) ) ?
22+ . GetCustomAttribute < DescriptionAttribute > ( ) ?
23+ . Description ?? x . ToString ( )
24+ ) ;
25+
26+ private static readonly Dictionary < string , FilterOperator > _operatorFromStringMap
27+ = _operatorToStringMap . ToDictionary ( x => x . Value , x => x . Key ) ;
2628
2729 private static readonly Regex _filterSplitter = new (
2830 $ "({ string . Join (
2931 '|' ,
30- _filterOperatorPairs
31- . Select ( x => x . StringValue )
32+ _operatorToStringMap
33+ . Values
3234 . OrderByDescending ( x => x . Length )
3335 . Select ( Regex . Escape )
3436 ) } )" ,
@@ -49,8 +51,8 @@ public static string ToQueryString(
4951 ) => new StringBuilder ( )
5052 . Append (
5153 string . Join ( '&' , definitions . Filters
52- . Select ( ToQueryString )
53- . Select ( x => $ "{ filterKey } []={ x } ")
54+ . Select ( ToQueryString )
55+ . Select ( x => $ "{ filterKey } []={ x } ")
5456 )
5557 )
5658 . Append ( definitions . Orders . Any ( ) ? $ "&{ orderKey } =" : string . Empty )
@@ -86,12 +88,12 @@ public static Definitions FromQueryString(
8688 if ( queryParam . Key == $ "{ filterKey } []")
8789 {
8890 var valParts = _filterSplitter . Split ( queryParam . Value , 2 ) ;
89- var op = OperatorFromString ( valParts [ 1 ] ) ;
91+ var op = _operatorFromStringMap [ valParts [ 1 ] ] ;
9092 var filterValue = StringValueParser . Parse ( valParts [ 2 ] ) ;
9193
9294 filters . Add ( new FilterDefinition < object ? > (
9395 valParts [ 0 ] ,
94- OperatorFromString ( valParts [ 1 ] ) ,
96+ _operatorFromStringMap [ valParts [ 1 ] ] ,
9597 filterValue
9698 ) ) ;
9799 }
@@ -127,12 +129,6 @@ private static string ToQueryString<T>(FilterDefinition<T> def)
127129 ? "[" + string . Join ( ',' , ( def . Value as IEnumerable ) ! . OfType < object > ( ) . Select ( x => x . ToString ( ) ? . Replace ( "," , "\\ ," ) ) ) + "]"
128130 : def . Value ? . ToString ( ) ;
129131
130- return HttpUtility . UrlEncode ( $ "{ def . Name } { OperatorToString ( def . Operation ) } { valueSet } ") ;
132+ return HttpUtility . UrlEncode ( $ "{ def . Name } { _operatorToStringMap [ def . Operation ] } { valueSet } ") ;
131133 }
132-
133- private static string OperatorToString ( FilterOperator op )
134- => _filterOperatorPairs . Single ( x => x . Operator == op ) . StringValue ;
135-
136- private static FilterOperator OperatorFromString ( string operatorString )
137- => _filterOperatorPairs . Single ( x => x . StringValue == operatorString ) . Operator ;
138134}
0 commit comments