Split filter object key for clarity

This commit is contained in:
Matt Nadareski
2024-10-24 02:32:21 -04:00
parent 58f0df6abf
commit 038c399114
5 changed files with 44 additions and 30 deletions

View File

@@ -12,9 +12,14 @@ namespace SabreTools.Core.Filter
public class FilterObject public class FilterObject
{ {
/// <summary> /// <summary>
/// Key name for the filter /// Item name associated with the field
/// </summary> /// </summary>
public readonly string[] Key; public readonly string ItemName;
/// <summary>
/// Field name associated with the filter
/// </summary>
public readonly string FieldName;
/// <summary> /// <summary>
/// Value to match in the filter /// Value to match in the filter
@@ -31,30 +36,33 @@ namespace SabreTools.Core.Filter
if (!SplitFilterString(filterString, out var keyItem, out Operation operation, out var value)) if (!SplitFilterString(filterString, out var keyItem, out Operation operation, out var value))
throw new ArgumentOutOfRangeException(nameof(filterString)); throw new ArgumentOutOfRangeException(nameof(filterString));
if (!FilterParser.ParseFilterId(keyItem, out var itemName, out var fieldName)) if (!FilterParser.ParseFilterId(keyItem, out string itemName, out string fieldName))
throw new ArgumentOutOfRangeException(nameof(filterString)); throw new ArgumentOutOfRangeException(nameof(filterString));
Key = [itemName, fieldName]; ItemName = itemName;
FieldName = fieldName;
Value = value; Value = value;
Operation = operation; Operation = operation;
} }
public FilterObject(string itemField, string? value, string? operation) public FilterObject(string itemField, string? value, string? operation)
{ {
if (!FilterParser.ParseFilterId(itemField, out var itemName, out var fieldName)) if (!FilterParser.ParseFilterId(itemField, out string itemName, out string fieldName))
throw new ArgumentOutOfRangeException(nameof(value)); throw new ArgumentOutOfRangeException(nameof(value));
Key = [itemName, fieldName]; ItemName = itemName;
FieldName = fieldName;
Value = value; Value = value;
Operation = GetOperation(operation); Operation = GetOperation(operation);
} }
public FilterObject(string itemField, string? value, Operation operation) public FilterObject(string itemField, string? value, Operation operation)
{ {
if (!FilterParser.ParseFilterId(itemField, out var itemName, out var fieldName)) if (!FilterParser.ParseFilterId(itemField, out string itemName, out string fieldName))
throw new ArgumentOutOfRangeException(nameof(value)); throw new ArgumentOutOfRangeException(nameof(value));
Key = [itemName!, fieldName!]; ItemName = itemName;
FieldName = fieldName;
Value = value; Value = value;
Operation = operation; Operation = operation;
} }
@@ -66,7 +74,7 @@ namespace SabreTools.Core.Filter
/// </summary> /// </summary>
public bool Matches(DictionaryBase dictionaryBase) public bool Matches(DictionaryBase dictionaryBase)
{ {
// TODO: Add validation of dictionary base type from Key[0] // TODO: Add validation of dictionary base type from the key values
return Operation switch return Operation switch
{ {
Operation.Equals => MatchesEqual(dictionaryBase), Operation.Equals => MatchesEqual(dictionaryBase),
@@ -85,11 +93,11 @@ namespace SabreTools.Core.Filter
private bool MatchesEqual(DictionaryBase dictionaryBase) private bool MatchesEqual(DictionaryBase dictionaryBase)
{ {
// If the key doesn't exist, we count it as null // If the key doesn't exist, we count it as null
if (!dictionaryBase.ContainsKey(Key[1])) if (!dictionaryBase.ContainsKey(FieldName))
return Value == null; return Value == null;
// If the value in the dictionary is null // If the value in the dictionary is null
string? checkValue = dictionaryBase.ReadString(Key[1]); string? checkValue = dictionaryBase.ReadString(FieldName);
if (checkValue == null) if (checkValue == null)
return Value == null; return Value == null;
@@ -128,11 +136,11 @@ namespace SabreTools.Core.Filter
private bool MatchesNotEqual(DictionaryBase dictionaryBase) private bool MatchesNotEqual(DictionaryBase dictionaryBase)
{ {
// If the key doesn't exist, we count it as null // If the key doesn't exist, we count it as null
if (!dictionaryBase.ContainsKey(Key[1])) if (!dictionaryBase.ContainsKey(FieldName))
return Value != null; return Value != null;
// If the value in the dictionary is null // If the value in the dictionary is null
string? checkValue = dictionaryBase.ReadString(Key[1]); string? checkValue = dictionaryBase.ReadString(FieldName);
if (checkValue == null) if (checkValue == null)
return Value == null; return Value == null;
@@ -171,11 +179,11 @@ namespace SabreTools.Core.Filter
private bool MatchesGreaterThan(DictionaryBase dictionaryBase) private bool MatchesGreaterThan(DictionaryBase dictionaryBase)
{ {
// If the key doesn't exist, we count it as null // If the key doesn't exist, we count it as null
if (!dictionaryBase.ContainsKey(Key[1])) if (!dictionaryBase.ContainsKey(FieldName))
return false; return false;
// If the value in the dictionary is null // If the value in the dictionary is null
string? checkValue = dictionaryBase.ReadString(Key[1]); string? checkValue = dictionaryBase.ReadString(FieldName);
if (checkValue == null) if (checkValue == null)
return false; return false;
@@ -204,11 +212,11 @@ namespace SabreTools.Core.Filter
private bool MatchesGreaterThanOrEqual(DictionaryBase dictionaryBase) private bool MatchesGreaterThanOrEqual(DictionaryBase dictionaryBase)
{ {
// If the key doesn't exist, we count it as null // If the key doesn't exist, we count it as null
if (!dictionaryBase.ContainsKey(Key[1])) if (!dictionaryBase.ContainsKey(FieldName))
return false; return false;
// If the value in the dictionary is null // If the value in the dictionary is null
string? checkValue = dictionaryBase.ReadString(Key[1]); string? checkValue = dictionaryBase.ReadString(FieldName);
if (checkValue == null) if (checkValue == null)
return false; return false;
@@ -237,11 +245,11 @@ namespace SabreTools.Core.Filter
private bool MatchesLessThan(DictionaryBase dictionaryBase) private bool MatchesLessThan(DictionaryBase dictionaryBase)
{ {
// If the key doesn't exist, we count it as null // If the key doesn't exist, we count it as null
if (!dictionaryBase.ContainsKey(Key[1])) if (!dictionaryBase.ContainsKey(FieldName))
return false; return false;
// If the value in the dictionary is null // If the value in the dictionary is null
string? checkValue = dictionaryBase.ReadString(Key[1]); string? checkValue = dictionaryBase.ReadString(FieldName);
if (checkValue == null) if (checkValue == null)
return false; return false;
@@ -270,11 +278,11 @@ namespace SabreTools.Core.Filter
private bool MatchesLessThanOrEqual(DictionaryBase dictionaryBase) private bool MatchesLessThanOrEqual(DictionaryBase dictionaryBase)
{ {
// If the key doesn't exist, we count it as null // If the key doesn't exist, we count it as null
if (!dictionaryBase.ContainsKey(Key[1])) if (!dictionaryBase.ContainsKey(FieldName))
return false; return false;
// If the value in the dictionary is null // If the value in the dictionary is null
string? checkValue = dictionaryBase.ReadString(Key[1]); string? checkValue = dictionaryBase.ReadString(FieldName);
if (checkValue == null) if (checkValue == null)
return false; return false;

View File

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

View File

@@ -145,10 +145,12 @@ namespace SabreTools.DatItems
FileTypes.Archives.TapeArchive => new Rom(baseFile), FileTypes.Archives.TapeArchive => new Rom(baseFile),
FileTypes.Archives.XZArchive => new Rom(baseFile), FileTypes.Archives.XZArchive => new Rom(baseFile),
FileTypes.Archives.ZipArchive => new Rom(baseFile), FileTypes.Archives.ZipArchive => new Rom(baseFile),
FileTypes.BaseArchive => new Rom(baseFile),
FileTypes.Folder => null, // Folders cannot be a DatItem
FileTypes.BaseFile => new Rom(baseFile),
// Miscellaneous // Miscellaneous
null => null, _ => null,
_ => new Rom(baseFile),
}; };
} }

View File

@@ -43,11 +43,13 @@ namespace SabreTools.Test.Filter
var filter = new FilterRunner(filters); var filter = new FilterRunner(filters);
// Check the filters // Check the filters
Assert.Equal(new string[] {"machine", "name"}, filter.Filters[0].Key); Assert.Equal("machine", filter.Filters[0].ItemName);
Assert.Equal("name", filter.Filters[0].FieldName);
Assert.Equal("foo", filter.Filters[0].Value); Assert.Equal("foo", filter.Filters[0].Value);
Assert.Equal(Operation.Equals, filter.Filters[0].Operation); Assert.Equal(Operation.Equals, filter.Filters[0].Operation);
Assert.Equal(new string[] {"machine", "name"}, filter.Filters[1].Key); Assert.Equal("machine", filter.Filters[1].ItemName);
Assert.Equal("name", filter.Filters[1].FieldName);
Assert.Equal("bar", filter.Filters[1].Value); Assert.Equal("bar", filter.Filters[1].Value);
Assert.Equal(Operation.NotEquals, filter.Filters[1].Operation); Assert.Equal(Operation.NotEquals, filter.Filters[1].Operation);
} }
@@ -66,11 +68,13 @@ namespace SabreTools.Test.Filter
var filter = new FilterRunner(filters); var filter = new FilterRunner(filters);
// Check the filters // Check the filters
Assert.Equal(new string[] { "rom", "name"}, filter.Filters[0].Key); Assert.Equal("rom", filter.Filters[0].ItemName);
Assert.Equal("name", filter.Filters[0].FieldName);
Assert.Equal("foo", filter.Filters[0].Value); Assert.Equal("foo", filter.Filters[0].Value);
Assert.Equal(Operation.Equals, filter.Filters[0].Operation); Assert.Equal(Operation.Equals, filter.Filters[0].Operation);
Assert.Equal(new string[] { "item", "name"}, filter.Filters[1].Key); Assert.Equal("item", filter.Filters[1].ItemName);
Assert.Equal("name", filter.Filters[1].FieldName);
Assert.Equal("bar", filter.Filters[1].Value); Assert.Equal("bar", filter.Filters[1].Value);
Assert.Equal(Operation.NotEquals, filter.Filters[1].Operation); Assert.Equal(Operation.NotEquals, filter.Filters[1].Operation);
} }

View File

@@ -67,7 +67,7 @@ namespace SabreTools.Test.Filtering
// Check the fields // Check the fields
Assert.Equal("name", datItem.GetName()); Assert.Equal("name", datItem.GetName());
Assert.Equal("!", datItem.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)); Assert.Equal("!", datItem.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey));
Assert.Equal("name-3", datItem.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.DescriptionKey)); Assert.Equal("!", datItem.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.DescriptionKey));
} }
[Theory] [Theory]