Skip to content

Commit

Permalink
Add ability to get enum values from int.
Browse files Browse the repository at this point in the history
  • Loading branch information
byme8 committed Jun 12, 2024
1 parent a780dd3 commit d25f19d
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ results
BenchmarkDotNet.Artifacts

*.sln.DotSettings.user
*.received.*
/AttributesExtractor.Benchmark/BenchmarkDotNet.Artifacts/

**/.DS_Store
51 changes: 51 additions & 0 deletions src/Apparatus.AOT.Reflection.Core/EnumHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,57 @@ public static IEnumerable<IEnumValueInfo<TEnum>> GetEnumInfo<TEnum>()
return data.Value.Values;
}

public static bool IsDefined<TEnum>(TEnum value)
where TEnum : Enum
{
var data = EnumMetadataStore<TEnum>.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>(TEnum value)
where TEnum : Enum
{
var data = EnumMetadataStore<TEnum>.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<TEnum> CreateOrDefault<TEnum>(int value, TEnum @default)
where TEnum : Enum
{
var enumValue = FromInt<TEnum>(value);
return CreateOrDefault(enumValue, @default);
}

public static IEnumValueInfo<TEnum> CreateOrDefault<TEnum>(TEnum value, TEnum @default)
where TEnum : Enum
{
var data = EnumMetadataStore<TEnum>.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<TEnum>(int value)
where TEnum : Enum
Expand Down
12 changes: 0 additions & 12 deletions src/Apparatus.AOT.Reflection.Core/EnumValueInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,6 @@

namespace Apparatus.AOT.Reflection
{
public interface IEnumValueInfo<TEnum>
where TEnum : Enum
{
string Name { get; }

Attribute[] Attributes { get; }

int RawValue { get; }

TEnum Value { get; }
}

public class EnumValueInfo<TEnum> : IEnumValueInfo<TEnum>, IEquatable<EnumValueInfo<TEnum>>
where TEnum : Enum
{
Expand Down
16 changes: 16 additions & 0 deletions src/Apparatus.AOT.Reflection.Core/IEnumValueInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;

namespace Apparatus.AOT.Reflection
{
public interface IEnumValueInfo<TEnum>
where TEnum : Enum
{
string Name { get; }

Attribute[] Attributes { get; }

int RawValue { get; }

TEnum Value { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
Name: Admin,
RawValue: 1,
Value: Admin,
Attributes: [
{
Description: Admin user,
TypeId: DescriptionAttribute
}
]
}
9 changes: 9 additions & 0 deletions src/Apparatus.AOT.Reflection.Tests/AOTReflectionEnumsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit d25f19d

Please sign in to comment.