Skip to content

Added support for converting numeric values to enum #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/SimplePatch.Tests/PropertiesTypesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ public void EnumNullableProp()
CreateDelta<Person, Cool?>(x => x.Coolness, null).Patch(John);
Assert.IsNull(John.Coolness);
}



#region From string

[TestMethod]
Expand Down Expand Up @@ -122,5 +121,25 @@ public void EnumNullablePropFromString()
}

#endregion

#region From numeric

[TestMethod]
public void EnumPropFromInt()
{
var gender = Gender.Male;
CreateDelta<Person, Gender>(x => x.Gender, (int)gender).Patch(John);
Assert.AreEqual(gender, John.Gender);
}

[TestMethod]
public void EnumPropFromLong()
{
var gender = Gender.Male;
CreateDelta<Person, Gender>(x => x.Gender, (long)gender).Patch(John);
Assert.AreEqual(gender, John.Gender);
}

#endregion
}
}
17 changes: 12 additions & 5 deletions src/SimplePatch/Delta.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions src/SimplePatch/Helpers/TypeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

/// <summary>
/// Indicates if the type is a valid numeric type for Enums
/// </summary>
/// <param name="type">Type to check.</param>
/// <returns>True if the specified type is a valid numeric type for Enum, otherwise false.</returns>
internal static bool IsEnumNumericType(Type type) => EnumNumericTypes.Contains(type);

/// <summary>
/// Obtains the list of properties belonging to the specified identity as <see cref="IEnumerable{DeltaInfo}"/>
/// </summary>
Expand Down