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

#80 added support for escaping pipe control characters #113

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 6 additions & 2 deletions Sieve/Models/FilterTerm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class FilterTerm : IFilterTerm, IEquatable<FilterTerm>
public FilterTerm() { }

private const string EscapedPipePattern = @"(?<!($|[^\\])(\\\\)*?\\)\|";
private const string PipeToEscape = @"\|";

private static readonly string[] Operators = new string[] {
"!@=*",
Expand Down Expand Up @@ -36,7 +37,11 @@ public string Filter
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;
Values = filterSplits.Length > 1
? Regex.Split(filterSplits[1], EscapedPipePattern)
.Select(t => t.Replace(PipeToEscape, "|").Trim())
.ToArray()
: null;
Operator = Array.Find(Operators, o => value.Contains(o)) ?? "==";
OperatorParsed = GetOperatorParsed(Operator);
OperatorIsCaseInsensitive = Operator.EndsWith("*");
Expand Down Expand Up @@ -90,6 +95,5 @@ public bool Equals(FilterTerm other)
&& Values.SequenceEqual(other.Values)
&& Operator == other.Operator;
}

}
}
12 changes: 8 additions & 4 deletions Sieve/Models/SieveModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class SieveModel<TFilterTerm, TSortTerm> : ISieveModel<TFilterTerm, TSort
where TSortTerm : ISortTerm, new()
{
private const string EscapedCommaPattern = @"(?<!($|[^\\])(\\\\)*?\\),\s*";
private const string CommaToEscape = @"\,";

[DataMember]
public string Filters { get; set; }
Expand All @@ -36,10 +37,13 @@ public List<TFilterTerm> GetFiltersParsed()
{
if (string.IsNullOrWhiteSpace(filter)) continue;

if (filter.StartsWith("("))
var escapedFilter = filter
.Replace(CommaToEscape, ",");

if (escapedFilter.StartsWith("("))
{
var filterOpAndVal = filter.Substring(filter.LastIndexOf(")") + 1);
var subfilters = filter.Replace(filterOpAndVal, "").Replace("(", "").Replace(")", "");
var filterOpAndVal = escapedFilter.Substring(escapedFilter.LastIndexOf(")") + 1);
var subfilters = escapedFilter.Replace(filterOpAndVal, "").Replace("(", "").Replace(")", "");
var filterTerm = new TFilterTerm
{
Filter = subfilters + filterOpAndVal
Expand All @@ -50,7 +54,7 @@ public List<TFilterTerm> GetFiltersParsed()
{
var filterTerm = new TFilterTerm
{
Filter = filter
Filter = escapedFilter
};
value.Add(filterTerm);
}
Expand Down
56 changes: 56 additions & 0 deletions SieveUnitTests/General.cs
Original file line number Diff line number Diff line change
Expand Up @@ -583,5 +583,61 @@ public void BaseDefinedPropertyMappingSortingWorks_WithCustomName()
Assert.AreEqual(posts[2].Id, 1);
Assert.AreEqual(posts[3].Id, 0);
}

[TestMethod]
public void CanFilter_WithEscapeCharacter()
{
var comments = new List<Comment>
{
new Comment
{
Id = 0,
DateCreated = DateTimeOffset.UtcNow,
Text = "Here is, a comment"
},
new Comment
{
Id = 1,
DateCreated = DateTimeOffset.UtcNow.AddDays(-1),
Text = "Here is, another comment"
},
}.AsQueryable();

var model = new SieveModel
{
Filters = "Text==Here is\\, another comment"
};

var result = _processor.Apply(model, comments);
Assert.AreEqual(1, result.Count());
}

[TestMethod]
public void OrEscapedPipeValueFilteringWorks()
{
var comments = new List<Comment>
{
new Comment
{
Id = 0,
DateCreated = DateTimeOffset.UtcNow,
Text = "Here is | a comment"
},
new Comment
{
Id = 1,
DateCreated = DateTimeOffset.UtcNow.AddDays(-1),
Text = "Here is | another comment"
},
}.AsQueryable();

var model = new SieveModel()
{
Filters = "Text==Here is \\| a comment|Here is \\| another comment",
};

var result = _processor.Apply(model, comments);
Assert.AreEqual(2, result.Count());
}
}
}