Merge branch 'master' into master

This commit is contained in:
ITDancer13 2023-07-14 11:31:32 +02:00 committed by GitHub
commit 80b6c07c79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 126 additions and 29 deletions

View File

@ -129,7 +129,8 @@ Then you can add the configuration:
"DefaultPageSize": "int number: optional number to fallback to when no page argument is given. Set <=0 to disable paging if no pageSize is specified (default).",
"MaxPageSize": "int number: maximum allowed page size. Set <=0 to make infinite (default)",
"ThrowExceptions": "boolean: should Sieve throw exceptions instead of silently failing? Defaults to false",
"IgnoreNullsOnNotEqual": "boolean: ignore null values when filtering using is not equal operator? Default to true"
"IgnoreNullsOnNotEqual": "boolean: ignore null values when filtering using is not equal operator? Defaults to true",
"DisableNullableTypeExpressionForSorting": "boolean: disable the creation of nullable type expression for sorting. Some databases do not handle it (yet). Defaults to false"
}
}
```
@ -207,10 +208,13 @@ You can replace this DSL with your own (eg. use JSON instead) by implementing an
| `<=` | Less than or equal to |
| `@=` | Contains |
| `_=` | Starts with |
| `_-=` | Ends with |
| `!@=` | Does not Contains |
| `!_=` | Does not Starts with |
| `!_-=` | Does not Ends with |
| `@=*` | Case-insensitive string Contains |
| `_=*` | Case-insensitive string Starts with |
| `_-=*` | Case-insensitive string Ends with |
| `==*` | Case-insensitive string Equals |
| `!=*` | Case-insensitive string Not equals |
| `!@=*` | Case-insensitive string does not Contains |
@ -231,7 +235,7 @@ It is recommended that you write exception-handling middleware to globally handl
You can find an example project incorporating most Sieve concepts in [SieveTests](https://github.com/Biarity/Sieve/tree/master/SieveTests).
## Fluent API
To use the Fluent API instead of attributes in marking properties, setup an alternative `SieveProcessor` that overrides `MapProperties`. For example:
To use the Fluent API instead of attributes in marking properties, setup an alternative `SieveProcessor` that overrides `MapProperties`. For [example](https://github.com/Biarity/Sieve/blob/master/Sieve.Sample/Services/ApplicationSieveProcessor.cs):
```C#
public class ApplicationSieveProcessor : SieveProcessor
@ -278,7 +282,7 @@ To enable functional grouping of mappings the `ISieveConfiguration` interface wa
```C#
public class SieveConfigurationForPost : ISieveConfiguration
{
protected override SievePropertyMapper Configure(SievePropertyMapper mapper)
public void Configure(SievePropertyMapper mapper)
{
mapper.Property<Post>(p => p.Title)
.CanFilter()
@ -330,7 +334,7 @@ public class ApplicationSieveProcessor : SieveProcessor
protected override SievePropertyMapper MapProperties(SievePropertyMapper mapper)
{
return mapper.ApplyConfigurationForAssembly(typeof(ApplicationSieveProcessor).Assembly);
return mapper.ApplyConfigurationsFromAssembly(typeof(ApplicationSieveProcessor).Assembly);
}
}
```

View File

@ -12,9 +12,10 @@ namespace Sieve.Extensions
string fullPropertyName,
PropertyInfo propertyInfo,
bool desc,
bool useThenBy)
bool useThenBy,
bool disableNullableTypeExpression = false)
{
var lambda = GenerateLambdaWithSafeMemberAccess<TEntity>(fullPropertyName, propertyInfo);
var lambda = GenerateLambdaWithSafeMemberAccess<TEntity>(fullPropertyName, propertyInfo, disableNullableTypeExpression);
var command = desc
? (useThenBy ? "ThenByDescending" : "OrderByDescending")
@ -33,7 +34,8 @@ namespace Sieve.Extensions
private static Expression<Func<TEntity, object>> GenerateLambdaWithSafeMemberAccess<TEntity>
(
string fullPropertyName,
PropertyInfo propertyInfo
PropertyInfo propertyInfo,
bool disableNullableTypeExpression
)
{
var parameter = Expression.Parameter(typeof(TEntity), "e");
@ -52,7 +54,7 @@ namespace Sieve.Extensions
propertyValue = Expression.MakeMemberAccess(propertyValue, propertyInfo);
}
if (propertyValue.Type.IsNullable())
if (propertyValue.Type.IsNullable() && !disableNullableTypeExpression)
{
nullCheck = GenerateOrderNullCheckExpression(propertyValue, nullCheck);
}

View File

@ -10,5 +10,6 @@
LessThanOrEqualTo,
Contains,
StartsWith,
EndsWith,
}
}

View File

@ -8,7 +8,7 @@ namespace Sieve.Models
public class FilterTerm : IFilterTerm, IEquatable<FilterTerm>
{
private const string EscapedPipePattern = @"(?<!($|[^\\]|^)(\\\\)*?\\)\|";
private const string OperatorsRegEx = @"(!@=\*|!_=\*|!=\*|!@=|!_=|==\*|@=\*|_=\*|==|!=|>=|<=|>|<|@=|_=)";
private const string OperatorsRegEx = @"(!@=\*|!_=\*|!_-=\*|!=\*|!@=|!_=|!_-=|==\*|@=\*|_=\*|_-=\*|==|!=|>=|<=|>|<|@=|_=|_-=)";
private const string EscapeNegPatternForOper = @"(?<!\\)" + OperatorsRegEx;
private const string EscapePosPatternForOper = @"(?<=\\)" + OperatorsRegEx;
@ -22,13 +22,13 @@ namespace Sieve.Models
{
set
{
var filterSplits = Regex.Split(value,EscapeNegPatternForOper).Select(t => t.Trim()).ToArray();
var filterSplits = Regex.Split(value, EscapeNegPatternForOper).Select(t => t.Trim()).ToArray();
Names = Regex.Split(filterSplits[0], EscapedPipePattern).Select(t => t.Trim()).ToArray();
if (filterSplits.Length > 2)
{
foreach (var match in Regex.Matches(filterSplits[2],EscapePosPatternForOper))
foreach (var match in Regex.Matches(filterSplits[2], EscapePosPatternForOper))
{
var matchStr = match.ToString();
filterSplits[2] = filterSplits[2].Replace('\\' + matchStr, matchStr);
@ -38,8 +38,8 @@ namespace Sieve.Models
.Select(UnEscape)
.ToArray();
}
Operator = Regex.Match(value,EscapeNegPatternForOper).Value;
Operator = Regex.Match(value, EscapeNegPatternForOper).Value;
OperatorParsed = GetOperatorParsed(Operator);
OperatorIsCaseInsensitive = Operator.EndsWith("*");
OperatorIsNegated = OperatorParsed != FilterOperator.NotEquals && Operator.StartsWith("!");
@ -80,6 +80,9 @@ namespace Sieve.Models
case "_=":
case "!_=":
return FilterOperator.StartsWith;
case "_-=":
case "!_-=":
return FilterOperator.EndsWith;
default:
return FilterOperator.Equals;
}

View File

@ -12,6 +12,8 @@ namespace Sieve.Models
public bool IgnoreNullsOnNotEqual { get; set; } = true;
public bool DisableNullableTypeExpressionForSorting { get; set; } = false;
public string CultureNameOfTypeConversion { get; set; } = "en";
}
}

View File

@ -341,6 +341,9 @@ namespace Sieve.Services
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),
_ => Expression.Equal(propertyValue, filterValue)
};
}
@ -367,7 +370,7 @@ namespace Sieve.Services
if (property != null)
{
result = result.OrderByDynamic(fullName, property, sortTerm.Descending, useThenBy);
result = result.OrderByDynamic(fullName, property, sortTerm.Descending, useThenBy, Options.Value.DisableNullableTypeExpressionForSorting);
}
else
{

View File

@ -73,6 +73,16 @@ namespace SieveUnitTests
CategoryId = 2,
TopComment = new Comment { Id = 1, Text = "D1" },
FeaturedComment = new Comment { Id = 7, Text = "D2" }
},
new Post
{
Id = 4,
Title = "Yen",
LikeCount = 5,
IsDraft = true,
CategoryId = 5,
TopComment = new Comment { Id = 4, Text = "Yen3" },
FeaturedComment = new Comment { Id = 8, Text = "Yen4" }
}
}.AsQueryable();
@ -124,7 +134,43 @@ namespace SieveUnitTests
var result = _processor.Apply(model, _posts);
Assert.Equal(1, result.First().Id);
Assert.True(result.Count() == 3);
Assert.True(result.Count() == 4);
}
[Fact]
public void EndsWithWorks()
{
var model = new SieveModel
{
Filters = "Title_-=n"
};
_testOutputHelper.WriteLine(model.GetFiltersParsed()[0].Values.ToString());
_testOutputHelper.WriteLine(model.GetFiltersParsed()[0].Operator);
_testOutputHelper.WriteLine(model.GetFiltersParsed()[0].OperatorParsed.ToString());
var result = _processor.Apply(model, _posts);
Assert.Equal(4, result.First().Id);
Assert.True(result.Count() == 1);
}
[Fact]
public void EndsWithCanBeCaseInsensitive()
{
var model = new SieveModel
{
Filters = "Title_-=*N"
};
_testOutputHelper.WriteLine(model.GetFiltersParsed()[0].Values.ToString());
_testOutputHelper.WriteLine(model.GetFiltersParsed()[0].Operator);
_testOutputHelper.WriteLine(model.GetFiltersParsed()[0].OperatorParsed.ToString());
var result = _processor.Apply(model, _posts);
Assert.Equal(4, result.First().Id);
Assert.True(result.Count() == 1);
}
[Fact]
@ -150,7 +196,7 @@ namespace SieveUnitTests
var result = _processor.Apply(model, _posts);
Assert.True(result.Count() == 3);
Assert.True(result.Count() == 4);
}
[Fact]
@ -205,8 +251,8 @@ namespace SieveUnitTests
var result = _processor.Apply(model, _posts);
var nullableResult = _nullableProcessor.Apply(model, _posts);
Assert.True(result.Count() == 1);
Assert.True(nullableResult.Count() == 2);
Assert.True(result.Count() == 2);
Assert.True(nullableResult.Count() == 3);
}
[Theory]
@ -255,7 +301,7 @@ namespace SieveUnitTests
var result = _processor.Apply(model, _posts);
Assert.False(result.Any(p => p.Id == 0));
Assert.True(result.Count() == 3);
Assert.True(result.Count() == 4);
}
[Fact]
@ -474,11 +520,12 @@ namespace SieveUnitTests
};
var result = _processor.Apply(model, _posts);
Assert.Equal(3, result.Count());
Assert.Equal(4, result.Count());
var posts = result.ToList();
Assert.Contains("B", posts[0].TopComment.Text);
Assert.Contains("C", posts[1].TopComment.Text);
Assert.Contains("D", posts[2].TopComment.Text);
Assert.Contains("Yen", posts[3].TopComment.Text);
}
[Fact]
@ -490,12 +537,13 @@ namespace SieveUnitTests
};
var result = _processor.Apply(model, _posts);
Assert.Equal(4, result.Count());
Assert.Equal(5, result.Count());
var posts = result.ToList();
Assert.Equal(0, posts[0].Id);
Assert.Equal(3, posts[1].Id);
Assert.Equal(2, posts[2].Id);
Assert.Equal(1, posts[3].Id);
Assert.Equal(4, posts[4].Id);
}
[Fact]
@ -630,13 +678,15 @@ namespace SieveUnitTests
};
var result = _processor.Apply(model, _posts);
Assert.Equal(4, result.Count());
Assert.Equal(5, result.Count());
var posts = result.ToList();
Assert.Equal(3,posts[0].Id);
Assert.Equal(2,posts[1].Id);
Assert.Equal(1,posts[2].Id);
Assert.Equal(0,posts[3].Id);
Assert.Equal(4, posts[0].Id);
Assert.Equal(3,posts[1].Id);
Assert.Equal(2,posts[2].Id);
Assert.Equal(1,posts[3].Id);
Assert.Equal(0,posts[4].Id);
}
[Fact]
@ -759,10 +809,13 @@ namespace SieveUnitTests
[InlineData(@"Title@=\>= ")]
[InlineData(@"Title@=\@= ")]
[InlineData(@"Title@=\_= ")]
[InlineData(@"Title@=\_-= ")]
[InlineData(@"Title@=!\@= ")]
[InlineData(@"Title@=!\_= ")]
[InlineData(@"Title@=!\_-= ")]
[InlineData(@"Title@=\@=* ")]
[InlineData(@"Title@=\_=* ")]
[InlineData(@"Title@=\_-=* ")]
[InlineData(@"Title@=\==* ")]
[InlineData(@"Title@=\!=* ")]
[InlineData(@"Title@=!\@=* ")]
@ -773,7 +826,7 @@ namespace SieveUnitTests
new Post
{
Id = 1,
Title = @"Operators: == != > < >= <= @= _= !@= !_= @=* _=* ==* !=* !@=* !_=* ",
Title = @"Operators: == != > < >= <= @= _= _-= !@= !_= !_-= @=* _=* ==* !=* !@=* !_=* !_-=* ",
LikeCount = 1,
IsDraft = true,
CategoryId = 1,

View File

@ -31,7 +31,7 @@ namespace SieveUnitTests
{
Id = 1,
DateCreated = DateTimeOffset.UtcNow,
Text = "null is here in the text",
Text = "null is here twice in the text ending by null",
Author = "Cat",
},
new Comment
@ -136,6 +136,21 @@ namespace SieveUnitTests
Assert.Equal(new[] {1}, result.Select(p => p.Id));
}
[Theory]
[InlineData("Text_-=null")]
[InlineData("Text_-=*null")]
[InlineData("Text_-=*NULL")]
[InlineData("Text_-=*NulL")]
[InlineData("Text_-=*null|text")]
public void Filter_EndsWith_NullString(string filter)
{
var model = new SieveModel { Filters = filter };
var result = _processor.Apply(model, _comments);
Assert.Equal(new[] { 1 }, result.Select(p => p.Id));
}
[Theory]
[InlineData("Text!@=null")]
[InlineData("Text!@=*null")]
@ -164,5 +179,19 @@ namespace SieveUnitTests
Assert.Equal(new[] {0, 2}, result.Select(p => p.Id));
}
[Theory]
[InlineData("Text!_-=null")]
[InlineData("Text!_-=*null")]
[InlineData("Text!_-=*NULL")]
[InlineData("Text!_-=*NulL")]
public void Filter_DoesNotEndsWith_NullString(string filter)
{
var model = new SieveModel { Filters = filter };
var result = _processor.Apply(model, _comments);
Assert.Equal(new[] { 0, 2 }, result.Select(p => p.Id));
}
}
}