Added OR flitering for values and ability to escape delimiters. Fixes #8, #21, and #41

This commit is contained in:
Biarity
2018-11-16 18:08:25 +10:00
parent f9c7fb4cb0
commit faa363edbb
8 changed files with 75 additions and 34 deletions

View File

@@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Text.RegularExpressions;
namespace Sieve.Models
{
@@ -7,6 +8,8 @@ namespace Sieve.Models
{
public FilterTerm() { }
private const string EscapedPipePattern = @"(?<!($|[^\\])(\\\\)*?\\)\|";
private static readonly string[] Operators = new string[] {
"==*",
"@=*",
@@ -25,9 +28,10 @@ namespace Sieve.Models
{
set
{
var filterSplits = value.Split(Operators, StringSplitOptions.RemoveEmptyEntries).Select(t => t.Trim()).ToArray();
Names = filterSplits[0].Split('|').Select(t => t.Trim()).ToArray();
Value = filterSplits.Length > 1 ? filterSplits[1] : null;
var filterSplits = value.Split(Operators, StringSplitOptions.RemoveEmptyEntries)
.Select(t => t.Trim()).ToArray();
Names = Regex.Split(filterSplits[0], EscapedPipePattern).Select(t => t.Trim()).ToArray();
Values = filterSplits.Length > 1 ? Regex.Split(filterSplits[1], EscapedPipePattern).Select(t => t.Trim()).ToArray() : null;
Operator = Array.Find(Operators, o => value.Contains(o)) ?? "==";
OperatorParsed = GetOperatorParsed(Operator);
OperatorIsCaseInsensitive = Operator.Contains("*");
@@ -39,7 +43,7 @@ namespace Sieve.Models
public FilterOperator OperatorParsed { get; private set; }
public string Value { get; private set; }
public string[] Values { get; private set; }
public string Operator { get; private set; }

View File

@@ -7,6 +7,6 @@
string Operator { get; }
bool OperatorIsCaseInsensitive { get; }
FilterOperator OperatorParsed { get; }
string Value { get; }
string[] Values { get; }
}
}

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
namespace Sieve.Models
{
@@ -13,6 +14,8 @@ namespace Sieve.Models
where TFilterTerm : IFilterTerm, new()
where TSortTerm : ISortTerm, new()
{
private const string EscapedCommaPattern = @"(?<!($|[^\\])(\\\\)*?\\),";
[DataMember]
public string Filters { get; set; }
@@ -30,7 +33,7 @@ namespace Sieve.Models
if (Filters != null)
{
var value = new List<TFilterTerm>();
foreach (var filter in Filters.Split(','))
foreach (var filter in Regex.Split(Filters, EscapedCommaPattern))
{
if (string.IsNullOrWhiteSpace(filter)) continue;
@@ -69,7 +72,7 @@ namespace Sieve.Models
if (Sorts != null)
{
var value = new List<TSortTerm>();
foreach (var sort in Sorts.Split(','))
foreach (var sort in Regex.Split(Sorts, EscapedCommaPattern))
{
if (string.IsNullOrWhiteSpace(sort)) continue;

View File

@@ -180,33 +180,37 @@ namespace Sieve.Services
if (property != null)
{
var converter = TypeDescriptor.GetConverter(property.PropertyType);
dynamic constantVal = converter.CanConvertFrom(typeof(string))
? converter.ConvertFrom(filterTerm.Value)
: Convert.ChangeType(filterTerm.Value, property.PropertyType);
Expression filterValue = GetClosureOverConstant(constantVal, property.PropertyType);
dynamic propertyValue = Expression.PropertyOrField(parameterExpression, property.Name);
if (filterTerm.OperatorIsCaseInsensitive)
foreach (var filterTermValue in filterTerm.Values)
{
propertyValue = Expression.Call(propertyValue,
typeof(string).GetMethods()
.First(m => m.Name == "ToUpper" && m.GetParameters().Length == 0));
filterValue = Expression.Call(filterValue,
typeof(string).GetMethods()
.First(m => m.Name == "ToUpper" && m.GetParameters().Length == 0));
}
dynamic constantVal = converter.CanConvertFrom(typeof(string))
? converter.ConvertFrom(filterTermValue)
: Convert.ChangeType(filterTermValue, property.PropertyType);
if (innerExpression == null)
{
innerExpression = GetExpression(filterTerm, filterValue, propertyValue);
}
else
{
innerExpression = Expression.Or(innerExpression, GetExpression(filterTerm, filterValue, propertyValue));
Expression filterValue = GetClosureOverConstant(constantVal, property.PropertyType);
if (filterTerm.OperatorIsCaseInsensitive)
{
propertyValue = Expression.Call(propertyValue,
typeof(string).GetMethods()
.First(m => m.Name == "ToUpper" && m.GetParameters().Length == 0));
filterValue = Expression.Call(filterValue,
typeof(string).GetMethods()
.First(m => m.Name == "ToUpper" && m.GetParameters().Length == 0));
}
if (innerExpression == null)
{
innerExpression = GetExpression(filterTerm, filterValue, propertyValue);
}
else
{
innerExpression = Expression.Or(innerExpression, GetExpression(filterTerm, filterValue, propertyValue));
}
}
}
else
@@ -215,7 +219,7 @@ namespace Sieve.Services
new object[] {
result,
filterTerm.Operator,
filterTerm.Value
filterTerm.Values
}, dataForCustomMethods);
}