From f380bd16723c137d7d25b079fd87f5cbac047abd Mon Sep 17 00:00:00 2001 From: Stanislav Silin Date: Wed, 12 Jun 2024 17:16:26 +0300 Subject: [PATCH] Add description. --- .../EnumValueInfo.cs | 10 ++++++++-- .../IEnumValueInfo.cs | 5 ++++- .../AotEnumReflectionSourceGenerator.cs | 15 ++++++++++++++- ...tEnumValueInfoWorksWithAttributes.verified.txt | 1 + 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/Apparatus.AOT.Reflection.Core/EnumValueInfo.cs b/src/Apparatus.AOT.Reflection.Core/EnumValueInfo.cs index 22bb5e4..dca0cfc 100644 --- a/src/Apparatus.AOT.Reflection.Core/EnumValueInfo.cs +++ b/src/Apparatus.AOT.Reflection.Core/EnumValueInfo.cs @@ -1,4 +1,5 @@ -using System; +#nullable enable +using System; using System.Collections.Generic; using System.Linq; @@ -51,6 +52,8 @@ public override int GetHashCode() var hashCode = (Name != null ? Name.GetHashCode() : 0); hashCode = (hashCode * 397) ^ Value.GetHashCode(); hashCode = (hashCode * 397) ^ (Attributes != null ? Attributes.GetHashCode() : 0); + hashCode = (hashCode * 397) ^ Description?.GetHashCode() ?? 0; + return hashCode; } } @@ -65,16 +68,19 @@ public override int GetHashCode() return !Equals(left, right); } - public EnumValueInfo(string name, int rawValue, TEnum value, Attribute[] attributes) + public EnumValueInfo(string name, string? description, int rawValue, TEnum value, Attribute[] attributes) { Name = name; RawValue = rawValue; Attributes = attributes; + Description = description; Value = value; } public string Name { get; } + public string? Description { get; } + public int RawValue { get; } public TEnum Value { get; } diff --git a/src/Apparatus.AOT.Reflection.Core/IEnumValueInfo.cs b/src/Apparatus.AOT.Reflection.Core/IEnumValueInfo.cs index b3013bb..abecbb3 100644 --- a/src/Apparatus.AOT.Reflection.Core/IEnumValueInfo.cs +++ b/src/Apparatus.AOT.Reflection.Core/IEnumValueInfo.cs @@ -1,4 +1,5 @@ -using System; +#nullable enable +using System; namespace Apparatus.AOT.Reflection { @@ -7,6 +8,8 @@ public interface IEnumValueInfo { string Name { get; } + string? Description { get; } + Attribute[] Attributes { get; } int RawValue { get; } diff --git a/src/Apparatus.AOT.Reflection.SourceGenerator/Reflection/AotEnumReflectionSourceGenerator.cs b/src/Apparatus.AOT.Reflection.SourceGenerator/Reflection/AotEnumReflectionSourceGenerator.cs index 85642d4..cf8f074 100644 --- a/src/Apparatus.AOT.Reflection.SourceGenerator/Reflection/AotEnumReflectionSourceGenerator.cs +++ b/src/Apparatus.AOT.Reflection.SourceGenerator/Reflection/AotEnumReflectionSourceGenerator.cs @@ -108,7 +108,7 @@ private string GenerateExtensionForEnum(ITypeSymbol typeToBake) .GetMembers() .OfType() .ToArray(); - + var source = $@" using System; using System.Linq; @@ -129,6 +129,7 @@ public static void Bootstrap() {fields.Select(o => $@" {{ {typeGlobalName}.{o.Name}, new EnumValueInfo<{typeGlobalName}>( ""{o.Name}"", + {(GetDescription(o) is string description ? $@"""{description}""" : "null")}, {o.ConstantValue}, {typeGlobalName}.{o.Name}, new Attribute[] @@ -144,5 +145,17 @@ public static void Bootstrap() "; return source; } + + private static string? GetDescription(IFieldSymbol field) + { + var description = field.GetAttributes() + .FirstOrDefault(o => o.AttributeClass?.Name == "DescriptionAttribute")? + .ConstructorArguments + .FirstOrDefault() + .Value? + .ToString(); + + return description; + } } } \ No newline at end of file diff --git a/src/Apparatus.AOT.Reflection.Tests/AOTReflectionEnumsTests.GetEnumValueInfoWorksWithAttributes.verified.txt b/src/Apparatus.AOT.Reflection.Tests/AOTReflectionEnumsTests.GetEnumValueInfoWorksWithAttributes.verified.txt index ae032d2..cee4cbd 100644 --- a/src/Apparatus.AOT.Reflection.Tests/AOTReflectionEnumsTests.GetEnumValueInfoWorksWithAttributes.verified.txt +++ b/src/Apparatus.AOT.Reflection.Tests/AOTReflectionEnumsTests.GetEnumValueInfoWorksWithAttributes.verified.txt @@ -1,5 +1,6 @@ { Name: Admin, + Description: Admin user, RawValue: 1, Value: Admin, Attributes: [