Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ability to filter int values using string operators #184

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions Sieve/Services/SieveProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,8 @@ protected virtual IQueryable<TEntity> ApplyFiltering<TEntity>(TSieveModel model,

if (filterTerm.OperatorIsCaseInsensitive && !isFilterTermValueNull)
{
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));
propertyValue = GetStringMethodExpression(propertyValue, "ToUpper");
filterValue = GetStringMethodExpression(filterValue, "ToUpper");
}

var expression = GetExpression(filterTerm, filterValue, propertyValue);
Expand Down Expand Up @@ -333,19 +328,30 @@ private static Expression GetExpression(TFilterTerm filterTerm, dynamic filterVa
FilterOperator.LessThan => Expression.LessThan(propertyValue, filterValue),
FilterOperator.GreaterThanOrEqualTo => Expression.GreaterThanOrEqual(propertyValue, filterValue),
FilterOperator.LessThanOrEqualTo => Expression.LessThanOrEqual(propertyValue, filterValue),
FilterOperator.Contains => Expression.Call(propertyValue,
typeof(string).GetMethods().First(m => m.Name == "Contains" && m.GetParameters().Length == 1),
filterValue),
FilterOperator.StartsWith => Expression.Call(propertyValue,
typeof(string).GetMethods().First(m => m.Name == "StartsWith" && m.GetParameters().Length == 1),
filterValue),
FilterOperator.EndsWith => Expression.Call(propertyValue,
typeof(string).GetMethods().First(m => m.Name == "EndsWith" && m.GetParameters().Length == 1),
filterValue),
FilterOperator.Contains => GetStringMethodExpression(filterValue, propertyValue, "Contains"),
FilterOperator.StartsWith => GetStringMethodExpression(filterValue, propertyValue, "StartsWith"),
FilterOperator.EndsWith => GetStringMethodExpression(filterValue, propertyValue, "EndsWith"),
_ => Expression.Equal(propertyValue, filterValue)
};
}

private static Expression GetStringMethodExpression(dynamic filterValue, dynamic propertyValue, string methodName) =>
propertyValue.Type == typeof(string) && filterValue.Type == typeof(string)
? Expression.Call(propertyValue, GetStringMethod(methodName, 1), filterValue)
: Expression.Call(CallToString(propertyValue), GetStringMethod(methodName, 1), CallToString(filterValue));

private static Expression GetStringMethodExpression(dynamic propertyValue, string methodName) =>
propertyValue.Type == typeof(string)
? Expression.Call(propertyValue, GetStringMethod(methodName, 0))
: Expression.Call(CallToString(propertyValue), GetStringMethod(methodName, 0));

private static Expression CallToString(dynamic value) =>
Expression.Call(value, typeof(object).GetMethod("ToString"));

private static MethodInfo GetStringMethod(string methodName, int paramCount) =>
typeof(string).GetMethods()
.First(m => m.Name == methodName && m.GetParameters().Length == paramCount);

// Workaround to ensure that the filter value gets passed as a parameter in generated SQL from EF Core
private static Expression GetClosureOverConstant<T>(T constant, Type targetType)
{
Expand Down
104 changes: 103 additions & 1 deletion SieveUnitTests/General.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public General(ITestOutputHelper testOutputHelper)
{
Id = 0,
Title = "A",
LikeCount = 100,
LikeCount = 500,
IsDraft = true,
CategoryId = null,
TopComment = new Comment { Id = 0, Text = "A1" },
Expand Down Expand Up @@ -254,6 +254,108 @@ public void CanFilterNullableIntsWithNotEqual()
Assert.True(result.Count() == 2);
Assert.True(nullableResult.Count() == 3);
}

[Theory]
[InlineData(5)]
[InlineData(0)]
public void CanFilterIntsUsingContainsOperator(int likeCount)
{
var model = new SieveModel
{
Filters = $"LikeCount@={likeCount}"
};

var result = _processor.Apply(model, _posts);

Assert.True(result.Count() == 3);
}

[Theory]
[InlineData(5)]
[InlineData(0)]
public void CanFilterIntsUsingCaseInsensitiveContainsOperator(int likeCount)
{
var model = new SieveModel
{
Filters = $"LikeCount@=*{likeCount}"
};

var result = _processor.Apply(model, _posts);

Assert.True(result.Count() == 3);
}

[Theory]
[InlineData(5)]
[InlineData(0)]
public void CanFilterIntsUsingDoesNotContainsOperator(int likeCount)
{
var model = new SieveModel
{
Filters = $"LikeCount!@={likeCount}"
};

var result = _processor.Apply(model, _posts);

Assert.True(result.Count() == 2);
}

[Theory]
[InlineData(5)]
[InlineData(0)]
public void CanFilterIntsUsingCaseInsensitiveDoesNotContainsOperator(int likeCount)
{
var model = new SieveModel
{
Filters = $"LikeCount!@=*{likeCount}"
};

var result = _processor.Apply(model, _posts);

Assert.True(result.Count() == 2);
}

[Theory]
[InlineData(5)]
public void CanFilterIntsUsingCaseInsensitiveDoesNotStartsWithOperator(int likeCount)
{
var model = new SieveModel
{
Filters = $"LikeCount!_=*{likeCount}"
};

var result = _processor.Apply(model, _posts);

Assert.True(result.Count() == 2);
}

[Theory]
[InlineData(5)]
public void CanFilterIntsUsingStartsWithOperator(int likeCount)
{
var model = new SieveModel
{
Filters = $"LikeCount_={likeCount}"
};

var result = _processor.Apply(model, _posts);

Assert.True(result.Count() == 3);
}

[Theory]
[InlineData(0)]
public void CanFilterIntsUsingEndsWithOperator(int likeCount)
{
var model = new SieveModel
{
Filters = $"LikeCount_-={likeCount}"
};

var result = _processor.Apply(model, _posts);

Assert.True(result.Count() == 3);
}

[Theory]
[InlineData(@"Text@=*\,")]
Expand Down