Cleanup of ModelBackedItem common functionality

This commit is contained in:
Matt Nadareski
2025-01-07 16:40:35 -05:00
parent b576f79c98
commit 6eaa7d0422
11 changed files with 921 additions and 962 deletions

View File

@@ -1,73 +0,0 @@
using System;
using SabreTools.Core.Tools;
using SabreTools.Models.Metadata;
namespace SabreTools.Core.Filter
{
public static class FieldManipulator
{
/// <summary>
/// Remove a field from a given DictionaryBase
/// </summary>
public static bool RemoveField(DictionaryBase? dictionaryBase, string? fieldName)
{
// If the item or field name are missing, we can't do anything
if (dictionaryBase == null || string.IsNullOrEmpty(fieldName))
return false;
// If the key doesn't exist, then it's already removed
if (!dictionaryBase.ContainsKey(fieldName!))
return true;
// Remove the key
dictionaryBase.Remove(fieldName!);
return true;
}
/// <summary>
/// Replace a field from one DictionaryBase to another
/// </summary>
public static bool ReplaceField(DictionaryBase? from, DictionaryBase? to, string? fieldName)
{
// If the items or field name are missing, we can't do anything
if (from == null || to == null || string.IsNullOrEmpty(fieldName))
return false;
// If the types of the items are not the same, we can't do anything
if (from.GetType() != to.GetType())
return false;
// If the key doesn't exist in the source, we can't do anything
if (!from.ContainsKey(fieldName!))
return false;
// Set the key
to[fieldName!] = from[fieldName!];
return true;
}
/// <summary>
/// Set a field in a given DictionaryBase
/// </summary>
public static bool SetField(DictionaryBase? dictionaryBase, string? fieldName, object value)
{
// If the item or field name are missing, we can't do anything
if (dictionaryBase == null || string.IsNullOrEmpty(fieldName))
return false;
// Retrieve the list of valid fields for the item
var constants = TypeHelper.GetConstants(dictionaryBase.GetType());
if (constants == null)
return false;
// Get the value that matches the field name provided
string? realField = Array.Find(constants, c => string.Equals(c, fieldName, StringComparison.OrdinalIgnoreCase));
if (realField == null)
return false;
// Set the field with the new value
dictionaryBase[realField] = value;
return true;
}
}
}

View File

@@ -149,5 +149,73 @@ namespace SabreTools.Core
}
#endregion
#region Manipulation
/// <summary>
/// Remove a field from the backing item
/// </summary>
public bool RemoveField(string? fieldName)
{
// If the item or field name are missing, we can't do anything
if (_internal == null || string.IsNullOrEmpty(fieldName))
return false;
// If the key doesn't exist, then it's already removed
if (!_internal.ContainsKey(fieldName!))
return true;
// Remove the key
_internal.Remove(fieldName!);
return true;
}
/// <summary>
/// Replace a field from another ModelBackedItem
/// </summary>
public bool ReplaceField(ModelBackedItem<T>? from, string? fieldName)
{
// If the items or field name are missing, we can't do anything
if (from?._internal == null || _internal == null || string.IsNullOrEmpty(fieldName))
return false;
// If the types of the items are not the same, we can't do anything
if (from._internal.GetType() != _internal.GetType())
return false;
// If the key doesn't exist in the source, we can't do anything
if (!from._internal.ContainsKey(fieldName!))
return false;
// Set the key
_internal[fieldName!] = from._internal[fieldName!];
return true;
}
/// <summary>
/// Set a field from the backing item
/// </summary>
public bool SetField(string? fieldName, object value)
{
// If the item or field name are missing, we can't do anything
if (_internal == null || string.IsNullOrEmpty(fieldName))
return false;
// Retrieve the list of valid fields for the item
var constants = TypeHelper.GetConstants(_internal.GetType());
if (constants == null)
return false;
// Get the value that matches the field name provided
string? realField = Array.Find(constants, c => string.Equals(c, fieldName, StringComparison.OrdinalIgnoreCase));
if (realField == null)
return false;
// Set the field with the new value
_internal[realField] = value;
return true;
}
#endregion
}
}

View File

@@ -51,7 +51,7 @@ namespace SabreTools.Core.Tools
/// </summary>
/// <typeparam name="T">Enum type that is expected</typeparam>
/// <returns>Dictionary of string to enum values</returns>
internal static Dictionary<string, T> GenerateToEnum<T>()
public static Dictionary<string, T> GenerateToEnum<T>()
{
try
{