Create and use FilterKey

This commit is contained in:
Matt Nadareski
2024-10-24 03:16:45 -04:00
parent 349d3d1a78
commit 14fc7777cb
9 changed files with 85 additions and 72 deletions

View File

@@ -0,0 +1,30 @@
namespace SabreTools.Core.Filter
{
/// <summary>
/// Represents a single filter key
/// </summary>
public class FilterKey
{
/// <summary>
/// Item name associated with the filter
/// </summary>
public readonly string ItemName;
/// <summary>
/// Field name associated with the filter
/// </summary>
public readonly string FieldName;
/// <summary>
/// Discrete value constructor
/// </summary>
public FilterKey(string itemName, string fieldName)
{
ItemName = itemName;
FieldName = fieldName;
}
/// <inheritdoc/>
public override string ToString() => $"{ItemName}.{FieldName}";
}
}

View File

@@ -12,14 +12,9 @@ namespace SabreTools.Core.Filter
public class FilterObject
{
/// <summary>
/// Item name associated with the field
/// Item key associated with the filter
/// </summary>
public readonly string ItemName;
/// <summary>
/// Field name associated with the filter
/// </summary>
public readonly string FieldName;
public readonly FilterKey Key;
/// <summary>
/// Value to match in the filter
@@ -39,8 +34,7 @@ namespace SabreTools.Core.Filter
if (!FilterParser.ParseFilterId(keyItem, out string itemName, out string fieldName))
throw new ArgumentOutOfRangeException(nameof(filterString));
ItemName = itemName;
FieldName = fieldName;
Key = new FilterKey(itemName, fieldName);
Value = value;
Operation = operation;
}
@@ -50,8 +44,7 @@ namespace SabreTools.Core.Filter
if (!FilterParser.ParseFilterId(itemField, out string itemName, out string fieldName))
throw new ArgumentOutOfRangeException(nameof(value));
ItemName = itemName;
FieldName = fieldName;
Key = new FilterKey(itemName, fieldName);
Value = value;
Operation = GetOperation(operation);
}
@@ -61,8 +54,7 @@ namespace SabreTools.Core.Filter
if (!FilterParser.ParseFilterId(itemField, out string itemName, out string fieldName))
throw new ArgumentOutOfRangeException(nameof(value));
ItemName = itemName;
FieldName = fieldName;
Key = new FilterKey(itemName, fieldName);
Value = value;
Operation = operation;
}
@@ -93,11 +85,11 @@ namespace SabreTools.Core.Filter
private bool MatchesEqual(DictionaryBase dictionaryBase)
{
// If the key doesn't exist, we count it as null
if (!dictionaryBase.ContainsKey(FieldName))
if (!dictionaryBase.ContainsKey(Key.FieldName))
return Value == null;
// If the value in the dictionary is null
string? checkValue = dictionaryBase.ReadString(FieldName);
string? checkValue = dictionaryBase.ReadString(Key.FieldName);
if (checkValue == null)
return Value == null;
@@ -136,11 +128,11 @@ namespace SabreTools.Core.Filter
private bool MatchesNotEqual(DictionaryBase dictionaryBase)
{
// If the key doesn't exist, we count it as null
if (!dictionaryBase.ContainsKey(FieldName))
if (!dictionaryBase.ContainsKey(Key.FieldName))
return Value != null;
// If the value in the dictionary is null
string? checkValue = dictionaryBase.ReadString(FieldName);
string? checkValue = dictionaryBase.ReadString(Key.FieldName);
if (checkValue == null)
return Value == null;
@@ -179,11 +171,11 @@ namespace SabreTools.Core.Filter
private bool MatchesGreaterThan(DictionaryBase dictionaryBase)
{
// If the key doesn't exist, we count it as null
if (!dictionaryBase.ContainsKey(FieldName))
if (!dictionaryBase.ContainsKey(Key.FieldName))
return false;
// If the value in the dictionary is null
string? checkValue = dictionaryBase.ReadString(FieldName);
string? checkValue = dictionaryBase.ReadString(Key.FieldName);
if (checkValue == null)
return false;
@@ -212,11 +204,11 @@ namespace SabreTools.Core.Filter
private bool MatchesGreaterThanOrEqual(DictionaryBase dictionaryBase)
{
// If the key doesn't exist, we count it as null
if (!dictionaryBase.ContainsKey(FieldName))
if (!dictionaryBase.ContainsKey(Key.FieldName))
return false;
// If the value in the dictionary is null
string? checkValue = dictionaryBase.ReadString(FieldName);
string? checkValue = dictionaryBase.ReadString(Key.FieldName);
if (checkValue == null)
return false;
@@ -245,11 +237,11 @@ namespace SabreTools.Core.Filter
private bool MatchesLessThan(DictionaryBase dictionaryBase)
{
// If the key doesn't exist, we count it as null
if (!dictionaryBase.ContainsKey(FieldName))
if (!dictionaryBase.ContainsKey(Key.FieldName))
return false;
// If the value in the dictionary is null
string? checkValue = dictionaryBase.ReadString(FieldName);
string? checkValue = dictionaryBase.ReadString(Key.FieldName);
if (checkValue == null)
return false;
@@ -278,11 +270,11 @@ namespace SabreTools.Core.Filter
private bool MatchesLessThanOrEqual(DictionaryBase dictionaryBase)
{
// If the key doesn't exist, we count it as null
if (!dictionaryBase.ContainsKey(FieldName))
if (!dictionaryBase.ContainsKey(Key.FieldName))
return false;
// If the value in the dictionary is null
string? checkValue = dictionaryBase.ReadString(FieldName);
string? checkValue = dictionaryBase.ReadString(Key.FieldName);
if (checkValue == null)
return false;

View File

@@ -54,9 +54,9 @@ namespace SabreTools.Core.Filter
foreach (var filter in Filters)
{
// If the filter isn't for this object type, skip
if (filter.ItemName != itemName)
if (filter.Key.ItemName != itemName)
continue;
else if (filter.ItemName == "item" && Array.IndexOf(TypeHelper.GetDatItemTypeNames(), itemName) > -1)
else if (filter.Key.ItemName == "item" && Array.IndexOf(TypeHelper.GetDatItemTypeNames(), itemName) > -1)
continue;
// If we don't get a match, it's a failure