-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make the soure generator format enums using their identifiers rather …
…than numeric values. (#87557) * Make the soure generator format enums using their identifiers rather than numeric values. * Address feedback
- Loading branch information
1 parent
87aa3e9
commit a2f4a94
Showing
3 changed files
with
34 additions
and
15 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
src/libraries/System.Text.Json/gen/Helpers/SourceGeneratorHelpers.cs
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,25 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Linq; | ||
using System.Collections.Generic; | ||
|
||
namespace System.Text.Json.SourceGeneration | ||
{ | ||
internal static class SourceGeneratorHelpers | ||
{ | ||
private static readonly char[] s_enumSeparator = new char[] { ',' }; | ||
|
||
public static string FormatEnumLiteral<TEnum>(string fullyQualifiedName, TEnum value) where TEnum : struct, Enum | ||
{ | ||
IEnumerable<string> values = value.ToString().Split(s_enumSeparator, StringSplitOptions.RemoveEmptyEntries) | ||
.Select(name => name.Trim()) | ||
.Select(name => | ||
int.TryParse(name, out _) | ||
? $"({fullyQualifiedName})({name})" | ||
: $"{fullyQualifiedName}.{name}"); | ||
|
||
return string.Join(" | ", values); | ||
} | ||
} | ||
} |
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