-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for duplicate values in flag enums
- Loading branch information
Showing
21 changed files
with
493 additions
and
239 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
Src/EnumSourceGen.Tests.CodeGen/Resources/DuplicateValues.input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
//This test is checks if we support duplicate values for flag enums | ||
|
||
namespace Some.Namespace.Here; | ||
|
||
[Flags] | ||
[EnumSourceGen] | ||
public enum Profile | ||
{ | ||
Unknown = 0, | ||
WindowsLegacy = 1 << 0, | ||
WindowsServer2022 = 1 << 8, | ||
NewestServer = WindowsServer2022, | ||
} |
235 changes: 235 additions & 0 deletions
235
Src/EnumSourceGen.Tests.CodeGen/Resources/DuplicateValues.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,235 @@ | ||
// <auto-generated /> | ||
#nullable enable | ||
using System; | ||
|
||
namespace Some.Namespace.Here; | ||
|
||
[Flags] | ||
public enum ProfileFormat : byte | ||
{ | ||
None = 0, | ||
Name = 1, | ||
Value = 2, | ||
Default = Name | Value | ||
} | ||
// <auto-generated /> | ||
#nullable enable | ||
using System; | ||
|
||
namespace Some.Namespace.Here; | ||
|
||
public static partial class Enums | ||
{ | ||
public static partial class Profile | ||
{ | ||
public const int MemberCount = 4; | ||
public const bool IsFlagEnum = true; | ||
|
||
public static string[] GetMemberNames() => _names ??= new string[] { | ||
"Unknown", | ||
"WindowsLegacy", | ||
"WindowsServer2022", | ||
"NewestServer" | ||
}; | ||
|
||
public static Some.Namespace.Here.Profile[] GetMemberValues() => _values ??= new Some.Namespace.Here.Profile[] { | ||
Some.Namespace.Here.Profile.Unknown, | ||
Some.Namespace.Here.Profile.WindowsLegacy, | ||
Some.Namespace.Here.Profile.WindowsServer2022, | ||
Some.Namespace.Here.Profile.NewestServer | ||
}; | ||
|
||
public static Int32[] GetUnderlyingValues() => _underlyingValues ??= new Int32[] { | ||
0, | ||
1, | ||
256, | ||
256 | ||
}; | ||
|
||
public static bool TryParse(string value, out Some.Namespace.Here.Profile result, Some.Namespace.Here.ProfileFormat format = Some.Namespace.Here.ProfileFormat.Default, StringComparison comparison = StringComparison.Ordinal) | ||
{ | ||
if (format.HasFlag(Some.Namespace.Here.ProfileFormat.Name)) | ||
{ | ||
if (value.Equals("Unknown", comparison)) | ||
{ | ||
result = Some.Namespace.Here.Profile.Unknown; | ||
return true; | ||
} | ||
|
||
if (value.Equals("WindowsLegacy", comparison)) | ||
{ | ||
result = Some.Namespace.Here.Profile.WindowsLegacy; | ||
return true; | ||
} | ||
|
||
if (value.Equals("WindowsServer2022", comparison)) | ||
{ | ||
result = Some.Namespace.Here.Profile.WindowsServer2022; | ||
return true; | ||
} | ||
|
||
if (value.Equals("NewestServer", comparison)) | ||
{ | ||
result = Some.Namespace.Here.Profile.NewestServer; | ||
return true; | ||
} | ||
} | ||
if (format.HasFlag(Some.Namespace.Here.ProfileFormat.Value)) | ||
{ | ||
if (value.Equals("0", comparison)) | ||
{ | ||
result = Some.Namespace.Here.Profile.Unknown; | ||
return true; | ||
} | ||
|
||
if (value.Equals("1", comparison)) | ||
{ | ||
result = Some.Namespace.Here.Profile.WindowsLegacy; | ||
return true; | ||
} | ||
|
||
if (value.Equals("256", comparison)) | ||
{ | ||
result = Some.Namespace.Here.Profile.WindowsServer2022; | ||
return true; | ||
} | ||
|
||
if (value.Equals("256", comparison)) | ||
{ | ||
result = Some.Namespace.Here.Profile.NewestServer; | ||
return true; | ||
} | ||
} | ||
result = default; | ||
return false; | ||
} | ||
|
||
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_1_OR_GREATER | ||
public static bool TryParse(ReadOnlySpan<char> value, out Some.Namespace.Here.Profile result, Some.Namespace.Here.ProfileFormat format = Some.Namespace.Here.ProfileFormat.Default, StringComparison comparison = StringComparison.Ordinal) | ||
{ | ||
if (format.HasFlag(Some.Namespace.Here.ProfileFormat.Name)) | ||
{ | ||
if (value.Equals("Unknown", comparison)) | ||
{ | ||
result = Some.Namespace.Here.Profile.Unknown; | ||
return true; | ||
} | ||
|
||
if (value.Equals("WindowsLegacy", comparison)) | ||
{ | ||
result = Some.Namespace.Here.Profile.WindowsLegacy; | ||
return true; | ||
} | ||
|
||
if (value.Equals("WindowsServer2022", comparison)) | ||
{ | ||
result = Some.Namespace.Here.Profile.WindowsServer2022; | ||
return true; | ||
} | ||
|
||
if (value.Equals("NewestServer", comparison)) | ||
{ | ||
result = Some.Namespace.Here.Profile.NewestServer; | ||
return true; | ||
} | ||
} | ||
if (format.HasFlag(Some.Namespace.Here.ProfileFormat.Value)) | ||
{ | ||
if (value.Equals("0", comparison)) | ||
{ | ||
result = Some.Namespace.Here.Profile.Unknown; | ||
return true; | ||
} | ||
|
||
if (value.Equals("1", comparison)) | ||
{ | ||
result = Some.Namespace.Here.Profile.WindowsLegacy; | ||
return true; | ||
} | ||
|
||
if (value.Equals("256", comparison)) | ||
{ | ||
result = Some.Namespace.Here.Profile.WindowsServer2022; | ||
return true; | ||
} | ||
|
||
if (value.Equals("256", comparison)) | ||
{ | ||
result = Some.Namespace.Here.Profile.NewestServer; | ||
return true; | ||
} | ||
} | ||
result = default; | ||
return false; | ||
} | ||
|
||
public static Some.Namespace.Here.Profile Parse(ReadOnlySpan<char> value, Some.Namespace.Here.ProfileFormat format = Some.Namespace.Here.ProfileFormat.Default, StringComparison comparison = StringComparison.Ordinal) | ||
{ | ||
if (!TryParse(value, out Some.Namespace.Here.Profile result, format, comparison)) | ||
throw new ArgumentOutOfRangeException($"Invalid value: {value.ToString()}"); | ||
|
||
return result; | ||
} | ||
#endif | ||
|
||
public static Some.Namespace.Here.Profile Parse(string value, Some.Namespace.Here.ProfileFormat format = Some.Namespace.Here.ProfileFormat.Default, StringComparison comparison = StringComparison.Ordinal) | ||
{ | ||
if (!TryParse(value, out Some.Namespace.Here.Profile result, format, comparison)) | ||
throw new ArgumentOutOfRangeException($"Invalid value: {value}"); | ||
|
||
return result; | ||
} | ||
|
||
public static bool IsDefined(Some.Namespace.Here.Profile input) | ||
{ | ||
return unchecked(((Int32)257UL & (Int32)input) == (Int32)input); | ||
} | ||
|
||
private static string[]? _names; | ||
private static Some.Namespace.Here.Profile[]? _values; | ||
private static Int32[]? _underlyingValues; | ||
|
||
} | ||
} | ||
// <auto-generated /> | ||
#nullable enable | ||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace Some.Namespace.Here; | ||
|
||
public static partial class ProfileExtensions | ||
{ | ||
public static string GetString(this Some.Namespace.Here.Profile value) => value.ToString(); | ||
|
||
public static bool TryGetUnderlyingValue(this Some.Namespace.Here.Profile value, out Int32 underlyingValue) | ||
{ | ||
switch (value.ToString()) | ||
{ | ||
case "Unknown": | ||
underlyingValue = 0; | ||
return true; | ||
case "WindowsLegacy": | ||
underlyingValue = 1; | ||
return true; | ||
case "WindowsServer2022": | ||
underlyingValue = 256; | ||
return true; | ||
case "NewestServer": | ||
underlyingValue = 256; | ||
return true; | ||
} | ||
underlyingValue = default; | ||
return false; | ||
} | ||
|
||
public static Int32 GetUnderlyingValue(this Some.Namespace.Here.Profile value) | ||
{ | ||
if (!TryGetUnderlyingValue(value, out Int32 underlyingValue)) | ||
throw new ArgumentOutOfRangeException($"Invalid value: {value}"); | ||
|
||
return underlyingValue; | ||
} | ||
|
||
public static bool IsFlagSet(this Some.Namespace.Here.Profile value, Some.Namespace.Here.Profile flag) => ((Int32)value & (Int32)flag) == (Int32)flag; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.