mirror of
https://github.com/SabreTools/SabreTools.Serialization.git
synced 2026-04-05 22:01:33 +00:00
Reduce extension confusion
This commit is contained in:
@@ -1851,6 +1851,19 @@ namespace SabreTools.Data.Extensions.Test
|
|||||||
Assert.Equal(expected, actual);
|
Assert.Equal(expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(null, null)]
|
||||||
|
[InlineData("INVALID", null)]
|
||||||
|
[InlineData("yes", true)]
|
||||||
|
[InlineData("True", true)]
|
||||||
|
[InlineData("no", false)]
|
||||||
|
[InlineData("False", false)]
|
||||||
|
public void AsYesNoTest(string? field, bool? expected)
|
||||||
|
{
|
||||||
|
bool? actual = field.AsYesNo();
|
||||||
|
Assert.Equal(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Enum to String
|
#region Enum to String
|
||||||
@@ -2137,6 +2150,16 @@ namespace SabreTools.Data.Extensions.Test
|
|||||||
Assert.Equal(expected, actual);
|
Assert.Equal(expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(null, null)]
|
||||||
|
[InlineData(true, "yes")]
|
||||||
|
[InlineData(false, "no")]
|
||||||
|
public void FromYesNo(bool? field, string? expected)
|
||||||
|
{
|
||||||
|
string? actual = field.FromYesNo();
|
||||||
|
Assert.Equal(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1214,6 +1214,21 @@ namespace SabreTools.Data.Extensions
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <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
|
||||||
|
{
|
||||||
|
"yes" or "true" => true,
|
||||||
|
"no" or "false" => false,
|
||||||
|
_ => null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Enum to String
|
#region Enum to String
|
||||||
@@ -1574,7 +1589,21 @@ namespace SabreTools.Data.Extensions
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
/// <summary>
|
||||||
|
/// Get string value from input bool?
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="yesno">bool? to get value from</param>
|
||||||
|
/// <returns>String corresponding to the bool?</returns>
|
||||||
|
public static string? FromYesNo(this bool? yesno)
|
||||||
|
{
|
||||||
|
return yesno switch
|
||||||
|
{
|
||||||
|
true => "yes",
|
||||||
|
false => "no",
|
||||||
|
_ => null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
using SabreTools.Data.Extensions;
|
||||||
using SabreTools.Metadata.DatItems.Formats;
|
using SabreTools.Metadata.DatItems.Formats;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using SabreTools.Data.Extensions;
|
||||||
|
|
||||||
namespace SabreTools.Metadata.DatItems
|
namespace SabreTools.Metadata.DatItems
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using SabreTools.Data.Extensions;
|
||||||
|
|
||||||
namespace SabreTools.Metadata.DatItems.Formats
|
namespace SabreTools.Metadata.DatItems.Formats
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using SabreTools.Data.Extensions;
|
||||||
|
|
||||||
namespace SabreTools.Metadata.DatItems.Formats
|
namespace SabreTools.Metadata.DatItems.Formats
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using SabreTools.Data.Extensions;
|
||||||
|
|
||||||
namespace SabreTools.Metadata.DatItems.Formats
|
namespace SabreTools.Metadata.DatItems.Formats
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using SabreTools.Data.Extensions;
|
||||||
|
|
||||||
namespace SabreTools.Metadata.DatItems.Formats
|
namespace SabreTools.Metadata.DatItems.Formats
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using SabreTools.Data.Extensions;
|
||||||
|
|
||||||
namespace SabreTools.Metadata.DatItems.Formats
|
namespace SabreTools.Metadata.DatItems.Formats
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using SabreTools.Data.Extensions;
|
||||||
|
|
||||||
namespace SabreTools.Metadata.DatItems.Formats
|
namespace SabreTools.Metadata.DatItems.Formats
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using SabreTools.Data.Extensions;
|
||||||
|
|
||||||
namespace SabreTools.Metadata.DatItems.Formats
|
namespace SabreTools.Metadata.DatItems.Formats
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using SabreTools.Data.Extensions;
|
||||||
|
|
||||||
namespace SabreTools.Metadata.DatItems.Formats
|
namespace SabreTools.Metadata.DatItems.Formats
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using SabreTools.Data.Extensions;
|
||||||
|
|
||||||
namespace SabreTools.Metadata.DatItems.Formats
|
namespace SabreTools.Metadata.DatItems.Formats
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using SabreTools.Data.Extensions;
|
||||||
|
|
||||||
namespace SabreTools.Metadata.DatItems.Formats
|
namespace SabreTools.Metadata.DatItems.Formats
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using SabreTools.Data.Extensions;
|
||||||
|
|
||||||
namespace SabreTools.Metadata.DatItems.Formats
|
namespace SabreTools.Metadata.DatItems.Formats
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using SabreTools.Data.Extensions;
|
||||||
|
|
||||||
namespace SabreTools.Metadata.DatItems.Formats
|
namespace SabreTools.Metadata.DatItems.Formats
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using SabreTools.Data.Extensions;
|
||||||
|
|
||||||
namespace SabreTools.Metadata.DatItems.Formats
|
namespace SabreTools.Metadata.DatItems.Formats
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using SabreTools.Data.Extensions;
|
||||||
|
|
||||||
namespace SabreTools.Metadata.DatItems.Formats
|
namespace SabreTools.Metadata.DatItems.Formats
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using SabreTools.Data.Extensions;
|
||||||
|
|
||||||
namespace SabreTools.Metadata.DatItems.Formats
|
namespace SabreTools.Metadata.DatItems.Formats
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using SabreTools.Data.Extensions;
|
||||||
|
|
||||||
namespace SabreTools.Metadata.DatItems.Formats
|
namespace SabreTools.Metadata.DatItems.Formats
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using SabreTools.Data.Extensions;
|
||||||
|
|
||||||
namespace SabreTools.Metadata.DatItems.Formats
|
namespace SabreTools.Metadata.DatItems.Formats
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using SabreTools.Data.Extensions;
|
||||||
|
|
||||||
namespace SabreTools.Metadata.DatItems.Formats
|
namespace SabreTools.Metadata.DatItems.Formats
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using SabreTools.Data.Extensions;
|
||||||
|
|
||||||
namespace SabreTools.Metadata.DatItems.Formats
|
namespace SabreTools.Metadata.DatItems.Formats
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using SabreTools.Data.Extensions;
|
||||||
|
|
||||||
namespace SabreTools.Metadata.DatItems.Formats
|
namespace SabreTools.Metadata.DatItems.Formats
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using SabreTools.Data.Extensions;
|
||||||
|
|
||||||
namespace SabreTools.Metadata.DatItems.Formats
|
namespace SabreTools.Metadata.DatItems.Formats
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using SabreTools.Data.Extensions;
|
||||||
|
|
||||||
namespace SabreTools.Metadata.DatItems.Formats
|
namespace SabreTools.Metadata.DatItems.Formats
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using SabreTools.Data.Extensions;
|
||||||
|
|
||||||
namespace SabreTools.Metadata.DatItems.Formats
|
namespace SabreTools.Metadata.DatItems.Formats
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using SabreTools.Data.Extensions;
|
||||||
|
|
||||||
namespace SabreTools.Metadata.DatItems.Formats
|
namespace SabreTools.Metadata.DatItems.Formats
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using SabreTools.Data.Extensions;
|
||||||
|
|
||||||
namespace SabreTools.Metadata.DatItems.Formats
|
namespace SabreTools.Metadata.DatItems.Formats
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using SabreTools.Data.Extensions;
|
||||||
|
|
||||||
namespace SabreTools.Metadata.DatItems
|
namespace SabreTools.Metadata.DatItems
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
using SabreTools.Data.Extensions;
|
||||||
using SabreTools.Data.Models.Metadata;
|
using SabreTools.Data.Models.Metadata;
|
||||||
using SabreTools.Text.Extensions;
|
using SabreTools.Text.Extensions;
|
||||||
|
|
||||||
|
|||||||
@@ -1,38 +0,0 @@
|
|||||||
using Xunit;
|
|
||||||
|
|
||||||
namespace SabreTools.Metadata.Test
|
|
||||||
{
|
|
||||||
public class ConvertersTests
|
|
||||||
{
|
|
||||||
#region String to Enum
|
|
||||||
|
|
||||||
[Theory]
|
|
||||||
[InlineData(null, null)]
|
|
||||||
[InlineData("INVALID", null)]
|
|
||||||
[InlineData("yes", true)]
|
|
||||||
[InlineData("True", true)]
|
|
||||||
[InlineData("no", false)]
|
|
||||||
[InlineData("False", false)]
|
|
||||||
public void AsYesNoTest(string? field, bool? expected)
|
|
||||||
{
|
|
||||||
bool? actual = field.AsYesNo();
|
|
||||||
Assert.Equal(expected, actual);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Enum to String
|
|
||||||
|
|
||||||
[Theory]
|
|
||||||
[InlineData(null, null)]
|
|
||||||
[InlineData(true, "yes")]
|
|
||||||
[InlineData(false, "no")]
|
|
||||||
public void FromYesNo(bool? field, string? expected)
|
|
||||||
{
|
|
||||||
string? actual = field.FromYesNo();
|
|
||||||
Assert.Equal(expected, actual);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
namespace SabreTools.Metadata
|
|
||||||
{
|
|
||||||
public static class Converters
|
|
||||||
{
|
|
||||||
#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
|
|
||||||
{
|
|
||||||
"yes" or "true" => true,
|
|
||||||
"no" or "false" => false,
|
|
||||||
_ => null,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Enum to String
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Get string value from input bool?
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="yesno">bool? to get value from</param>
|
|
||||||
/// <returns>String corresponding to the bool?</returns>
|
|
||||||
public static string? FromYesNo(this bool? yesno)
|
|
||||||
{
|
|
||||||
return yesno switch
|
|
||||||
{
|
|
||||||
true => "yes",
|
|
||||||
false => "no",
|
|
||||||
_ => null,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -8,45 +8,6 @@ namespace SabreTools.Metadata
|
|||||||
{
|
{
|
||||||
public static class DictionaryBaseExtensions
|
public static class DictionaryBaseExtensions
|
||||||
{
|
{
|
||||||
#region Cloning
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Deep clone a DictionaryBase object
|
|
||||||
/// </summary>
|
|
||||||
public static DictionaryBase? Clone(this DictionaryBase self)
|
|
||||||
{
|
|
||||||
// If construction failed, we can't do anything
|
|
||||||
if (Activator.CreateInstance(self.GetType()) is not DictionaryBase clone)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
// Loop through and clone per type
|
|
||||||
foreach (string key in self.Keys)
|
|
||||||
{
|
|
||||||
object? value = self[key];
|
|
||||||
clone[key] = value switch
|
|
||||||
{
|
|
||||||
// Primative types
|
|
||||||
bool or long or double or string => value,
|
|
||||||
|
|
||||||
// DictionaryBase types
|
|
||||||
DictionaryBase db => db.Clone(),
|
|
||||||
|
|
||||||
// Enumerable types
|
|
||||||
byte[] bytArr => bytArr.Clone(),
|
|
||||||
string[] strArr => strArr.Clone(),
|
|
||||||
DictionaryBase[] dbArr => Array.ConvertAll(dbArr, Clone),
|
|
||||||
ICloneable[] clArr => Array.ConvertAll(clArr, cl => cl.Clone()),
|
|
||||||
|
|
||||||
// Everything else just copies
|
|
||||||
_ => value,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
return clone;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Equality Checking
|
#region Equality Checking
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user