@@ -19,6 +19,8 @@ public class Parser<T> where T : class
19
19
private int _take ;
20
20
private int _skip ;
21
21
private bool _sortDisabled = false ;
22
+ private string _startsWithtoken = "*|" ;
23
+ private string _endsWithToken = "|*" ;
22
24
23
25
private Dictionary < string , Expression > _converters = new Dictionary < string , Expression > ( ) ;
24
26
@@ -166,6 +168,35 @@ public Parser<T> SetConverter(Expression<Func<T,object>> property, Expression<Fu
166
168
return this ;
167
169
}
168
170
171
+ ///<summary>
172
+ /// SetStartsWithToken overrides the default StartsWith filter token
173
+ /// The default token is *|
174
+ /// By default all filters are in the form of string.Contains(FILTER_STRING)
175
+ /// If a filter string is in the form of token + FILTER_STRING eg. *|app,
176
+ /// the search will be translated to string.StartsWith("app")
177
+ ///</summary>
178
+ /// <param name="token">A string used to replace the default token</param>
179
+ public Parser < T > SetStartsWithToken ( string token )
180
+ {
181
+ this . _startsWithtoken = token ;
182
+ return this ;
183
+ }
184
+
185
+ ///<summary>
186
+ /// SetEndsWithToken overrides the default EndsWith filter token
187
+ /// The default token is |*
188
+ /// By default all filters are in the form of string.Contains(FILTER_STRING)
189
+ /// If a filter string is in the form of FILTER_STRING + token eg. app|*,
190
+ /// the search will be translated to string.EndsWith("app")
191
+ ///</summary>
192
+ /// <param name="token">A string used to replace the default token</param>
193
+ public Parser < T > SetEndsWithToken ( string token )
194
+ {
195
+ this . _endsWithToken = token ;
196
+ return this ;
197
+ }
198
+
199
+
169
200
private void ApplySort ( )
170
201
{
171
202
var sorted = false ;
@@ -268,6 +299,23 @@ private bool EnumerablFilter(T item)
268
299
269
300
}
270
301
302
+ private string GetFilterFn ( string filter )
303
+ {
304
+ switch ( filter )
305
+ {
306
+ case null :
307
+ return Constants . CONTAINS_FN ;
308
+ case var f when f . StartsWith ( _startsWithtoken ) && f . EndsWith ( _endsWithToken ) :
309
+ return Constants . CONTAINS_FN ;
310
+ case var f when f . StartsWith ( _startsWithtoken ) :
311
+ return Constants . STARTS_WITH_FN ;
312
+ case var f when f . EndsWith ( _endsWithToken ) :
313
+ return Constants . ENDS_WITH_FN ;
314
+ default :
315
+ return Constants . CONTAINS_FN ;
316
+ }
317
+ }
318
+
271
319
/// <summary>
272
320
/// Generate a lamda expression based on a search filter for all mapped columns
273
321
/// </summary>
@@ -277,11 +325,12 @@ private Expression<Func<T, bool>> GenerateEntityFilter()
277
325
var paramExpression = Expression . Parameter ( _type , "val" ) ;
278
326
279
327
string filter = _config [ Constants . SEARCH_KEY ] ;
280
-
328
+ string globalFilterFn = null ;
281
329
ConstantExpression globalFilterConst = null ;
282
330
Expression filterExpr = null ;
283
331
if ( ! string . IsNullOrWhiteSpace ( filter ) )
284
332
{
333
+ globalFilterFn = GetFilterFn ( filter ) ;
285
334
globalFilterConst = Expression . Constant ( filter . ToLower ( ) ) ;
286
335
}
287
336
@@ -293,6 +342,7 @@ private Expression<Func<T, bool>> GenerateEntityFilter()
293
342
var prop = propMap . Value . Property ;
294
343
var isString = prop . PropertyType == typeof ( string ) ;
295
344
var hasCustomExpr = _converters . ContainsKey ( prop . Name ) ;
345
+ string propFilterFn = null ;
296
346
297
347
if ( ( ! prop . CanWrite || ( ! _convertable . Any ( t => t == prop . PropertyType ) && ! isString ) ) && ! hasCustomExpr )
298
348
{
@@ -302,6 +352,7 @@ private Expression<Func<T, bool>> GenerateEntityFilter()
302
352
ConstantExpression individualFilterConst = null ;
303
353
if ( ! string . IsNullOrWhiteSpace ( propMap . Value . Filter ) )
304
354
{
355
+ propFilterFn = GetFilterFn ( propMap . Value . Filter ) ;
305
356
individualFilterConst = Expression . Constant ( propMap . Value . Filter . ToLower ( ) ) ;
306
357
}
307
358
@@ -323,7 +374,7 @@ private Expression<Func<T, bool>> GenerateEntityFilter()
323
374
324
375
if ( globalFilterConst != null )
325
376
{
326
- var globalTest = Expression . Call ( toLower , typeof ( string ) . GetMethod ( "Contains" , new [ ] { typeof ( string ) } ) , globalFilterConst ) ;
377
+ var globalTest = Expression . Call ( toLower , typeof ( string ) . GetMethod ( globalFilterFn , new [ ] { typeof ( string ) } ) , globalFilterConst ) ;
327
378
328
379
if ( filterExpr == null )
329
380
{
@@ -337,7 +388,7 @@ private Expression<Func<T, bool>> GenerateEntityFilter()
337
388
338
389
if ( individualFilterConst != null )
339
390
{
340
- individualConditions . Add ( Expression . Call ( toLower , typeof ( string ) . GetMethod ( "Contains" , new [ ] { typeof ( string ) } ) , individualFilterConst ) ) ;
391
+ individualConditions . Add ( Expression . Call ( toLower , typeof ( string ) . GetMethod ( propFilterFn , new [ ] { typeof ( string ) } ) , individualFilterConst ) ) ;
341
392
342
393
}
343
394
@@ -419,6 +470,10 @@ public class Constants
419
470
public const string ORDER_DIRECTION_FORMAT = "order[{0}][dir]" ;
420
471
public const string ORDERING_ENABLED = "ordering" ;
421
472
473
+ public const string CONTAINS_FN = "Contains" ;
474
+ public const string STARTS_WITH_FN = "StartsWith" ;
475
+ public const string ENDS_WITH_FN = "EndsWith" ;
476
+
422
477
public static string GetKey ( string format , string index )
423
478
{
424
479
return String . Format ( format , index ) ;
0 commit comments