Skip to content

Commit

Permalink
Add global alias prefix to disambiguate System namespace (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewlock authored Mar 9, 2023
1 parent 2a69eeb commit c8ecb00
Show file tree
Hide file tree
Showing 13 changed files with 222 additions and 209 deletions.
38 changes: 19 additions & 19 deletions src/NetEscapades.EnumGenerators/SourceGenerationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ namespace NetEscapades.EnumGenerators
/// <summary>
/// Add to enums to indicate that extension methods should be generated for the type
/// </summary>
[System.AttributeUsage(System.AttributeTargets.Enum)]
[System.Diagnostics.Conditional(""NETESCAPADES_ENUMGENERATORS_USAGES"")]
public class EnumExtensionsAttribute : System.Attribute
[global::System.AttributeUsage(global::System.AttributeTargets.Enum)]
[global::System.Diagnostics.Conditional(""NETESCAPADES_ENUMGENERATORS_USAGES"")]
public class EnumExtensionsAttribute : global::System.Attribute
{
/// <summary>
/// The namespace to generate the extension class.
Expand Down Expand Up @@ -110,13 +110,13 @@ public static string ToStringFast(this ").Append(enumToGenerate.FullyQualifiedNa
/// <summary>
/// Determines whether one or more bit fields are set in the current instance.
/// Equivalent to calling <see cref=""System.Enum.HasFlag"" /> on <paramref name=""value""/>.
/// Equivalent to calling <see cref=""global::System.Enum.HasFlag"" /> on <paramref name=""value""/>.
/// </summary>
/// <param name=""value"">The value of the instance to investigate</param>
/// <param name=""flag"">The flag to check for</param>
/// <returns><c>true</c> if the fields set in the flag are also set in the current instance; otherwise <c>false</c>.</returns>
/// <remarks>If the underlying value of <paramref name=""flag""/> is zero, the method returns true.
/// This is consistent with the behaviour of <see cref=""System.Enum.HasFlag"" /></remarks>
/// This is consistent with the behaviour of <see cref=""global::System.Enum.HasFlag"" /></remarks>
public static bool HasFlagFast(this ").Append(enumToGenerate.FullyQualifiedName).Append(@" value, ").Append(enumToGenerate.FullyQualifiedName).Append(@" flag)
=> flag == 0 ? true : (value & flag) == flag;");
}
Expand Down Expand Up @@ -242,7 +242,7 @@ public static bool IsDefined(in ReadOnlySpan<char> name, bool allowMatchingMetad
if (member.Value.DisplayName is not null && member.Value.IsDisplayNameTheFirstPresence)
{
sb.Append(@"
ReadOnlySpan<char> current when current.Equals(""").Append(member.Value.DisplayName).Append(@""".AsSpan(), System.StringComparison.Ordinal) => true,");
ReadOnlySpan<char> current when current.Equals(""").Append(member.Value.DisplayName).Append(@""".AsSpan(), global::System.StringComparison.Ordinal) => true,");
}
}

Expand All @@ -265,7 +265,7 @@ public static bool IsDefined(in ReadOnlySpan<char> name, bool allowMatchingMetad
{
sb.Append(@"
ReadOnlySpan<char> current when current.Equals(nameof(").Append(enumToGenerate.FullyQualifiedName).Append('.').Append(member.Key)
.Append(@").AsSpan(), System.StringComparison.Ordinal) => true,");
.Append(@").AsSpan(), global::System.StringComparison.Ordinal) => true,");
}

sb.Append(@"
Expand All @@ -290,7 +290,7 @@ ReadOnlySpan<char> current when current.Equals(nameof(").Append(enumToGenerate.F
/// <returns><c>true</c> if the value parameter was converted successfully; otherwise, <c>false</c>.</returns>
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
#endif
string? name,
out ").Append(enumToGenerate.FullyQualifiedName).Append(@" value)
Expand All @@ -312,7 +312,7 @@ public static bool TryParse(
/// <returns><c>true</c> if the value parameter was converted successfully; otherwise, <c>false</c>.</returns>
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
#endif
string? name,
out ").Append(enumToGenerate.FullyQualifiedName).Append(@" value,
Expand All @@ -337,7 +337,7 @@ public static bool TryParse(
/// <returns><c>true</c> if the value parameter was converted successfully; otherwise, <c>false</c>.</returns>
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
#endif
string? name,
out ").Append(enumToGenerate.FullyQualifiedName).Append(@" value,
Expand All @@ -359,7 +359,7 @@ public static bool TryParse(
if (member.Value.DisplayName is not null && member.Value.IsDisplayNameTheFirstPresence)
{
sb.Append(@"
case string s when s.Equals(""").Append(member.Value.DisplayName).Append(@""", System.StringComparison.OrdinalIgnoreCase):
case string s when s.Equals(""").Append(member.Value.DisplayName).Append(@""", global::System.StringComparison.OrdinalIgnoreCase):
value = ").Append(enumToGenerate.FullyQualifiedName).Append('.').Append(member.Key).Append(@";
return true;");
}
Expand Down Expand Up @@ -403,7 +403,7 @@ public static bool TryParse(
{
sb.Append(@"
case string s when s.Equals(nameof(").Append(enumToGenerate.FullyQualifiedName).Append('.').Append(member.Key).Append(
@"), System.StringComparison.OrdinalIgnoreCase):
@"), global::System.StringComparison.OrdinalIgnoreCase):
value = ").Append(enumToGenerate.FullyQualifiedName).Append('.').Append(member.Key).Append(@";
return true;");
}
Expand Down Expand Up @@ -457,7 +457,7 @@ public static bool TryParse(
/// <returns><c>true</c> if the value parameter was converted successfully; otherwise, <c>false</c>.</returns>
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
#endif
in ReadOnlySpan<char> name,
out ").Append(enumToGenerate.FullyQualifiedName).Append(@" value)
Expand All @@ -479,7 +479,7 @@ public static bool TryParse(
/// <returns><c>true</c> if the value parameter was converted successfully; otherwise, <c>false</c>.</returns>
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
#endif
in ReadOnlySpan<char> name,
out ").Append(enumToGenerate.FullyQualifiedName).Append(@" value,
Expand All @@ -505,7 +505,7 @@ public static bool TryParse(
/// <returns><c>true</c> if the value parameter was converted successfully; otherwise, <c>false</c>.</returns>
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
#endif
in ReadOnlySpan<char> name,
out ").Append(enumToGenerate.FullyQualifiedName).Append(@" result,
Expand All @@ -528,7 +528,7 @@ public static bool TryParse(
{
sb.Append(@"
case ReadOnlySpan<char> current when current.Equals(""").Append(member.Value.DisplayName).Append(
@""".AsSpan(), System.StringComparison.OrdinalIgnoreCase):
@""".AsSpan(), global::System.StringComparison.OrdinalIgnoreCase):
result = ").Append(enumToGenerate.FullyQualifiedName).Append('.').Append(member.Key).Append(@";
return true;");
}
Expand All @@ -548,7 +548,7 @@ public static bool TryParse(
if (member.Value.DisplayName is not null && member.Value.IsDisplayNameTheFirstPresence)
{
sb.Append(@"
case ReadOnlySpan<char> current when current.Equals(""").Append(member.Value.DisplayName).Append(@""".AsSpan(), System.StringComparison.Ordinal):
case ReadOnlySpan<char> current when current.Equals(""").Append(member.Value.DisplayName).Append(@""".AsSpan(), global::System.StringComparison.Ordinal):
result = ").Append(enumToGenerate.FullyQualifiedName).Append('.').Append(member.Key).Append(@";
return true;");
}
Expand All @@ -572,7 +572,7 @@ public static bool TryParse(
{
sb.Append(@"
case ReadOnlySpan<char> current when current.Equals(nameof(").Append(enumToGenerate.FullyQualifiedName).Append('.').Append(member.Key).Append(
@").AsSpan(), System.StringComparison.OrdinalIgnoreCase):
@").AsSpan(), global::System.StringComparison.OrdinalIgnoreCase):
result = ").Append(enumToGenerate.FullyQualifiedName).Append('.').Append(member.Key).Append(@";
return true;");
}
Expand All @@ -594,7 +594,7 @@ public static bool TryParse(
{
sb.Append(@"
case ReadOnlySpan<char> current when current.Equals(nameof(").Append(enumToGenerate.FullyQualifiedName).Append('.').Append(member.Key).Append(
@").AsSpan(), System.StringComparison.Ordinal):
@").AsSpan(), global::System.StringComparison.Ordinal):
result = ").Append(enumToGenerate.FullyQualifiedName).Append('.').Append(member.Key).Append(@";
return true;");
}
Expand Down
13 changes: 13 additions & 0 deletions tests/NetEscapades.EnumGenerators.IntegrationTests/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace System
{
using NetEscapades.EnumGenerators;

[EnumExtensions]
public enum EnumInSystem
{
First = 0,
Second = 1,
Third = 2,
}
}

namespace NetEscapades.EnumGenerators.IntegrationTests
{
[EnumExtensions]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ namespace MyTestNameSpace

/// <summary>
/// Determines whether one or more bit fields are set in the current instance.
/// Equivalent to calling <see cref="System.Enum.HasFlag" /> on <paramref name="value"/>.
/// Equivalent to calling <see cref="global::System.Enum.HasFlag" /> on <paramref name="value"/>.
/// </summary>
/// <param name="value">The value of the instance to investigate</param>
/// <param name="flag">The flag to check for</param>
/// <returns><c>true</c> if the fields set in the flag are also set in the current instance; otherwise <c>false</c>.</returns>
/// <remarks>If the underlying value of <paramref name="flag"/> is zero, the method returns true.
/// This is consistent with the behaviour of <see cref="System.Enum.HasFlag" /></remarks>
/// This is consistent with the behaviour of <see cref="global::System.Enum.HasFlag" /></remarks>
public static bool HasFlagFast(this MyTestNameSpace.MyEnum value, MyTestNameSpace.MyEnum flag)
=> flag == 0 ? true : (value & flag) == flag;

Expand Down Expand Up @@ -117,9 +117,9 @@ namespace MyTestNameSpace
{
return name switch
{
ReadOnlySpan<char> current when current.Equals(nameof(MyTestNameSpace.MyEnum.First).AsSpan(), System.StringComparison.Ordinal) => true,
ReadOnlySpan<char> current when current.Equals(nameof(MyTestNameSpace.MyEnum.Second).AsSpan(), System.StringComparison.Ordinal) => true,
ReadOnlySpan<char> current when current.Equals(nameof(MyTestNameSpace.MyEnum.Third).AsSpan(), System.StringComparison.Ordinal) => true,
ReadOnlySpan<char> current when current.Equals(nameof(MyTestNameSpace.MyEnum.First).AsSpan(), global::System.StringComparison.Ordinal) => true,
ReadOnlySpan<char> current when current.Equals(nameof(MyTestNameSpace.MyEnum.Second).AsSpan(), global::System.StringComparison.Ordinal) => true,
ReadOnlySpan<char> current when current.Equals(nameof(MyTestNameSpace.MyEnum.Third).AsSpan(), global::System.StringComparison.Ordinal) => true,
_ => false,
};
}
Expand All @@ -139,7 +139,7 @@ namespace MyTestNameSpace
/// <returns><c>true</c> if the value parameter was converted successfully; otherwise, <c>false</c>.</returns>
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
#endif
string? name,
out MyTestNameSpace.MyEnum value)
Expand All @@ -160,7 +160,7 @@ namespace MyTestNameSpace
/// <returns><c>true</c> if the value parameter was converted successfully; otherwise, <c>false</c>.</returns>
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
#endif
string? name,
out MyTestNameSpace.MyEnum value,
Expand All @@ -184,7 +184,7 @@ namespace MyTestNameSpace
/// <returns><c>true</c> if the value parameter was converted successfully; otherwise, <c>false</c>.</returns>
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
#endif
string? name,
out MyTestNameSpace.MyEnum value,
Expand All @@ -195,13 +195,13 @@ namespace MyTestNameSpace
{
switch (name)
{
case string s when s.Equals(nameof(MyTestNameSpace.MyEnum.First), System.StringComparison.OrdinalIgnoreCase):
case string s when s.Equals(nameof(MyTestNameSpace.MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase):
value = MyTestNameSpace.MyEnum.First;
return true;
case string s when s.Equals(nameof(MyTestNameSpace.MyEnum.Second), System.StringComparison.OrdinalIgnoreCase):
case string s when s.Equals(nameof(MyTestNameSpace.MyEnum.Second), global::System.StringComparison.OrdinalIgnoreCase):
value = MyTestNameSpace.MyEnum.Second;
return true;
case string s when s.Equals(nameof(MyTestNameSpace.MyEnum.Third), System.StringComparison.OrdinalIgnoreCase):
case string s when s.Equals(nameof(MyTestNameSpace.MyEnum.Third), global::System.StringComparison.OrdinalIgnoreCase):
value = MyTestNameSpace.MyEnum.Third;
return true;
case string s when int.TryParse(name, out var val):
Expand Down Expand Up @@ -250,7 +250,7 @@ namespace MyTestNameSpace
/// <returns><c>true</c> if the value parameter was converted successfully; otherwise, <c>false</c>.</returns>
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
#endif
in ReadOnlySpan<char> name,
out MyTestNameSpace.MyEnum value)
Expand All @@ -271,7 +271,7 @@ namespace MyTestNameSpace
/// <returns><c>true</c> if the value parameter was converted successfully; otherwise, <c>false</c>.</returns>
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
#endif
in ReadOnlySpan<char> name,
out MyTestNameSpace.MyEnum value,
Expand All @@ -295,7 +295,7 @@ namespace MyTestNameSpace
/// <returns><c>true</c> if the value parameter was converted successfully; otherwise, <c>false</c>.</returns>
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
#endif
in ReadOnlySpan<char> name,
out MyTestNameSpace.MyEnum result,
Expand All @@ -306,13 +306,13 @@ namespace MyTestNameSpace
{
switch (name)
{
case ReadOnlySpan<char> current when current.Equals(nameof(MyTestNameSpace.MyEnum.First).AsSpan(), System.StringComparison.OrdinalIgnoreCase):
case ReadOnlySpan<char> current when current.Equals(nameof(MyTestNameSpace.MyEnum.First).AsSpan(), global::System.StringComparison.OrdinalIgnoreCase):
result = MyTestNameSpace.MyEnum.First;
return true;
case ReadOnlySpan<char> current when current.Equals(nameof(MyTestNameSpace.MyEnum.Second).AsSpan(), System.StringComparison.OrdinalIgnoreCase):
case ReadOnlySpan<char> current when current.Equals(nameof(MyTestNameSpace.MyEnum.Second).AsSpan(), global::System.StringComparison.OrdinalIgnoreCase):
result = MyTestNameSpace.MyEnum.Second;
return true;
case ReadOnlySpan<char> current when current.Equals(nameof(MyTestNameSpace.MyEnum.Third).AsSpan(), System.StringComparison.OrdinalIgnoreCase):
case ReadOnlySpan<char> current when current.Equals(nameof(MyTestNameSpace.MyEnum.Third).AsSpan(), global::System.StringComparison.OrdinalIgnoreCase):
result = MyTestNameSpace.MyEnum.Third;
return true;
case ReadOnlySpan<char> current when int.TryParse(name, out var numericResult):
Expand All @@ -327,13 +327,13 @@ namespace MyTestNameSpace
{
switch (name)
{
case ReadOnlySpan<char> current when current.Equals(nameof(MyTestNameSpace.MyEnum.First).AsSpan(), System.StringComparison.Ordinal):
case ReadOnlySpan<char> current when current.Equals(nameof(MyTestNameSpace.MyEnum.First).AsSpan(), global::System.StringComparison.Ordinal):
result = MyTestNameSpace.MyEnum.First;
return true;
case ReadOnlySpan<char> current when current.Equals(nameof(MyTestNameSpace.MyEnum.Second).AsSpan(), System.StringComparison.Ordinal):
case ReadOnlySpan<char> current when current.Equals(nameof(MyTestNameSpace.MyEnum.Second).AsSpan(), global::System.StringComparison.Ordinal):
result = MyTestNameSpace.MyEnum.Second;
return true;
case ReadOnlySpan<char> current when current.Equals(nameof(MyTestNameSpace.MyEnum.Third).AsSpan(), System.StringComparison.Ordinal):
case ReadOnlySpan<char> current when current.Equals(nameof(MyTestNameSpace.MyEnum.Third).AsSpan(), global::System.StringComparison.Ordinal):
result = MyTestNameSpace.MyEnum.Third;
return true;
case ReadOnlySpan<char> current when int.TryParse(name, out var numericResult):
Expand Down
Loading

0 comments on commit c8ecb00

Please sign in to comment.