2023-08-11 12:47:59 -04:00
|
|
|
using System;
|
2023-08-11 13:16:43 -04:00
|
|
|
using System.Text.RegularExpressions;
|
2023-08-11 12:47:59 -04:00
|
|
|
using SabreTools.Models.Internal;
|
|
|
|
|
|
|
|
|
|
namespace SabreTools.Filter
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Represents a single filtering object
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class FilterObject
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Key name for the filter
|
|
|
|
|
/// </summary>
|
2023-08-11 13:16:43 -04:00
|
|
|
public string[] Key { get; init; }
|
2023-08-11 12:47:59 -04:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Value to match in the filter
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string? Value { get; init; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Operation on how to match the filter
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Operation Operation { get; init; }
|
|
|
|
|
|
2023-08-11 13:16:43 -04:00
|
|
|
public FilterObject(string filterString)
|
2023-08-11 12:47:59 -04:00
|
|
|
{
|
2023-08-11 13:16:43 -04:00
|
|
|
(string? keyItem, Operation operation, string? value) = SplitFilterString(filterString);
|
|
|
|
|
if (keyItem == null)
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(filterString));
|
2023-08-11 12:47:59 -04:00
|
|
|
|
2023-08-11 13:16:43 -04:00
|
|
|
(string? itemName, string? fieldName) = FilterParser.ParseFilterId(keyItem);
|
|
|
|
|
if (itemName == null || fieldName == null)
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(filterString));
|
2023-08-11 12:47:59 -04:00
|
|
|
|
2023-08-11 13:16:43 -04:00
|
|
|
this.Key = new string[] { itemName, fieldName };
|
|
|
|
|
this.Value = value;
|
2023-08-11 12:47:59 -04:00
|
|
|
this.Operation = operation;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-11 13:16:43 -04:00
|
|
|
public FilterObject(string itemField, string? value, string? operation)
|
2023-08-11 12:47:59 -04:00
|
|
|
{
|
2023-08-11 13:16:43 -04:00
|
|
|
(string? itemName, string? fieldName) = FilterParser.ParseFilterId(itemField);
|
|
|
|
|
if (itemName == null || fieldName == null)
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(value));
|
2023-08-11 12:47:59 -04:00
|
|
|
|
2023-08-11 13:16:43 -04:00
|
|
|
this.Key = new string[] { itemName, fieldName };
|
|
|
|
|
this.Value = value;
|
2023-08-11 12:47:59 -04:00
|
|
|
this.Operation = GetOperation(operation);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-11 13:16:43 -04:00
|
|
|
public FilterObject(string itemField, string? value, Operation operation)
|
2023-08-11 12:47:59 -04:00
|
|
|
{
|
2023-08-11 13:16:43 -04:00
|
|
|
(string? itemName, string? fieldName) = FilterParser.ParseFilterId(itemField);
|
|
|
|
|
if (itemName == null || fieldName == null)
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(value));
|
2023-08-11 12:47:59 -04:00
|
|
|
|
2023-08-11 13:16:43 -04:00
|
|
|
this.Key = new string[] { itemName, fieldName };
|
|
|
|
|
this.Value = value;
|
2023-08-11 12:47:59 -04:00
|
|
|
this.Operation = operation;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region Matching
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determine if a DictionaryBase object matches the key and value
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool Matches(DictionaryBase dictionaryBase)
|
|
|
|
|
{
|
2023-08-11 13:16:43 -04:00
|
|
|
// TODO: Add validation of dictionary base type from this.Key[0]
|
2023-08-11 12:47:59 -04:00
|
|
|
return this.Operation switch
|
|
|
|
|
{
|
|
|
|
|
Operation.Equals => MatchesEqual(dictionaryBase),
|
|
|
|
|
Operation.NotEquals => MatchesNotEqual(dictionaryBase),
|
|
|
|
|
Operation.GreaterThan => MatchesGreaterThan(dictionaryBase),
|
|
|
|
|
Operation.GreaterThanOrEqual => MatchesGreaterThanOrEqual(dictionaryBase),
|
|
|
|
|
Operation.LessThan => MatchesLessThan(dictionaryBase),
|
|
|
|
|
Operation.LessThanOrEqual => MatchesLessThanOrEqual(dictionaryBase),
|
|
|
|
|
_ => false,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines if a value matches exactly
|
|
|
|
|
/// </summary>
|
2023-08-11 14:30:31 -04:00
|
|
|
/// <remarks>
|
|
|
|
|
/// TODO: Add regex matching to this method
|
|
|
|
|
/// TODO: Add logic to convert SI suffixes and hex
|
|
|
|
|
/// </remarks>
|
2023-08-11 12:47:59 -04:00
|
|
|
private bool MatchesEqual(DictionaryBase dictionaryBase)
|
|
|
|
|
{
|
2023-08-11 13:16:43 -04:00
|
|
|
if (!dictionaryBase.ContainsKey(this.Key[1]))
|
2023-08-11 12:47:59 -04:00
|
|
|
return this.Value == null;
|
|
|
|
|
|
2023-08-11 13:16:43 -04:00
|
|
|
string? checkValue = dictionaryBase.ReadString(this.Key[1]);
|
2023-08-11 12:47:59 -04:00
|
|
|
return checkValue == this.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines if a value does not match exactly
|
|
|
|
|
/// </summary>
|
2023-08-11 14:30:31 -04:00
|
|
|
/// <remarks>
|
|
|
|
|
/// TODO: Add regex matching to this method
|
|
|
|
|
/// TODO: Add logic to convert SI suffixes and hex
|
|
|
|
|
/// </remarks>
|
2023-08-11 12:47:59 -04:00
|
|
|
private bool MatchesNotEqual(DictionaryBase dictionaryBase)
|
|
|
|
|
{
|
2023-08-11 13:16:43 -04:00
|
|
|
if (!dictionaryBase.ContainsKey(this.Key[1]))
|
2023-08-11 12:47:59 -04:00
|
|
|
return this.Value != null;
|
|
|
|
|
|
2023-08-11 13:16:43 -04:00
|
|
|
string? checkValue = dictionaryBase.ReadString(this.Key[1]);
|
2023-08-11 12:47:59 -04:00
|
|
|
return checkValue != this.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines if a value is strictly greater than
|
|
|
|
|
/// </summary>
|
2023-08-11 14:30:31 -04:00
|
|
|
/// <remarks>TODO: Add logic to convert SI suffixes and hex</remarks>
|
2023-08-11 12:47:59 -04:00
|
|
|
private bool MatchesGreaterThan(DictionaryBase dictionaryBase)
|
|
|
|
|
{
|
2023-08-11 13:16:43 -04:00
|
|
|
if (!dictionaryBase.ContainsKey(this.Key[1]))
|
2023-08-11 12:47:59 -04:00
|
|
|
return false;
|
|
|
|
|
|
2023-08-11 13:16:43 -04:00
|
|
|
long? checkLongValue = dictionaryBase.ReadLong(this.Key[1]);
|
2023-08-11 12:47:59 -04:00
|
|
|
if (checkLongValue != null)
|
|
|
|
|
{
|
|
|
|
|
if (!long.TryParse(this.Value, out long matchValue))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return checkLongValue > matchValue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-11 13:16:43 -04:00
|
|
|
double? checkDoubleValue = dictionaryBase.ReadDouble(this.Key[1]);
|
2023-08-11 12:47:59 -04:00
|
|
|
if (checkDoubleValue != null)
|
|
|
|
|
{
|
|
|
|
|
if (!double.TryParse(this.Value, out double matchValue))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return checkDoubleValue > matchValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines if a value is greater than or equal
|
|
|
|
|
/// </summary>
|
2023-08-11 14:30:31 -04:00
|
|
|
/// <remarks>TODO: Add logic to convert SI suffixes and hex</remarks>
|
2023-08-11 12:47:59 -04:00
|
|
|
private bool MatchesGreaterThanOrEqual(DictionaryBase dictionaryBase)
|
|
|
|
|
{
|
2023-08-11 13:16:43 -04:00
|
|
|
if (!dictionaryBase.ContainsKey(this.Key[1]))
|
2023-08-11 12:47:59 -04:00
|
|
|
return false;
|
|
|
|
|
|
2023-08-11 13:16:43 -04:00
|
|
|
long? checkLongValue = dictionaryBase.ReadLong(this.Key[1]);
|
2023-08-11 12:47:59 -04:00
|
|
|
if (checkLongValue != null)
|
|
|
|
|
{
|
|
|
|
|
if (!long.TryParse(this.Value, out long matchValue))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return checkLongValue >= matchValue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-11 13:16:43 -04:00
|
|
|
double? checkDoubleValue = dictionaryBase.ReadDouble(this.Key[1]);
|
2023-08-11 12:47:59 -04:00
|
|
|
if (checkDoubleValue != null)
|
|
|
|
|
{
|
|
|
|
|
if (!double.TryParse(this.Value, out double matchValue))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return checkDoubleValue >= matchValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-08-11 13:16:43 -04:00
|
|
|
|
2023-08-11 12:47:59 -04:00
|
|
|
/// <summary>
|
|
|
|
|
/// Determines if a value is strictly less than
|
|
|
|
|
/// </summary>
|
2023-08-11 14:30:31 -04:00
|
|
|
/// <remarks>TODO: Add logic to convert SI suffixes and hex</remarks>
|
2023-08-11 12:47:59 -04:00
|
|
|
private bool MatchesLessThan(DictionaryBase dictionaryBase)
|
|
|
|
|
{
|
2023-08-11 13:16:43 -04:00
|
|
|
if (!dictionaryBase.ContainsKey(this.Key[1]))
|
2023-08-11 12:47:59 -04:00
|
|
|
return false;
|
|
|
|
|
|
2023-08-11 13:16:43 -04:00
|
|
|
long? checkLongValue = dictionaryBase.ReadLong(this.Key[1]);
|
2023-08-11 12:47:59 -04:00
|
|
|
if (checkLongValue != null)
|
|
|
|
|
{
|
|
|
|
|
if (!long.TryParse(this.Value, out long matchValue))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return checkLongValue < matchValue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-11 13:16:43 -04:00
|
|
|
double? checkDoubleValue = dictionaryBase.ReadDouble(this.Key[1]);
|
2023-08-11 12:47:59 -04:00
|
|
|
if (checkDoubleValue != null)
|
|
|
|
|
{
|
|
|
|
|
if (!double.TryParse(this.Value, out double matchValue))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return checkDoubleValue < matchValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines if a value is less than or equal
|
|
|
|
|
/// </summary>
|
2023-08-11 14:30:31 -04:00
|
|
|
/// <remarks>TODO: Add logic to convert SI suffixes and hex</remarks>
|
2023-08-11 12:47:59 -04:00
|
|
|
private bool MatchesLessThanOrEqual(DictionaryBase dictionaryBase)
|
|
|
|
|
{
|
2023-08-11 13:16:43 -04:00
|
|
|
if (!dictionaryBase.ContainsKey(this.Key[1]))
|
2023-08-11 12:47:59 -04:00
|
|
|
return false;
|
|
|
|
|
|
2023-08-11 13:16:43 -04:00
|
|
|
long? checkLongValue = dictionaryBase.ReadLong(this.Key[1]);
|
2023-08-11 12:47:59 -04:00
|
|
|
if (checkLongValue != null)
|
|
|
|
|
{
|
|
|
|
|
if (!long.TryParse(this.Value, out long matchValue))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return checkLongValue <= matchValue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-11 13:16:43 -04:00
|
|
|
double? checkDoubleValue = dictionaryBase.ReadDouble(this.Key[1]);
|
2023-08-11 12:47:59 -04:00
|
|
|
if (checkDoubleValue != null)
|
|
|
|
|
{
|
|
|
|
|
if (!double.TryParse(this.Value, out double matchValue))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return checkDoubleValue <= matchValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-08-11 13:16:43 -04:00
|
|
|
|
2023-08-11 12:47:59 -04:00
|
|
|
#endregion
|
2023-08-11 13:34:28 -04:00
|
|
|
|
|
|
|
|
#region Helpers
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Derive an operation from the input string, if possible
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static Operation GetOperation(string? operation)
|
|
|
|
|
{
|
|
|
|
|
return operation?.ToLowerInvariant() switch
|
|
|
|
|
{
|
|
|
|
|
"=" => Operation.Equals,
|
|
|
|
|
"==" => Operation.Equals,
|
|
|
|
|
":" => Operation.Equals,
|
|
|
|
|
"::" => Operation.Equals,
|
|
|
|
|
|
|
|
|
|
"!" => Operation.NotEquals,
|
|
|
|
|
"!=" => Operation.NotEquals,
|
|
|
|
|
"!:" => Operation.NotEquals,
|
|
|
|
|
|
|
|
|
|
">" => Operation.GreaterThan,
|
|
|
|
|
">=" => Operation.GreaterThanOrEqual,
|
|
|
|
|
|
|
|
|
|
"<" => Operation.LessThan,
|
|
|
|
|
"<=" => Operation.LessThanOrEqual,
|
|
|
|
|
|
|
|
|
|
_ => Operation.NONE,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Derive a key, operation, and value from the input string, if possible
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static (string?, Operation, string?) SplitFilterString(string? filterString)
|
|
|
|
|
{
|
|
|
|
|
if (filterString == null)
|
|
|
|
|
return (null, Operation.NONE, null);
|
|
|
|
|
|
|
|
|
|
// Split the string using regex
|
|
|
|
|
var match = Regex.Match(filterString, @"^(?<itemField>[a-zA-Z.]+)(?<operation>[=!:><]{1,2})(?<value>.*)$");
|
|
|
|
|
if (!match.Success)
|
|
|
|
|
return (null, Operation.NONE, null);
|
|
|
|
|
|
|
|
|
|
string itemField = match.Groups["itemField"].Value;
|
|
|
|
|
Operation operation = GetOperation(match.Groups["operation"].Value);
|
|
|
|
|
string value = match.Groups["value"].Value;
|
|
|
|
|
|
|
|
|
|
return (itemField, operation, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2023-08-11 12:47:59 -04:00
|
|
|
}
|
|
|
|
|
}
|