From dcb364f535a84579d95fc0d35e95ec9a5b532fa4 Mon Sep 17 00:00:00 2001 From: pagratios Date: Mon, 21 Oct 2019 01:11:47 +0300 Subject: [PATCH] Added support for converting numeric values_to enum - Added IsEnumNumericType utility method to TypeHelper - Updated SetPropertiesValue now checking if new value is numeric and converts it to Enum - Added unit tests --- src/SimplePatch.Tests/PropertiesTypesTests.cs | 23 +++++++++++++-- src/SimplePatch/Delta.cs | 17 +++++++---- src/SimplePatch/Helpers/TypeHelper.cs | 28 +++++++++++++++++++ 3 files changed, 61 insertions(+), 7 deletions(-) diff --git a/src/SimplePatch.Tests/PropertiesTypesTests.cs b/src/SimplePatch.Tests/PropertiesTypesTests.cs index 109cb70..75960fa 100644 --- a/src/SimplePatch.Tests/PropertiesTypesTests.cs +++ b/src/SimplePatch.Tests/PropertiesTypesTests.cs @@ -71,8 +71,7 @@ public void EnumNullableProp() CreateDelta(x => x.Coolness, null).Patch(John); Assert.IsNull(John.Coolness); } - - + #region From string [TestMethod] @@ -122,5 +121,25 @@ public void EnumNullablePropFromString() } #endregion + + #region From numeric + + [TestMethod] + public void EnumPropFromInt() + { + var gender = Gender.Male; + CreateDelta(x => x.Gender, (int)gender).Patch(John); + Assert.AreEqual(gender, John.Gender); + } + + [TestMethod] + public void EnumPropFromLong() + { + var gender = Gender.Male; + CreateDelta(x => x.Gender, (long)gender).Patch(John); + Assert.AreEqual(gender, John.Gender); + } + + #endregion } } diff --git a/src/SimplePatch/Delta.cs b/src/SimplePatch/Delta.cs index c42ace0..fe1cbe7 100644 --- a/src/SimplePatch/Delta.cs +++ b/src/SimplePatch/Delta.cs @@ -240,18 +240,25 @@ private TEntity SetPropertiesValue(TEntity entity) if (truePropertyType == typeof(Guid) && newPropertyValueType == typeof(string)) { newPropertyValue = new Guid((string)newPropertyValue); - propertyInfo.SetValue(entity, newPropertyValue); } // Enum from string - else if (truePropertyType.GetTypeInfo().IsEnum && newPropertyValueType == typeof(string)) + else if (truePropertyType.GetTypeInfo().IsEnum) { - newPropertyValue = Enum.Parse(truePropertyType, (string)newPropertyValue); - propertyInfo.SetValue(entity, newPropertyValue); + if (newPropertyValueType == typeof(string)) + { + newPropertyValue = Enum.Parse(truePropertyType, (string) newPropertyValue); + } + else if (TypeHelper.IsEnumNumericType(newPropertyValueType)) + { + newPropertyValue = Enum.ToObject(truePropertyType, newPropertyValue); + } } else { - propertyInfo.SetValue(entity, Convert.ChangeType(newPropertyValue, truePropertyType)); + newPropertyValue = Convert.ChangeType(newPropertyValue, truePropertyType); } + + propertyInfo.SetValue(entity, newPropertyValue); } } } diff --git a/src/SimplePatch/Helpers/TypeHelper.cs b/src/SimplePatch/Helpers/TypeHelper.cs index 7f5ccc5..c2e601b 100644 --- a/src/SimplePatch/Helpers/TypeHelper.cs +++ b/src/SimplePatch/Helpers/TypeHelper.cs @@ -7,6 +7,34 @@ namespace SimplePatch.Helpers { internal class TypeHelper { + internal static readonly Type ByteType = typeof(byte); + internal static readonly Type IntType = typeof(int); + internal static readonly Type LongType = typeof(long); + internal static readonly Type SByteType = typeof(sbyte); + internal static readonly Type ShortType = typeof(short); + internal static readonly Type UIntType = typeof(uint); + internal static readonly Type ULongType = typeof(ulong); + internal static readonly Type UShortType = typeof(ushort); + + internal static readonly Type[] EnumNumericTypes = new Type[8] + { + ULongType, + LongType, + UIntType, + IntType, + UShortType, + ShortType, + ByteType, + SByteType + }; + + /// + /// Indicates if the type is a valid numeric type for Enums + /// + /// Type to check. + /// True if the specified type is a valid numeric type for Enum, otherwise false. + internal static bool IsEnumNumericType(Type type) => EnumNumericTypes.Contains(type); + /// /// Obtains the list of properties belonging to the specified identity as ///