Files
SabreTools/SabreTools.Core/Tools/Converters.cs

133 lines
4.3 KiB
C#
Raw Normal View History

2023-04-19 17:35:20 -04:00
using System;
using System.Collections.Generic;
2020-08-28 22:21:35 -07:00
namespace SabreTools.Core.Tools
{
public static class Converters
{
2020-08-24 13:21:59 -07:00
#region String to Enum
/// <summary>
/// Get bool? value from input string
/// </summary>
/// <param name="yesno">String to get value from</param>
/// <returns>bool? corresponding to the string</returns>
public static bool? AsYesNo(this string? yesno)
{
return yesno?.ToLowerInvariant() switch
{
2023-04-20 10:34:37 -04:00
"yes" or "true" => true,
"no" or "false" => false,
2020-08-23 22:54:09 -07:00
_ => null,
};
}
2023-04-20 10:34:37 -04:00
2023-04-19 17:35:20 -04:00
/// <summary>
/// Get a set of mappings from strings to enum values
/// </summary>
2023-04-20 10:34:37 -04:00
/// <typeparam name="T">Enum type that is expected</typeparam>
2023-04-19 17:35:20 -04:00
/// <returns>Dictionary of string to enum values</returns>
2025-05-11 23:36:48 -04:00
public static Dictionary<string, T> GenerateToEnum<T>()
2023-04-19 17:35:20 -04:00
{
try
{
// Get all of the values for the enum type
2023-04-20 10:34:37 -04:00
var values = Enum.GetValues(typeof(T));
2023-04-19 17:35:20 -04:00
// Build the output dictionary
2024-02-28 19:19:50 -05:00
Dictionary<string, T> mappings = [];
2024-02-28 19:49:09 -05:00
foreach (T? value in values)
2023-04-19 17:35:20 -04:00
{
2024-02-28 19:49:09 -05:00
// If the value is null
if (value == null)
continue;
2023-04-19 17:35:20 -04:00
// Try to get the mapping attribute
MappingAttribute? attr = AttributeHelper<T>.GetAttribute(value);
2024-10-19 12:07:43 -04:00
if (attr?.Mappings == null || attr.Mappings.Length == 0)
2023-04-19 17:35:20 -04:00
continue;
// Loop through the mappings and add each
foreach (string mapString in attr.Mappings)
{
2023-04-20 12:12:56 -04:00
if (mapString != null)
mappings[mapString] = value;
2023-04-19 17:35:20 -04:00
}
}
// Return the output dictionary
return mappings;
}
catch
{
// This should not happen, only if the type was not an enum
2024-02-28 19:19:50 -05:00
return [];
2023-04-19 17:35:20 -04:00
}
}
2020-08-23 22:54:09 -07:00
2020-08-24 13:21:59 -07:00
#endregion
#region Enum to String
2020-08-23 22:54:09 -07:00
/// <summary>
/// Get string value from input bool?
/// </summary>
/// <param name="yesno">bool? to get value from</param>
2020-08-24 13:21:59 -07:00
/// <returns>String corresponding to the bool?</returns>
public static string? FromYesNo(this bool? yesno)
2020-08-23 22:54:09 -07:00
{
return yesno switch
{
true => "yes",
false => "no",
_ => null,
};
}
2020-08-24 13:21:59 -07:00
2023-04-19 17:35:20 -04:00
/// <summary>
/// Get a set of mappings from enum values to string
/// </summary>
2023-04-20 12:12:56 -04:00
/// <param name="useSecond">True to use the second mapping option, if it exists</param>
/// <typeparam name="T">Enum type that is expected</typeparam>
2023-04-19 17:35:20 -04:00
/// <returns>Dictionary of enum to string values</returns>
2025-05-11 23:36:48 -04:00
public static Dictionary<T, string> GenerateToString<T>(bool useSecond) where T : notnull
2023-04-19 17:35:20 -04:00
{
try
{
// Get all of the values for the enum type
2023-04-20 10:34:37 -04:00
var values = Enum.GetValues(typeof(T));
2023-04-19 17:35:20 -04:00
// Build the output dictionary
2024-02-28 19:19:50 -05:00
Dictionary<T, string> mappings = [];
2024-02-28 19:49:09 -05:00
foreach (T? value in values)
2023-04-19 17:35:20 -04:00
{
2024-02-28 19:49:09 -05:00
// If the value is null
if (value == null)
continue;
2023-04-19 17:35:20 -04:00
// Try to get the mapping attribute
MappingAttribute? attr = AttributeHelper<T>.GetAttribute(value);
2024-10-19 12:07:43 -04:00
if (attr?.Mappings == null || attr.Mappings.Length == 0)
2023-04-19 17:35:20 -04:00
continue;
2023-04-20 12:12:56 -04:00
// Use either the first or second item in the list
if (attr.Mappings.Length > 1 && useSecond)
mappings[value] = attr.Mappings[1];
else
mappings[value] = attr.Mappings[0];
2023-04-19 17:35:20 -04:00
}
// Return the output dictionary
return mappings;
}
catch
{
// This should not happen, only if the type was not an enum
2024-02-28 19:19:50 -05:00
return [];
2023-04-19 17:35:20 -04:00
}
}
2020-08-24 13:21:59 -07:00
#endregion
}
}