diff --git a/.gitignore b/.gitignore index 8dd0324..71fd847 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ results BenchmarkDotNet.Artifacts *.sln.DotSettings.user +*.received.* /AttributesExtractor.Benchmark/BenchmarkDotNet.Artifacts/ **/.DS_Store diff --git a/src/Apparatus.AOT.Reflection.Core/EnumHelper.cs b/src/Apparatus.AOT.Reflection.Core/EnumHelper.cs index 6edaa50..a682844 100644 --- a/src/Apparatus.AOT.Reflection.Core/EnumHelper.cs +++ b/src/Apparatus.AOT.Reflection.Core/EnumHelper.cs @@ -20,6 +20,57 @@ public static IEnumerable> GetEnumInfo() return data.Value.Values; } + public static bool IsDefined(TEnum value) + where TEnum : Enum + { + var data = EnumMetadataStore.Data; + if (data is null) + { + throw new InvalidOperationException( + $"Type '{typeof(TEnum).FullName}' is not registered. Use 'Apparatus.AOT.Reflection.GenericHelper.Bootstrap' to bootstrap it."); + } + + return data.Value.ContainsKey(value); + } + + public static string GetName(TEnum value) + where TEnum : Enum + { + var data = EnumMetadataStore.Data; + if (data is null) + { + throw new InvalidOperationException( + $"Type '{typeof(TEnum).FullName}' is not registered. Use 'Apparatus.AOT.Reflection.GenericHelper.Bootstrap' to bootstrap it."); + } + + return data.Value[value].Name; + } + + public static IEnumValueInfo CreateOrDefault(int value, TEnum @default) + where TEnum : Enum + { + var enumValue = FromInt(value); + return CreateOrDefault(enumValue, @default); + } + + public static IEnumValueInfo CreateOrDefault(TEnum value, TEnum @default) + where TEnum : Enum + { + var data = EnumMetadataStore.Data; + if (data is null) + { + throw new InvalidOperationException( + $"Type '{typeof(TEnum).FullName}' is not registered. Use 'Apparatus.AOT.Reflection.GenericHelper.Bootstrap' to bootstrap it."); + } + + if (IsDefined(value)) + { + return data.Value[value]; + } + + return data.Value[@default]; + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static TEnum FromInt(int value) where TEnum : Enum diff --git a/src/Apparatus.AOT.Reflection.Core/EnumValueInfo.cs b/src/Apparatus.AOT.Reflection.Core/EnumValueInfo.cs index 4613c23..22bb5e4 100644 --- a/src/Apparatus.AOT.Reflection.Core/EnumValueInfo.cs +++ b/src/Apparatus.AOT.Reflection.Core/EnumValueInfo.cs @@ -4,18 +4,6 @@ namespace Apparatus.AOT.Reflection { - public interface IEnumValueInfo - where TEnum : Enum - { - string Name { get; } - - Attribute[] Attributes { get; } - - int RawValue { get; } - - TEnum Value { get; } - } - public class EnumValueInfo : IEnumValueInfo, IEquatable> where TEnum : Enum { diff --git a/src/Apparatus.AOT.Reflection.Core/IEnumValueInfo.cs b/src/Apparatus.AOT.Reflection.Core/IEnumValueInfo.cs new file mode 100644 index 0000000..b3013bb --- /dev/null +++ b/src/Apparatus.AOT.Reflection.Core/IEnumValueInfo.cs @@ -0,0 +1,16 @@ +using System; + +namespace Apparatus.AOT.Reflection +{ + public interface IEnumValueInfo + where TEnum : Enum + { + string Name { get; } + + Attribute[] Attributes { get; } + + int RawValue { get; } + + TEnum Value { get; } + } +} \ No newline at end of file diff --git a/src/Apparatus.AOT.Reflection.Tests/AOTReflectionEnumsTests.CreateEnumFromIntWorks.verified.txt b/src/Apparatus.AOT.Reflection.Tests/AOTReflectionEnumsTests.CreateEnumFromIntWorks.verified.txt new file mode 100644 index 0000000..ae032d2 --- /dev/null +++ b/src/Apparatus.AOT.Reflection.Tests/AOTReflectionEnumsTests.CreateEnumFromIntWorks.verified.txt @@ -0,0 +1,11 @@ +{ + Name: Admin, + RawValue: 1, + Value: Admin, + Attributes: [ + { + Description: Admin user, + TypeId: DescriptionAttribute + } + ] +} \ No newline at end of file diff --git a/src/Apparatus.AOT.Reflection.Tests/AOTReflectionEnumsTests.cs b/src/Apparatus.AOT.Reflection.Tests/AOTReflectionEnumsTests.cs index af04e2a..58ea688 100644 --- a/src/Apparatus.AOT.Reflection.Tests/AOTReflectionEnumsTests.cs +++ b/src/Apparatus.AOT.Reflection.Tests/AOTReflectionEnumsTests.cs @@ -14,6 +14,15 @@ public async Task GetEnumValueInfoWorks() await Verify(result); } + + [Fact] + public async Task CreateEnumFromIntWorks() + { + var project = TestProject.Project; + var result = await project.ExecuteTest("return EnumHelper.CreateOrDefault(1, UserKind.User);"); + + await Verify(result); + } [Fact] public async Task GetEnumValueInfoWorksWithAttributes()