fixed 163 issue (#164)

* Added ability to skip operators and '\' with \ in filtering.
* Added unit tests for filtering '\|' and skipping operators
This commit is contained in:
AViktorovGRSE 2021-12-16 21:50:53 +03:00 committed by GitHub
parent 5ef8843f3d
commit 7b6f3c7d85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 127 additions and 33 deletions

View File

@ -6,43 +6,36 @@ namespace Sieve.Models
{ {
public class FilterTerm : IFilterTerm, IEquatable<FilterTerm> public class FilterTerm : IFilterTerm, IEquatable<FilterTerm>
{ {
public FilterTerm() { } private const string EscapedPipePattern = @"(?<!($|[^\\]|^)(\\\\)*?\\)\|";
private const string EscapedPipePattern = @"(?<!($|[^\\])(\\\\)*?\\)\|";
private const string PipeToEscape = @"\|"; private const string PipeToEscape = @"\|";
private const string BackslashToEscape = @"\\";
private static readonly string[] Operators = new string[] { private const string OperatorsRegEx = @"(!@=\*|!_=\*|!=\*|!@=|!_=|==\*|@=\*|_=\*|==|!=|>=|<=|>|<|@=|_=)";
"!@=*", private const string EscapeNegPatternForOper = @"(?<!\\)" + OperatorsRegEx;
"!_=*", private const string EscapePosPatternForOper = @"(?<=\\)" + OperatorsRegEx;
"!=*",
"!@=",
"!_=",
"==*",
"@=*",
"_=*",
"==",
"!=",
">=",
"<=",
">",
"<",
"@=",
"_="
};
public string Filter public string Filter
{ {
set set
{ {
var filterSplits = value.Split(Operators, StringSplitOptions.RemoveEmptyEntries) var filterSplits = Regex.Split(value,EscapeNegPatternForOper).Select(t => t.Trim()).ToArray();
.Select(t => t.Trim()).ToArray();
Names = Regex.Split(filterSplits[0], EscapedPipePattern).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) if (filterSplits.Length > 2)
{
foreach (var match in Regex.Matches(filterSplits[2],EscapePosPatternForOper))
{
var matchStr = match.ToString();
filterSplits[2] = filterSplits[2].Replace('\\' + matchStr, matchStr);
}
Values = Regex.Split(filterSplits[2], EscapedPipePattern)
.Select(t => t.Replace(PipeToEscape, "|").Trim()) .Select(t => t.Replace(PipeToEscape, "|").Trim())
.ToArray() .Select(t => t.Replace(BackslashToEscape, "\\").Trim())
: null; .ToArray();
Operator = Array.Find(Operators, o => value.Contains(o)) ?? "=="; }
Operator = Regex.Match(value,EscapeNegPatternForOper).Value;
OperatorParsed = GetOperatorParsed(Operator); OperatorParsed = GetOperatorParsed(Operator);
OperatorIsCaseInsensitive = Operator.EndsWith("*"); OperatorIsCaseInsensitive = Operator.EndsWith("*");
OperatorIsNegated = OperatorParsed != FilterOperator.NotEquals && Operator.StartsWith("!"); OperatorIsNegated = OperatorParsed != FilterOperator.NotEquals && Operator.StartsWith("!");

View File

@ -73,7 +73,7 @@ namespace SieveUnitTests
CategoryId = 2, CategoryId = 2,
TopComment = new Comment { Id = 1, Text = "D1" }, TopComment = new Comment { Id = 1, Text = "D1" },
FeaturedComment = new Comment { Id = 7, Text = "D2" } FeaturedComment = new Comment { Id = 7, Text = "D2" }
}, }
}.AsQueryable(); }.AsQueryable();
_comments = new List<Comment> _comments = new List<Comment>
@ -684,15 +684,116 @@ namespace SieveUnitTests
DateCreated = DateTimeOffset.UtcNow.AddDays(-1), DateCreated = DateTimeOffset.UtcNow.AddDays(-1),
Text = "Here is | another comment" Text = "Here is | another comment"
}, },
new Comment
{
Id = 2,
DateCreated = DateTimeOffset.UtcNow.AddDays(-1),
Text = @"Here is \| another comment(1)"
}
}.AsQueryable(); }.AsQueryable();
var model = new SieveModel() var model = new SieveModel
{ {
Filters = "Text==Here is \\| a comment|Here is \\| another comment", Filters = @"Text==Here is \| a comment|Here is \| another comment|Here is \\\| another comment(1)",
}; };
var result = _processor.Apply(model, comments); var result = _processor.Apply(model, comments);
Assert.Equal(2, result.Count()); Assert.Equal(3, result.Count());
} }
[Theory]
[InlineData("CategoryId==1,(CategoryId|LikeCount)==50")]
[InlineData("(CategoryId|LikeCount)==50,CategoryId==1")]
public void CanFilterWithEscape(string filter)
{
var model = new SieveModel
{
Filters = filter
};
var result = _processor.Apply(model, _posts);
var entry = result.FirstOrDefault();
var resultCount = result.Count();
Assert.NotNull(entry);
Assert.Equal(1, resultCount);
}
[Theory]
[InlineData(@"Title@=\\")]
public void CanFilterWithEscapedBackSlash(string filter)
{
var posts = new List<Post>
{
new Post
{
Id = 1,
Title = "E\\",
LikeCount = 4,
IsDraft = true,
CategoryId = 1,
TopComment = new Comment { Id = 1, Text = "E1" },
FeaturedComment = new Comment { Id = 7, Text = "E2" }
}
}.AsQueryable();
var model = new SieveModel
{
Filters = filter
};
var result = _processor.Apply(model, posts);
var entry = result.FirstOrDefault();
var resultCount = result.Count();
Assert.NotNull(entry);
Assert.Equal(1, resultCount);
}
[Theory]
[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@=\==* ")]
[InlineData(@"Title@=\!=* ")]
[InlineData(@"Title@=!\@=* ")]
public void CanFilterWithEscapedOperators(string filter)
{
var posts = new List<Post>
{
new Post
{
Id = 1,
Title = @"Operators: == != > < >= <= @= _= !@= !_= @=* _=* ==* !=* !@=* !_=* ",
LikeCount = 1,
IsDraft = true,
CategoryId = 1,
TopComment = new Comment { Id = 1, Text = "F1" },
FeaturedComment = new Comment { Id = 7, Text = "F2" }
}
}.AsQueryable();
var model = new SieveModel
{
Filters = filter,
};
var result = _processor.Apply(model, posts);
var entry = result.FirstOrDefault();
var resultCount = result.Count();
Assert.NotNull(entry);
Assert.Equal(1, resultCount);
}
} }
} }