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 public class FilterObject
{ {
/// <summary> /// <summary>
/// Item name associated with the field /// Item key associated with the filter
/// </summary> /// </summary>
public readonly string ItemName; public readonly FilterKey Key;
/// <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
@@ -39,8 +34,7 @@ namespace SabreTools.Core.Filter
if (!FilterParser.ParseFilterId(keyItem, out string itemName, out string fieldName)) if (!FilterParser.ParseFilterId(keyItem, out string itemName, out string fieldName))
throw new ArgumentOutOfRangeException(nameof(filterString)); throw new ArgumentOutOfRangeException(nameof(filterString));
ItemName = itemName; Key = new FilterKey(itemName, fieldName);
FieldName = fieldName;
Value = value; Value = value;
Operation = operation; Operation = operation;
} }
@@ -50,8 +44,7 @@ namespace SabreTools.Core.Filter
if (!FilterParser.ParseFilterId(itemField, out string itemName, out string fieldName)) if (!FilterParser.ParseFilterId(itemField, out string itemName, out string fieldName))
throw new ArgumentOutOfRangeException(nameof(value)); throw new ArgumentOutOfRangeException(nameof(value));
ItemName = itemName; Key = new FilterKey(itemName, fieldName);
FieldName = fieldName;
Value = value; Value = value;
Operation = GetOperation(operation); Operation = GetOperation(operation);
} }
@@ -61,8 +54,7 @@ namespace SabreTools.Core.Filter
if (!FilterParser.ParseFilterId(itemField, out string itemName, out string fieldName)) if (!FilterParser.ParseFilterId(itemField, out string itemName, out string fieldName))
throw new ArgumentOutOfRangeException(nameof(value)); throw new ArgumentOutOfRangeException(nameof(value));
ItemName = itemName; Key = new FilterKey(itemName, fieldName);
FieldName = fieldName;
Value = value; Value = value;
Operation = operation; Operation = operation;
} }
@@ -93,11 +85,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(FieldName)) if (!dictionaryBase.ContainsKey(Key.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(FieldName); string? checkValue = dictionaryBase.ReadString(Key.FieldName);
if (checkValue == null) if (checkValue == null)
return Value == null; return Value == null;
@@ -136,11 +128,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(FieldName)) if (!dictionaryBase.ContainsKey(Key.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(FieldName); string? checkValue = dictionaryBase.ReadString(Key.FieldName);
if (checkValue == null) if (checkValue == null)
return Value == null; return Value == null;
@@ -179,11 +171,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(FieldName)) if (!dictionaryBase.ContainsKey(Key.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(FieldName); string? checkValue = dictionaryBase.ReadString(Key.FieldName);
if (checkValue == null) if (checkValue == null)
return false; return false;
@@ -212,11 +204,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(FieldName)) if (!dictionaryBase.ContainsKey(Key.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(FieldName); string? checkValue = dictionaryBase.ReadString(Key.FieldName);
if (checkValue == null) if (checkValue == null)
return false; return false;
@@ -245,11 +237,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(FieldName)) if (!dictionaryBase.ContainsKey(Key.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(FieldName); string? checkValue = dictionaryBase.ReadString(Key.FieldName);
if (checkValue == null) if (checkValue == null)
return false; return false;
@@ -278,11 +270,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(FieldName)) if (!dictionaryBase.ContainsKey(Key.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(FieldName); string? checkValue = dictionaryBase.ReadString(Key.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.ItemName != itemName) if (filter.Key.ItemName != itemName)
continue; 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; 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

@@ -29,7 +29,7 @@ namespace SabreTools.DatFiles
/// <summary> /// <summary>
/// Mappings to set DatItem fields /// Mappings to set DatItem fields
/// </summary> /// </summary>
public Dictionary<(string, string), string> ItemFieldMappings { get; } = []; public Dictionary<FilterKey, string> ItemFieldMappings { get; } = [];
#endregion #endregion
@@ -49,7 +49,7 @@ namespace SabreTools.DatFiles
/// </summary> /// </summary>
/// <param name="field">Field name</param> /// <param name="field">Field name</param>
/// <param name="value">Field value</param> /// <param name="value">Field value</param>
public void PopulateSetters(string field, string value) public void PopulateSetters(FilterKey field, string value)
=> PopulateSettersFromList([field], [value]); => PopulateSettersFromList([field], [value]);
/// <summary> /// <summary>
@@ -57,7 +57,7 @@ namespace SabreTools.DatFiles
/// </summary> /// </summary>
/// <param name="fields">List of field names</param> /// <param name="fields">List of field names</param>
/// <param name="values">List of field values</param> /// <param name="values">List of field values</param>
public void PopulateSettersFromList(List<string> fields, List<string> values) public void PopulateSettersFromList(List<FilterKey> fields, List<string> values)
{ {
// If the list is null or empty, just return // If the list is null or empty, just return
if (values == null || values.Count == 0) if (values == null || values.Count == 0)
@@ -68,7 +68,7 @@ namespace SabreTools.DatFiles
// Now we loop through and get values for everything // Now we loop through and get values for everything
for (int i = 0; i < fields.Count; i++) for (int i = 0; i < fields.Count; i++)
{ {
string field = fields[i]; FilterKey field = fields[i];
string value = values[i]; string value = values[i];
if (!SetSetter(field, value)) if (!SetSetter(field, value))
@@ -82,7 +82,7 @@ namespace SabreTools.DatFiles
/// Populate the setters using a set of field names /// Populate the setters using a set of field names
/// </summary> /// </summary>
/// <param name="mappings">Dictionary of mappings</param> /// <param name="mappings">Dictionary of mappings</param>
public void PopulateSettersFromDictionary(Dictionary<string, string>? mappings) public void PopulateSettersFromDictionary(Dictionary<FilterKey, string>? mappings)
{ {
// If the dictionary is null or empty, just return // If the dictionary is null or empty, just return
if (mappings == null || mappings.Count == 0) if (mappings == null || mappings.Count == 0)
@@ -93,7 +93,7 @@ namespace SabreTools.DatFiles
// Now we loop through and get values for everything // Now we loop through and get values for everything
foreach (var mapping in mappings) foreach (var mapping in mappings)
{ {
string field = mapping.Key; FilterKey field = mapping.Key;
string value = mapping.Value; string value = mapping.Value;
if (!SetSetter(field, value)) if (!SetSetter(field, value))
@@ -106,29 +106,32 @@ namespace SabreTools.DatFiles
/// <summary> /// <summary>
/// Set remover from a value /// Set remover from a value
/// </summary> /// </summary>
/// <param name="field">Key for the remover to be set</param> /// <param name="key">Key for the remover to be set</param>
private bool SetSetter(string field, string value) private bool SetSetter(FilterKey key, string value)
{ {
// If the key is null or empty, return false // Split the key values for validation
if (string.IsNullOrEmpty(field)) string itemName = key.ItemName;
return false; string fieldName = key.FieldName;
// Get the parser pair out of it, if possible // Get the parser pair out of it, if possible
if (!FilterParser.ParseFilterId(field, out string type, out string key)) if (!FilterParser.ParseFilterId(ref itemName, ref fieldName))
return false; return false;
switch (type) // Set the values back on the key
key = new FilterKey(itemName, fieldName);
switch (itemName)
{ {
case Models.Metadata.MetadataFile.HeaderKey: case Models.Metadata.MetadataFile.HeaderKey:
HeaderFieldMappings[key] = value; HeaderFieldMappings[fieldName] = value;
return true; return true;
case Models.Metadata.MetadataFile.MachineKey: case Models.Metadata.MetadataFile.MachineKey:
MachineFieldMappings[key] = value; MachineFieldMappings[fieldName] = value;
return true; return true;
default: default:
ItemFieldMappings[(type, key)] = value; ItemFieldMappings[key] = value;
return true; return true;
} }
} }
@@ -189,16 +192,16 @@ namespace SabreTools.DatFiles
// If there are no field names for this type or generic, return // If there are no field names for this type or generic, return
string? itemType = datItem.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>().AsStringValue(); string? itemType = datItem.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>().AsStringValue();
if (itemType == null || (!ItemFieldMappings.Keys.Any(kvp => kvp.Item1 == itemType) && !ItemFieldMappings.Keys.Any(kvp => kvp.Item1 == "item"))) if (itemType == null || (!ItemFieldMappings.Keys.Any(kvp => kvp.ItemName == itemType) && !ItemFieldMappings.Keys.Any(kvp => kvp.ItemName == "item")))
return; return;
// Get the combined list of fields to remove // Get the combined list of fields to remove
var fieldMappings = new Dictionary<string, string>(); var fieldMappings = new Dictionary<string, string>();
foreach (var mapping in ItemFieldMappings.Where(kvp => kvp.Key.Item1 == "item").ToDictionary(kvp => kvp.Key.Item2, kvp => kvp.Value)) foreach (var mapping in ItemFieldMappings.Where(kvp => kvp.Key.ItemName == "item").ToDictionary(kvp => kvp.Key.FieldName, kvp => kvp.Value))
{ {
fieldMappings[mapping.Key] = mapping.Value; fieldMappings[mapping.Key] = mapping.Value;
} }
foreach (var mapping in ItemFieldMappings.Where(kvp => kvp.Key.Item1 == itemType).ToDictionary(kvp => kvp.Key.Item2, kvp => kvp.Value)) foreach (var mapping in ItemFieldMappings.Where(kvp => kvp.Key.ItemName == itemType).ToDictionary(kvp => kvp.Key.FieldName, kvp => kvp.Value))
{ {
fieldMappings[mapping.Key] = mapping.Value; fieldMappings[mapping.Key] = mapping.Value;
} }

View File

@@ -208,9 +208,9 @@ namespace SabreTools.Filtering
/// Combine ExtraIni fields /// Combine ExtraIni fields
/// </summary> /// </summary>
/// <returns>Mapping dictionary from machine name to field mapping</returns> /// <returns>Mapping dictionary from machine name to field mapping</returns>
private Dictionary<string, Dictionary<string, string>> CombineExtras() private Dictionary<string, Dictionary<FilterKey, string>> CombineExtras()
{ {
var machineMap = new Dictionary<string, Dictionary<string, string>>(); var machineMap = new Dictionary<string, Dictionary<FilterKey, string>>();
// Loop through each of the extras // Loop through each of the extras
foreach (ExtraIniItem item in Items) foreach (ExtraIniItem item in Items)

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using SabreTools.Core.Filter;
using SabreTools.IO.Logging; using SabreTools.IO.Logging;
using SabreTools.IO.Readers; using SabreTools.IO.Readers;
@@ -13,17 +14,7 @@ namespace SabreTools.Filtering
/// Item type and field to update with INI information /// Item type and field to update with INI information
/// </summary> /// </summary>
/// <remarks>Formatted like "ItemName.FieldName"</remarks> /// <remarks>Formatted like "ItemName.FieldName"</remarks>
public string Key => $"{_itemName}.{_fieldName}"; public readonly FilterKey Key;
/// <summary>
/// Item type to update with INI information
/// </summary>
private readonly string _itemName;
/// <summary>
/// Field to update with INI information
/// </summary>
private readonly string _fieldName;
/// <summary> /// <summary>
/// Mappings from machine names to value /// Mappings from machine names to value
@@ -36,8 +27,7 @@ namespace SabreTools.Filtering
public ExtraIniItem(string itemName, string fieldName) public ExtraIniItem(string itemName, string fieldName)
{ {
_itemName = itemName; Key = new FilterKey(itemName, fieldName);
_fieldName = fieldName;
} }
#endregion #endregion

View File

@@ -1,3 +1,4 @@
using SabreTools.Core.Filter;
using SabreTools.DatFiles; using SabreTools.DatFiles;
using SabreTools.DatItems; using SabreTools.DatItems;
using SabreTools.DatItems.Formats; using SabreTools.DatItems.Formats;
@@ -12,7 +13,7 @@ namespace SabreTools.Test.DatFiles
{ {
var datItem = CreateDatItem(); var datItem = CreateDatItem();
var setter = new Setter(); var setter = new Setter();
setter.PopulateSetters("datitem.name", "bar"); setter.PopulateSetters(new FilterKey("datitem", "name"), "bar");
setter.SetFields(datItem); setter.SetFields(datItem);
Assert.Equal("bar", datItem.GetName()); Assert.Equal("bar", datItem.GetName());
} }
@@ -22,7 +23,7 @@ namespace SabreTools.Test.DatFiles
{ {
var datItem = CreateDatItem(); var datItem = CreateDatItem();
var setter = new Setter(); var setter = new Setter();
setter.PopulateSetters("machine.name", "foo"); setter.PopulateSetters(new FilterKey("machine", "name"), "foo");
setter.SetFields(datItem.GetFieldValue<Machine>(DatItem.MachineKey)); setter.SetFields(datItem.GetFieldValue<Machine>(DatItem.MachineKey));
Assert.Equal("foo", datItem.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)); Assert.Equal("foo", datItem.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey));
} }

View File

@@ -43,13 +43,11 @@ namespace SabreTools.Test.Filter
var filter = new FilterRunner(filters); var filter = new FilterRunner(filters);
// Check the filters // Check the filters
Assert.Equal("machine", filter.Filters[0].ItemName); Assert.Equal("machine.name", filter.Filters[0].Key.ToString());
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("machine", filter.Filters[1].ItemName); Assert.Equal("machine.name", filter.Filters[1].Key.ToString());
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);
} }
@@ -68,13 +66,11 @@ namespace SabreTools.Test.Filter
var filter = new FilterRunner(filters); var filter = new FilterRunner(filters);
// Check the filters // Check the filters
Assert.Equal("rom", filter.Filters[0].ItemName); Assert.Equal("rom.name", filter.Filters[0].Key.ToString());
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("item", filter.Filters[1].ItemName); Assert.Equal("item.name", filter.Filters[1].Key.ToString());
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

@@ -825,7 +825,8 @@ Reset the internal state: reset();";
string value = Arguments[1]; string value = Arguments[1];
var setter = new Setter(); var setter = new Setter();
setter.PopulateSetters(field, value); FilterParser.ParseFilterId(Arguments[0], out string itemName, out string fieldName);
setter.PopulateSetters(new FilterKey(itemName, fieldName), value);
// Set the header field // Set the header field
setter.SetFields(batchState.DatFile.Header); setter.SetFields(batchState.DatFile.Header);