| | 1 | | using Fluorite.Strainer.Models.Filtering.Terms; |
| | 2 | | using System.Text.RegularExpressions; |
| | 3 | |
|
| | 4 | | namespace Fluorite.Strainer.Services.Filtering; |
| | 5 | |
|
| | 6 | | public class FilterTermParser : IFilterTermParser |
| | 7 | | { |
| | 8 | | private const string EscapedCommaPattern = @"(?<!($|[^\\])(\\\\)*?\\),"; |
| | 9 | | private const string EscapedPipePattern = @"(?<!($|[^\\])(\\\\)*?\\)\|"; |
| | 10 | |
|
| | 11 | | private readonly IFilterOperatorParser _operatorParser; |
| | 12 | | private readonly IFilterTermNamesParser _namesParser; |
| | 13 | | private readonly IFilterTermValuesParser _valuesParser; |
| | 14 | | private readonly IFilterTermSectionsParser _termSectionsParser; |
| | 15 | |
|
| 15 | 16 | | public FilterTermParser( |
| 15 | 17 | | IFilterOperatorParser operatorParser, |
| 15 | 18 | | IFilterTermNamesParser namesParser, |
| 15 | 19 | | IFilterTermValuesParser valuesParser, |
| 15 | 20 | | IFilterTermSectionsParser termSectionsParser) |
| | 21 | | { |
| 15 | 22 | | _operatorParser = Guard.Against.Null(operatorParser); |
| 15 | 23 | | _namesParser = Guard.Against.Null(namesParser); |
| 15 | 24 | | _valuesParser = Guard.Against.Null(valuesParser); |
| 15 | 25 | | _termSectionsParser = Guard.Against.Null(termSectionsParser); |
| 15 | 26 | | } |
| | 27 | |
|
| | 28 | | public IList<IFilterTerm> GetParsedTerms(string? input) |
| | 29 | | { |
| 14 | 30 | | if (string.IsNullOrWhiteSpace(input)) |
| | 31 | | { |
| 3 | 32 | | return new List<IFilterTerm>(); |
| | 33 | | } |
| | 34 | |
|
| 11 | 35 | | var terms = new List<IFilterTerm>(); |
| 11 | 36 | | var inputParts = Regex.Split(input, EscapedCommaPattern, RegexOptions.None, TimeSpan.FromMilliseconds(100)); |
| | 37 | |
|
| 48 | 38 | | foreach (var filterPart in inputParts) |
| | 39 | | { |
| 13 | 40 | | if (string.IsNullOrWhiteSpace(filterPart)) |
| | 41 | | { |
| | 42 | | continue; |
| | 43 | | } |
| | 44 | |
|
| 11 | 45 | | string filterTermInput = filterPart; |
| | 46 | |
|
| 11 | 47 | | if (filterPart.StartsWith("(")) |
| | 48 | | { |
| 1 | 49 | | var filterOperatorAndValue = ParseFilterOperatorAndValue(filterPart); |
| 1 | 50 | | var subfilters = ParseSubfilters(filterPart, filterOperatorAndValue); |
| 1 | 51 | | filterTermInput = subfilters + filterOperatorAndValue; |
| | 52 | | } |
| | 53 | |
|
| 11 | 54 | | var filterTerm = ParseFilterTerm(filterTermInput); |
| 11 | 55 | | if (filterTerm is null) |
| | 56 | | { |
| | 57 | | continue; |
| | 58 | | } |
| | 59 | |
|
| 9 | 60 | | terms.Add(filterTerm); |
| | 61 | | } |
| | 62 | |
|
| 11 | 63 | | return terms; |
| | 64 | | } |
| | 65 | |
|
| | 66 | | private IFilterTerm? ParseFilterTerm(string input) |
| | 67 | | { |
| 11 | 68 | | var sections = _termSectionsParser.Parse(input); |
| 11 | 69 | | var names = _namesParser.Parse(sections.Names); |
| | 70 | |
|
| 11 | 71 | | if (!names.Any()) |
| | 72 | | { |
| 2 | 73 | | return null; |
| | 74 | | } |
| | 75 | |
|
| 9 | 76 | | var values = _valuesParser.Parse(sections.Values); |
| 9 | 77 | | var operatorParsed = _operatorParser.GetParsedOperator(sections.OperatorSymbol); |
| | 78 | |
|
| 9 | 79 | | return new FilterTerm(input) |
| 9 | 80 | | { |
| 9 | 81 | | Names = names, |
| 9 | 82 | | Operator = operatorParsed, |
| 9 | 83 | | Values = values, |
| 9 | 84 | | }; |
| | 85 | | } |
| | 86 | |
|
| | 87 | | private string ParseFilterOperatorAndValue(string filter) |
| | 88 | | { |
| 1 | 89 | | return filter.Substring(filter.LastIndexOf(")") + 1); |
| | 90 | | } |
| | 91 | |
|
| | 92 | | private string ParseSubfilters(string filter, string filterOpAndVal) |
| | 93 | | { |
| 1 | 94 | | return filter |
| 1 | 95 | | .Replace(filterOpAndVal, string.Empty) |
| 1 | 96 | | .Replace("(", string.Empty) |
| 1 | 97 | | .Replace(")", string.Empty); |
| | 98 | | } |
| | 99 | | } |