mirror of
https://github.com/Biarity/Sieve.git
synced 2024-11-24 06:23:02 +01:00
Add filtering on null.
This commit is contained in:
parent
b47ed62f77
commit
f39944d0e4
@ -56,6 +56,7 @@ namespace Sieve.Services
|
|||||||
where TFilterTerm : IFilterTerm, new()
|
where TFilterTerm : IFilterTerm, new()
|
||||||
where TSortTerm : ISortTerm, new()
|
where TSortTerm : ISortTerm, new()
|
||||||
{
|
{
|
||||||
|
private const string nullFilterValue = "null";
|
||||||
private readonly IOptions<SieveOptions> _options;
|
private readonly IOptions<SieveOptions> _options;
|
||||||
private readonly ISieveCustomSortMethods _customSortMethods;
|
private readonly ISieveCustomSortMethods _customSortMethods;
|
||||||
private readonly ISieveCustomFilterMethods _customFilterMethods;
|
private readonly ISieveCustomFilterMethods _customFilterMethods;
|
||||||
@ -179,15 +180,14 @@ namespace Sieve.Services
|
|||||||
var (fullPropertyName, property) = GetSieveProperty<TEntity>(false, true, filterTermName);
|
var (fullPropertyName, property) = GetSieveProperty<TEntity>(false, true, filterTermName);
|
||||||
if (property != null)
|
if (property != null)
|
||||||
{
|
{
|
||||||
var converter = TypeDescriptor.GetConverter(property.PropertyType);
|
|
||||||
Expression propertyValue = parameter;
|
Expression propertyValue = parameter;
|
||||||
Expression nullCheck = null;
|
Expression nullCheck = null;
|
||||||
|
var names = fullPropertyName.Split('.');
|
||||||
foreach (var name in fullPropertyName.Split('.'))
|
for (var i = 0; i < names.Length; i++)
|
||||||
{
|
{
|
||||||
propertyValue = Expression.PropertyOrField(propertyValue, name);
|
propertyValue = Expression.PropertyOrField(propertyValue, names[i]);
|
||||||
|
|
||||||
if (propertyValue.Type.IsNullable())
|
if (i != names.Length - 1 && propertyValue.Type.IsNullable())
|
||||||
{
|
{
|
||||||
nullCheck = GenerateFilterNullCheckExpression(propertyValue, nullCheck);
|
nullCheck = GenerateFilterNullCheckExpression(propertyValue, nullCheck);
|
||||||
}
|
}
|
||||||
@ -195,15 +195,13 @@ namespace Sieve.Services
|
|||||||
|
|
||||||
if (filterTerm.Values == null) continue;
|
if (filterTerm.Values == null) continue;
|
||||||
|
|
||||||
|
var converter = TypeDescriptor.GetConverter(property.PropertyType);
|
||||||
foreach (var filterTermValue in filterTerm.Values)
|
foreach (var filterTermValue in filterTerm.Values)
|
||||||
{
|
{
|
||||||
|
var isFilterTermValueNull = filterTermValue.ToLower() == nullFilterValue;
|
||||||
dynamic constantVal = converter.CanConvertFrom(typeof(string))
|
var filterValue = isFilterTermValueNull
|
||||||
? converter.ConvertFrom(filterTermValue)
|
? Expression.Constant(null, property.PropertyType)
|
||||||
: Convert.ChangeType(filterTermValue, property.PropertyType);
|
: ConvertStringValueToConstantExpression(filterTermValue, property, converter);
|
||||||
|
|
||||||
Expression filterValue = GetClosureOverConstant(constantVal, property.PropertyType);
|
|
||||||
|
|
||||||
|
|
||||||
if (filterTerm.OperatorIsCaseInsensitive)
|
if (filterTerm.OperatorIsCaseInsensitive)
|
||||||
{
|
{
|
||||||
@ -223,9 +221,13 @@ namespace Sieve.Services
|
|||||||
expression = Expression.Not(expression);
|
expression = Expression.Not(expression);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nullCheck != null)
|
var filterValueNullCheck = !isFilterTermValueNull && propertyValue.Type.IsNullable()
|
||||||
|
? GenerateFilterNullCheckExpression(propertyValue, nullCheck)
|
||||||
|
: nullCheck;
|
||||||
|
|
||||||
|
if (filterValueNullCheck != null)
|
||||||
{
|
{
|
||||||
expression = Expression.AndAlso(nullCheck, expression);
|
expression = Expression.AndAlso(filterValueNullCheck, expression);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (innerExpression == null)
|
if (innerExpression == null)
|
||||||
@ -272,6 +274,15 @@ namespace Sieve.Services
|
|||||||
: Expression.AndAlso(nullCheckExpression, Expression.NotEqual(propertyValue, Expression.Default(propertyValue.Type)));
|
: Expression.AndAlso(nullCheckExpression, Expression.NotEqual(propertyValue, Expression.Default(propertyValue.Type)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Expression ConvertStringValueToConstantExpression(string value, PropertyInfo property, TypeConverter converter)
|
||||||
|
{
|
||||||
|
dynamic constantVal = converter.CanConvertFrom(typeof(string))
|
||||||
|
? converter.ConvertFrom(value)
|
||||||
|
: Convert.ChangeType(value, property.PropertyType);
|
||||||
|
|
||||||
|
return GetClosureOverConstant(constantVal, property.PropertyType);
|
||||||
|
}
|
||||||
|
|
||||||
private static Expression GetExpression(TFilterTerm filterTerm, dynamic filterValue, dynamic propertyValue)
|
private static Expression GetExpression(TFilterTerm filterTerm, dynamic filterValue, dynamic propertyValue)
|
||||||
{
|
{
|
||||||
switch (filterTerm.OperatorParsed)
|
switch (filterTerm.OperatorParsed)
|
||||||
|
@ -528,5 +528,41 @@ namespace SieveUnitTests
|
|||||||
Assert.AreEqual(sortedPosts[0].Id, 2);
|
Assert.AreEqual(sortedPosts[0].Id, 2);
|
||||||
Assert.AreEqual(sortedPosts[1].Id, 1);
|
Assert.AreEqual(sortedPosts[1].Id, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void FilteringOnNullWorks()
|
||||||
|
{
|
||||||
|
var posts = new List<Post>
|
||||||
|
{
|
||||||
|
new Post() {
|
||||||
|
Id = 1,
|
||||||
|
Title = null,
|
||||||
|
LikeCount = 0,
|
||||||
|
IsDraft = false,
|
||||||
|
CategoryId = null,
|
||||||
|
TopComment = null,
|
||||||
|
FeaturedComment = null
|
||||||
|
},
|
||||||
|
new Post() {
|
||||||
|
Id = 2,
|
||||||
|
Title = null,
|
||||||
|
LikeCount = 0,
|
||||||
|
IsDraft = false,
|
||||||
|
CategoryId = null,
|
||||||
|
TopComment = null,
|
||||||
|
FeaturedComment = new Comment { Id = 1, Text = null }
|
||||||
|
},
|
||||||
|
}.AsQueryable();
|
||||||
|
|
||||||
|
var model = new SieveModel()
|
||||||
|
{
|
||||||
|
Filters = "FeaturedComment.Text==null",
|
||||||
|
};
|
||||||
|
|
||||||
|
var result = _processor.Apply(model, posts);
|
||||||
|
Assert.AreEqual(1, result.Count());
|
||||||
|
var filteredPosts = result.ToList();
|
||||||
|
Assert.AreEqual(filteredPosts[0].Id, 2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user