Skip to content

Commit

Permalink
Add description.
Browse files Browse the repository at this point in the history
  • Loading branch information
byme8 committed Jun 12, 2024
1 parent a416434 commit f380bd1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/Apparatus.AOT.Reflection.Core/EnumValueInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;

Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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; }
Expand Down
5 changes: 4 additions & 1 deletion src/Apparatus.AOT.Reflection.Core/IEnumValueInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
#nullable enable
using System;

namespace Apparatus.AOT.Reflection
{
Expand All @@ -7,6 +8,8 @@ public interface IEnumValueInfo<TEnum>
{
string Name { get; }

string? Description { get; }

Attribute[] Attributes { get; }

int RawValue { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private string GenerateExtensionForEnum(ITypeSymbol typeToBake)
.GetMembers()
.OfType<IFieldSymbol>()
.ToArray();

var source = $@"
using System;
using System.Linq;
Expand All @@ -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[]
Expand All @@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
Name: Admin,
Description: Admin user,
RawValue: 1,
Value: Admin,
Attributes: [
Expand Down

0 comments on commit f380bd1

Please sign in to comment.