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

View File

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

View File

@@ -208,9 +208,9 @@ namespace SabreTools.Filtering
/// Combine ExtraIni fields
/// </summary>
/// <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
foreach (ExtraIniItem item in Items)

View File

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

View File

@@ -1,3 +1,4 @@
using SabreTools.Core.Filter;
using SabreTools.DatFiles;
using SabreTools.DatItems;
using SabreTools.DatItems.Formats;
@@ -12,7 +13,7 @@ namespace SabreTools.Test.DatFiles
{
var datItem = CreateDatItem();
var setter = new Setter();
setter.PopulateSetters("datitem.name", "bar");
setter.PopulateSetters(new FilterKey("datitem", "name"), "bar");
setter.SetFields(datItem);
Assert.Equal("bar", datItem.GetName());
}
@@ -22,7 +23,7 @@ namespace SabreTools.Test.DatFiles
{
var datItem = CreateDatItem();
var setter = new Setter();
setter.PopulateSetters("machine.name", "foo");
setter.PopulateSetters(new FilterKey("machine", "name"), "foo");
setter.SetFields(datItem.GetFieldValue<Machine>(DatItem.MachineKey));
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);
// Check the filters
Assert.Equal("machine", filter.Filters[0].ItemName);
Assert.Equal("name", filter.Filters[0].FieldName);
Assert.Equal("machine.name", filter.Filters[0].Key.ToString());
Assert.Equal("foo", filter.Filters[0].Value);
Assert.Equal(Operation.Equals, filter.Filters[0].Operation);
Assert.Equal("machine", filter.Filters[1].ItemName);
Assert.Equal("name", filter.Filters[1].FieldName);
Assert.Equal("machine.name", filter.Filters[1].Key.ToString());
Assert.Equal("bar", filter.Filters[1].Value);
Assert.Equal(Operation.NotEquals, filter.Filters[1].Operation);
}
@@ -68,13 +66,11 @@ namespace SabreTools.Test.Filter
var filter = new FilterRunner(filters);
// Check the filters
Assert.Equal("rom", filter.Filters[0].ItemName);
Assert.Equal("name", filter.Filters[0].FieldName);
Assert.Equal("rom.name", filter.Filters[0].Key.ToString());
Assert.Equal("foo", filter.Filters[0].Value);
Assert.Equal(Operation.Equals, filter.Filters[0].Operation);
Assert.Equal("item", filter.Filters[1].ItemName);
Assert.Equal("name", filter.Filters[1].FieldName);
Assert.Equal("item.name", filter.Filters[1].Key.ToString());
Assert.Equal("bar", filter.Filters[1].Value);
Assert.Equal(Operation.NotEquals, filter.Filters[1].Operation);
}

View File

@@ -825,7 +825,8 @@ Reset the internal state: reset();";
string value = Arguments[1];
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
setter.SetFields(batchState.DatFile.Header);