Skip to content

Commit 529c7db

Browse files
StringyEnum now works with all datatypes lower or equal then uint
1 parent b372a60 commit 529c7db

File tree

7 files changed

+66
-17
lines changed

7 files changed

+66
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
3+
namespace StringyEnums.Shared.Models
4+
{
5+
[Flags]
6+
public enum TestByteEnum : byte
7+
{
8+
[StringRepresentation("Enum 1")]
9+
EnumOne = 1,
10+
[StringRepresentation("Enum 2")]
11+
EnumTwo = 2,
12+
[StringRepresentation("Enum 3")]
13+
EnumThree = 4,
14+
[StringRepresentation("Enum 4")]
15+
EnumFour = 8
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
3+
namespace StringyEnums.Shared.Models
4+
{
5+
[Flags]
6+
public enum TestUShortEnum : ushort
7+
{
8+
[StringRepresentation("Enum 1")]
9+
EnumOne = 1,
10+
[StringRepresentation("Enum 2")]
11+
EnumTwo = 2,
12+
[StringRepresentation("Enum 3")]
13+
EnumThree = 4,
14+
[StringRepresentation("Enum 4")]
15+
EnumFour = 8
16+
}
17+
}

src/StringyEnums/StringyEnums.Test/EnumExtensionTests.cs

+16-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ public class EnumExtensionTests
88
{
99
public EnumExtensionTests()
1010
{
11-
EnumCore.Init(init => init.InitWith<TestEnum>());
11+
EnumCore.Init(init =>
12+
{
13+
init.InitWith<TestEnum>();
14+
init.InitWith<TestByteEnum>();
15+
init.InitWith<TestUShortEnum>();
16+
});
1217
}
1318

1419
[Theory]
@@ -26,5 +31,15 @@ public void IsFlagRepresenationEqual(TestEnum enumVal, params string[] represent
2631
[InlineData(TestEnum.EnumOne, "Enum 1")]
2732
public void IsParseRepresenationEqual(TestEnum enumVal, string representation)
2833
=> Assert.Equal(representation.GetEnumFromRepresentation<TestEnum>(), enumVal);
34+
35+
[Theory]
36+
[InlineData(TestByteEnum.EnumOne, "Enum 1")]
37+
public void IsByteParseRepresenationEqual(TestByteEnum enumVal, string representation)
38+
=> Assert.Equal(representation.GetEnumFromRepresentation<TestByteEnum>(), enumVal);
39+
40+
[Theory]
41+
[InlineData(TestUShortEnum.EnumOne, "Enum 1")]
42+
public void IsUShortParseRepresenationEqual(TestUShortEnum enumVal, string representation)
43+
=> Assert.Equal(representation.GetEnumFromRepresentation<TestUShortEnum>(), enumVal);
2944
}
3045
}

src/StringyEnums/StringyEnums/CacheInitializer.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ namespace StringyEnums
1212
/// </summary>
1313
public class CacheInitializer
1414
{
15-
private readonly Dictionary<Type, IReadOnlyBiDictionary<int, string>> _tempCache;
15+
private readonly Dictionary<Type, IReadOnlyBiDictionary<uint, string>> _tempCache;
1616

1717
internal CacheInitializer()
1818
{
19-
_tempCache = new Dictionary<Type, IReadOnlyBiDictionary<int, string>>();
19+
_tempCache = new Dictionary<Type, IReadOnlyBiDictionary<uint, string>>();
2020
}
2121

2222
/// <summary>
@@ -70,15 +70,15 @@ private void InitWith(Type enumType)
7070

7171
var fields = enumType.GetFields();
7272

73-
var temptDict = new BiDictionary<int, string>();
73+
var temptDict = new BiDictionary<uint, string>();
7474

7575
foreach (var field in fields.Where(x => x.IsStatic))
7676
{
7777
var attr = field.GetCustomAttribute<StringRepresentationAttribute>();
7878

7979
if (attr is { })
8080
{
81-
temptDict.TryAdd((int)field.GetValue(enumType)!, attr.StringRepresentation);
81+
temptDict.TryAdd(Convert.ToUInt32(field.GetValue(enumType))!, attr.StringRepresentation);
8282
}
8383
}
8484

@@ -88,7 +88,7 @@ private void InitWith(Type enumType)
8888
}
8989
}
9090

91-
internal IReadOnlyDictionary<Type, IReadOnlyBiDictionary<int, string>> CustructCache()
92-
=> new ReadOnlyDictionary<Type, IReadOnlyBiDictionary<int, string>>(_tempCache.ToDictionary(k => k.Key, v => (IReadOnlyBiDictionary<int, string>)new ReadOnlyBiDictionary<int, string>(v.Value)));
91+
internal IReadOnlyDictionary<Type, IReadOnlyBiDictionary<uint, string>> CustructCache()
92+
=> new ReadOnlyDictionary<Type, IReadOnlyBiDictionary<uint, string>>(_tempCache.ToDictionary(k => k.Key, v => (IReadOnlyBiDictionary<uint, string>)new ReadOnlyBiDictionary<uint, string>(v.Value)));
9393
}
9494
}

src/StringyEnums/StringyEnums/EnumCore.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ namespace StringyEnums
1010
/// </summary>
1111
public static class EnumCore
1212
{
13-
private static IReadOnlyDictionary<Type, IReadOnlyBiDictionary<int, string>>? _representationCache;
13+
private static IReadOnlyDictionary<Type, IReadOnlyBiDictionary<uint, string>>? _representationCache;
1414

15-
internal static IReadOnlyDictionary<Type, IReadOnlyBiDictionary<int, string>> RepresentationCache
15+
internal static IReadOnlyDictionary<Type, IReadOnlyBiDictionary<uint, string>> RepresentationCache
1616
{
1717
get => _representationCache ?? throw new NotInitializedException("Please call the EnumCore.Initialize method at application startup.");
1818
private set => _representationCache = value;

src/StringyEnums/StringyEnums/EnumExtensions.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static class EnumExtensions
1616
/// <returns>The string representation of the given <paramref name="enumValue"/>.</returns>
1717
/// <exception cref="KeyNotFoundException">Is thrown when the given <paramref name="enumValue"/> doesn't contain any string representation or the <typeparamref name="TEnum"/> isn't cached.</exception>
1818
public static string GetRepresentation<TEnum>(this TEnum enumValue) where TEnum : struct, Enum
19-
=> EnumCore.RepresentationCache[typeof(TEnum)][(int)(dynamic)enumValue];
19+
=> EnumCore.RepresentationCache[typeof(TEnum)][(uint)(dynamic)enumValue];
2020

2121
/// <summary>
2222
/// Tries to get the string representation from the <see cref="StringRepresentationAttribute"/> on the given <typeparamref name="TEnum"/>.
@@ -29,7 +29,7 @@ public static bool TryGetRepresentation<TEnum>(this TEnum enumValue, out string?
2929
{
3030
if (EnumCore.RepresentationCache.TryGetValue(typeof(TEnum), out var dict))
3131
{
32-
return dict.TryGet((int)(dynamic)enumValue, out representation);
32+
return dict.TryGet((uint)(dynamic)enumValue, out representation);
3333
}
3434

3535
representation = default;
@@ -47,13 +47,13 @@ public static string[] GetFlagRepresentation<TEnum>(this TEnum enumValue) where
4747
{
4848
var dict = EnumCore.RepresentationCache[typeof(TEnum)];
4949

50-
var flags = (int)(dynamic)enumValue;
50+
var flags = (uint)(dynamic)enumValue;
5151

5252
var tempArr = new string[GetHammingWeight(flags)];
5353

5454
int lastIndex = 0;
5555

56-
for (int i = 1; i <= flags; i *= 2)
56+
for (uint i = 1; i <= flags; i *= 2)
5757
{
5858
if ((flags & i) != 0)
5959
{
@@ -75,13 +75,13 @@ public static bool TryGetFlagRepresentation<TEnum>(this TEnum enumValue, out str
7575
{
7676
if (EnumCore.RepresentationCache.TryGetValue(typeof(TEnum), out var dict))
7777
{
78-
var flags = (int)(dynamic)enumValue;
78+
var flags = (uint)(dynamic)enumValue;
7979

8080
representations = new string[GetHammingWeight(flags)];
8181

8282
int lastIndex = 0;
8383

84-
for (int i = 1; i <= flags; i *= 2)
84+
for (uint i = 1; i <= flags; i *= 2)
8585
{
8686
if ((flags & i) != 0)
8787
{
@@ -137,7 +137,7 @@ public static bool TryGetEnumFromRepresentation<TEnum>(this string value, out TE
137137
return false;
138138
}
139139

140-
private static int GetHammingWeight(int value)
140+
private static uint GetHammingWeight(uint value)
141141
{
142142
value -= ((value >> 1) & 0x55555555);
143143
value = (value & 0x33333333) + ((value >> 2) & 0x33333333);

src/StringyEnums/StringyEnums/StringyEnums.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<LangVersion>8</LangVersion>
66
<Nullable>enable</Nullable>
77
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
8-
<Version>1.0.0.1</Version>
8+
<Version>1.0.0.2</Version>
99
<Authors>Twenty</Authors>
1010
<Company>Twenty</Company>
1111
<Product>StringyEnums</Product>

0 commit comments

Comments
 (0)