mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Fix faulty constructor logic for Filter
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
using SabreTools.Models.Internal;
|
using SabreTools.Models.Internal;
|
||||||
|
|
||||||
namespace SabreTools.Filter
|
namespace SabreTools.Filter
|
||||||
@@ -11,7 +12,7 @@ namespace SabreTools.Filter
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Key name for the filter
|
/// Key name for the filter
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string Key { get; init; }
|
public string[] Key { get; init; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Value to match in the filter
|
/// Value to match in the filter
|
||||||
@@ -23,47 +24,40 @@ namespace SabreTools.Filter
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public Operation Operation { get; init; }
|
public Operation Operation { get; init; }
|
||||||
|
|
||||||
public FilterObject(string keyValue, string? operation)
|
public FilterObject(string filterString)
|
||||||
{
|
{
|
||||||
(string? itemName, string? fieldName) = FilterParser.ParseFilterId(keyValue);
|
(string? keyItem, Operation operation, string? value) = SplitFilterString(filterString);
|
||||||
if (itemName == null)
|
if (keyItem == null)
|
||||||
throw new ArgumentOutOfRangeException(nameof(keyValue));
|
throw new ArgumentOutOfRangeException(nameof(filterString));
|
||||||
|
|
||||||
this.Key = itemName;
|
(string? itemName, string? fieldName) = FilterParser.ParseFilterId(keyItem);
|
||||||
this.Value = fieldName;
|
if (itemName == null || fieldName == null)
|
||||||
this.Operation = GetOperation(operation);
|
throw new ArgumentOutOfRangeException(nameof(filterString));
|
||||||
}
|
|
||||||
|
|
||||||
public FilterObject(string keyValue, Operation operation)
|
this.Key = new string[] { itemName, fieldName };
|
||||||
{
|
this.Value = value;
|
||||||
(string? itemName, string? fieldName) = FilterParser.ParseFilterId(keyValue);
|
|
||||||
if (itemName == null)
|
|
||||||
throw new ArgumentOutOfRangeException(nameof(keyValue));
|
|
||||||
|
|
||||||
this.Key = itemName;
|
|
||||||
this.Value = fieldName;
|
|
||||||
this.Operation = operation;
|
this.Operation = operation;
|
||||||
}
|
}
|
||||||
|
|
||||||
public FilterObject(string key, string? value, string? operation)
|
public FilterObject(string itemField, string? value, string? operation)
|
||||||
{
|
{
|
||||||
(string? itemName, string? fieldName) = FilterParser.ParseFilterId(key, value);
|
(string? itemName, string? fieldName) = FilterParser.ParseFilterId(itemField);
|
||||||
if (itemName == null)
|
if (itemName == null || fieldName == null)
|
||||||
throw new ArgumentOutOfRangeException(nameof(fieldName));
|
throw new ArgumentOutOfRangeException(nameof(value));
|
||||||
|
|
||||||
this.Key = itemName;
|
this.Key = new string[] { itemName, fieldName };
|
||||||
this.Value = fieldName;
|
this.Value = value;
|
||||||
this.Operation = GetOperation(operation);
|
this.Operation = GetOperation(operation);
|
||||||
}
|
}
|
||||||
|
|
||||||
public FilterObject(string key, string? value, Operation operation)
|
public FilterObject(string itemField, string? value, Operation operation)
|
||||||
{
|
{
|
||||||
(string? itemName, string? fieldName) = FilterParser.ParseFilterId(key, value);
|
(string? itemName, string? fieldName) = FilterParser.ParseFilterId(itemField);
|
||||||
if (itemName == null)
|
if (itemName == null || fieldName == null)
|
||||||
throw new ArgumentOutOfRangeException(nameof(fieldName));
|
throw new ArgumentOutOfRangeException(nameof(value));
|
||||||
|
|
||||||
this.Key = itemName;
|
this.Key = new string[] { itemName, fieldName };
|
||||||
this.Value = fieldName;
|
this.Value = value;
|
||||||
this.Operation = operation;
|
this.Operation = operation;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,6 +87,26 @@ namespace SabreTools.Filter
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <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);
|
||||||
|
}
|
||||||
|
|
||||||
#region Matching
|
#region Matching
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -100,6 +114,7 @@ namespace SabreTools.Filter
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public bool Matches(DictionaryBase dictionaryBase)
|
public bool Matches(DictionaryBase dictionaryBase)
|
||||||
{
|
{
|
||||||
|
// TODO: Add validation of dictionary base type from this.Key[0]
|
||||||
return this.Operation switch
|
return this.Operation switch
|
||||||
{
|
{
|
||||||
Operation.Equals => MatchesEqual(dictionaryBase),
|
Operation.Equals => MatchesEqual(dictionaryBase),
|
||||||
@@ -118,10 +133,10 @@ namespace SabreTools.Filter
|
|||||||
/// <remarks>TODO: Add regex matching to this method</remarks>
|
/// <remarks>TODO: Add regex matching to this method</remarks>
|
||||||
private bool MatchesEqual(DictionaryBase dictionaryBase)
|
private bool MatchesEqual(DictionaryBase dictionaryBase)
|
||||||
{
|
{
|
||||||
if (!dictionaryBase.ContainsKey(this.Key))
|
if (!dictionaryBase.ContainsKey(this.Key[1]))
|
||||||
return this.Value == null;
|
return this.Value == null;
|
||||||
|
|
||||||
string? checkValue = dictionaryBase.ReadString(this.Key);
|
string? checkValue = dictionaryBase.ReadString(this.Key[1]);
|
||||||
return checkValue == this.Value;
|
return checkValue == this.Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,10 +146,10 @@ namespace SabreTools.Filter
|
|||||||
/// <remarks>TODO: Add regex matching to this method</remarks>
|
/// <remarks>TODO: Add regex matching to this method</remarks>
|
||||||
private bool MatchesNotEqual(DictionaryBase dictionaryBase)
|
private bool MatchesNotEqual(DictionaryBase dictionaryBase)
|
||||||
{
|
{
|
||||||
if (!dictionaryBase.ContainsKey(this.Key))
|
if (!dictionaryBase.ContainsKey(this.Key[1]))
|
||||||
return this.Value != null;
|
return this.Value != null;
|
||||||
|
|
||||||
string? checkValue = dictionaryBase.ReadString(this.Key);
|
string? checkValue = dictionaryBase.ReadString(this.Key[1]);
|
||||||
return checkValue != this.Value;
|
return checkValue != this.Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -143,10 +158,10 @@ namespace SabreTools.Filter
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private bool MatchesGreaterThan(DictionaryBase dictionaryBase)
|
private bool MatchesGreaterThan(DictionaryBase dictionaryBase)
|
||||||
{
|
{
|
||||||
if (!dictionaryBase.ContainsKey(this.Key))
|
if (!dictionaryBase.ContainsKey(this.Key[1]))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
long? checkLongValue = dictionaryBase.ReadLong(this.Key);
|
long? checkLongValue = dictionaryBase.ReadLong(this.Key[1]);
|
||||||
if (checkLongValue != null)
|
if (checkLongValue != null)
|
||||||
{
|
{
|
||||||
if (!long.TryParse(this.Value, out long matchValue))
|
if (!long.TryParse(this.Value, out long matchValue))
|
||||||
@@ -155,7 +170,7 @@ namespace SabreTools.Filter
|
|||||||
return checkLongValue > matchValue;
|
return checkLongValue > matchValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
double? checkDoubleValue = dictionaryBase.ReadDouble(this.Key);
|
double? checkDoubleValue = dictionaryBase.ReadDouble(this.Key[1]);
|
||||||
if (checkDoubleValue != null)
|
if (checkDoubleValue != null)
|
||||||
{
|
{
|
||||||
if (!double.TryParse(this.Value, out double matchValue))
|
if (!double.TryParse(this.Value, out double matchValue))
|
||||||
@@ -172,10 +187,10 @@ namespace SabreTools.Filter
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private bool MatchesGreaterThanOrEqual(DictionaryBase dictionaryBase)
|
private bool MatchesGreaterThanOrEqual(DictionaryBase dictionaryBase)
|
||||||
{
|
{
|
||||||
if (!dictionaryBase.ContainsKey(this.Key))
|
if (!dictionaryBase.ContainsKey(this.Key[1]))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
long? checkLongValue = dictionaryBase.ReadLong(this.Key);
|
long? checkLongValue = dictionaryBase.ReadLong(this.Key[1]);
|
||||||
if (checkLongValue != null)
|
if (checkLongValue != null)
|
||||||
{
|
{
|
||||||
if (!long.TryParse(this.Value, out long matchValue))
|
if (!long.TryParse(this.Value, out long matchValue))
|
||||||
@@ -184,7 +199,7 @@ namespace SabreTools.Filter
|
|||||||
return checkLongValue >= matchValue;
|
return checkLongValue >= matchValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
double? checkDoubleValue = dictionaryBase.ReadDouble(this.Key);
|
double? checkDoubleValue = dictionaryBase.ReadDouble(this.Key[1]);
|
||||||
if (checkDoubleValue != null)
|
if (checkDoubleValue != null)
|
||||||
{
|
{
|
||||||
if (!double.TryParse(this.Value, out double matchValue))
|
if (!double.TryParse(this.Value, out double matchValue))
|
||||||
@@ -195,16 +210,16 @@ namespace SabreTools.Filter
|
|||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Determines if a value is strictly less than
|
/// Determines if a value is strictly less than
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private bool MatchesLessThan(DictionaryBase dictionaryBase)
|
private bool MatchesLessThan(DictionaryBase dictionaryBase)
|
||||||
{
|
{
|
||||||
if (!dictionaryBase.ContainsKey(this.Key))
|
if (!dictionaryBase.ContainsKey(this.Key[1]))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
long? checkLongValue = dictionaryBase.ReadLong(this.Key);
|
long? checkLongValue = dictionaryBase.ReadLong(this.Key[1]);
|
||||||
if (checkLongValue != null)
|
if (checkLongValue != null)
|
||||||
{
|
{
|
||||||
if (!long.TryParse(this.Value, out long matchValue))
|
if (!long.TryParse(this.Value, out long matchValue))
|
||||||
@@ -213,7 +228,7 @@ namespace SabreTools.Filter
|
|||||||
return checkLongValue < matchValue;
|
return checkLongValue < matchValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
double? checkDoubleValue = dictionaryBase.ReadDouble(this.Key);
|
double? checkDoubleValue = dictionaryBase.ReadDouble(this.Key[1]);
|
||||||
if (checkDoubleValue != null)
|
if (checkDoubleValue != null)
|
||||||
{
|
{
|
||||||
if (!double.TryParse(this.Value, out double matchValue))
|
if (!double.TryParse(this.Value, out double matchValue))
|
||||||
@@ -230,10 +245,10 @@ namespace SabreTools.Filter
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private bool MatchesLessThanOrEqual(DictionaryBase dictionaryBase)
|
private bool MatchesLessThanOrEqual(DictionaryBase dictionaryBase)
|
||||||
{
|
{
|
||||||
if (!dictionaryBase.ContainsKey(this.Key))
|
if (!dictionaryBase.ContainsKey(this.Key[1]))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
long? checkLongValue = dictionaryBase.ReadLong(this.Key);
|
long? checkLongValue = dictionaryBase.ReadLong(this.Key[1]);
|
||||||
if (checkLongValue != null)
|
if (checkLongValue != null)
|
||||||
{
|
{
|
||||||
if (!long.TryParse(this.Value, out long matchValue))
|
if (!long.TryParse(this.Value, out long matchValue))
|
||||||
@@ -242,7 +257,7 @@ namespace SabreTools.Filter
|
|||||||
return checkLongValue <= matchValue;
|
return checkLongValue <= matchValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
double? checkDoubleValue = dictionaryBase.ReadDouble(this.Key);
|
double? checkDoubleValue = dictionaryBase.ReadDouble(this.Key[1]);
|
||||||
if (checkDoubleValue != null)
|
if (checkDoubleValue != null)
|
||||||
{
|
{
|
||||||
if (!double.TryParse(this.Value, out double matchValue))
|
if (!double.TryParse(this.Value, out double matchValue))
|
||||||
@@ -253,7 +268,7 @@ namespace SabreTools.Filter
|
|||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user