Skip to content

Commit

Permalink
tag attribute clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
ZacharyPatten committed Aug 14, 2022
1 parent 8622b1f commit 809afd7
Showing 1 changed file with 8 additions and 31 deletions.
39 changes: 8 additions & 31 deletions Sources/Towel/TagAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,7 @@ public static class TagAttributeExtensions
public static (bool Found, object? Value) GetTag(this MemberInfo memberInfo, object? tag)
{
if (memberInfo is null) throw new ArgumentNullException(nameof(memberInfo));
bool found = false;
object? value = default;
foreach (TagAttribute valueAttribute in memberInfo.GetCustomAttributes<TagAttribute>())
{
if (ReferenceEquals(tag, valueAttribute.Tag) || (tag is not null && tag.Equals(valueAttribute.Tag)))
{
if (found)
{
return (false, default);
}
found = true;
value = valueAttribute.Value;
}
}
return (found, value);
return Find(memberInfo.GetCustomAttributes<TagAttribute>(), tag);
}

/// <summary>Gets a <see cref="TagAttribute"/> on a <see cref="ParameterInfo"/>.</summary>
Expand All @@ -59,21 +45,7 @@ public static (bool Found, object? Value) GetTag(this MemberInfo memberInfo, obj
public static (bool Found, object? Value) GetTag(this ParameterInfo parameterInfo, object? tag)
{
if (parameterInfo is null) throw new ArgumentNullException(nameof(parameterInfo));
bool found = false;
object? value = default;
foreach (TagAttribute valueAttribute in parameterInfo.GetCustomAttributes<TagAttribute>())
{
if (ReferenceEquals(tag, valueAttribute.Tag) || (tag is not null && tag.Equals(valueAttribute.Tag)))
{
if (found)
{
return (false, default);
}
found = true;
value = valueAttribute.Value;
}
}
return (found, value);
return Find(parameterInfo.GetCustomAttributes<TagAttribute>(), tag);
}

/// <summary>Gets a <see cref="TagAttribute"/> on a <see cref="MemberInfo"/>.</summary>
Expand All @@ -88,9 +60,14 @@ public static (bool Found, object? Value) GetTag<TEnum>(this TEnum enumValue, ob
where TEnum : Enum
{
if (enumValue is null) throw new ArgumentNullException(nameof(enumValue));
return Find(enumValue.GetEnumAttributes<TagAttribute>(), tag);
}

internal static (bool Found, object? Value) Find(System.Collections.Generic.IEnumerable<TagAttribute> enumerable, object? tag)
{
bool found = false;
object? value = default;
foreach (TagAttribute valueAttribute in enumValue.GetEnumAttributes<TagAttribute>())
foreach (TagAttribute valueAttribute in enumerable)
{
if (ReferenceEquals(tag, valueAttribute.Tag) || (tag is not null && tag.Equals(valueAttribute.Tag)))
{
Expand Down

0 comments on commit 809afd7

Please sign in to comment.