From a2f4a94e3f3bbbebfea5136dbe8039aba824b1df Mon Sep 17 00:00:00 2001 From: Eirik Tsarpalis Date: Thu, 15 Jun 2023 13:32:19 +0100 Subject: [PATCH] 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 --- .../gen/Helpers/SourceGeneratorHelpers.cs | 25 +++++++++++++++++++ .../gen/JsonSourceGenerator.Emitter.cs | 23 ++++++----------- .../System.Text.Json.SourceGeneration.targets | 1 + 3 files changed, 34 insertions(+), 15 deletions(-) create mode 100644 src/libraries/System.Text.Json/gen/Helpers/SourceGeneratorHelpers.cs diff --git a/src/libraries/System.Text.Json/gen/Helpers/SourceGeneratorHelpers.cs b/src/libraries/System.Text.Json/gen/Helpers/SourceGeneratorHelpers.cs new file mode 100644 index 0000000000000..26c5d3fb93773 --- /dev/null +++ b/src/libraries/System.Text.Json/gen/Helpers/SourceGeneratorHelpers.cs @@ -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(string fullyQualifiedName, TEnum value) where TEnum : struct, Enum + { + IEnumerable 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); + } + } +} diff --git a/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs b/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs index 068ee526206ea..2658b1df15640 100644 --- a/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs +++ b/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs @@ -1211,23 +1211,16 @@ private SourceText GetPropertyNameInitialization(ContextGenerationSpec contextSp return CompleteSourceFileAndReturnText(writer); } - private static string GetNumberHandlingAsStr(JsonNumberHandling? numberHandling) => - numberHandling switch - { - null => "null", - >= 0 => $"({JsonNumberHandlingTypeRef}){(int)numberHandling.Value}", - < 0 => $"({JsonNumberHandlingTypeRef})({(int)numberHandling.Value})" - }; + private static string GetNumberHandlingAsStr(JsonNumberHandling? numberHandling) + => numberHandling.HasValue + ? SourceGeneratorHelpers.FormatEnumLiteral(JsonNumberHandlingTypeRef, numberHandling.Value) + : "null"; - private static string GetObjectCreationHandlingAsStr(JsonObjectCreationHandling creationHandling) => - creationHandling >= 0 - ? $"({JsonObjectCreationHandlingTypeRef}){(int)creationHandling}" - : $"({JsonObjectCreationHandlingTypeRef})({(int)creationHandling})"; + private static string GetObjectCreationHandlingAsStr(JsonObjectCreationHandling creationHandling) + => SourceGeneratorHelpers.FormatEnumLiteral(JsonObjectCreationHandlingTypeRef, creationHandling); - private static string GetUnmappedMemberHandlingAsStr(JsonUnmappedMemberHandling unmappedMemberHandling) => - unmappedMemberHandling >= 0 - ? $"({JsonUnmappedMemberHandlingTypeRef}){(int)unmappedMemberHandling}" - : $"({JsonUnmappedMemberHandlingTypeRef})({(int)unmappedMemberHandling})"; + private static string GetUnmappedMemberHandlingAsStr(JsonUnmappedMemberHandling unmappedMemberHandling) + => SourceGeneratorHelpers.FormatEnumLiteral(JsonUnmappedMemberHandlingTypeRef, unmappedMemberHandling); private static string GetCreateValueInfoMethodRef(string typeCompilableName) => $"{CreateValueInfoMethodName}<{typeCompilableName}>"; diff --git a/src/libraries/System.Text.Json/gen/System.Text.Json.SourceGeneration.targets b/src/libraries/System.Text.Json/gen/System.Text.Json.SourceGeneration.targets index b7aa0a9f813a9..06fccb03c2bce 100644 --- a/src/libraries/System.Text.Json/gen/System.Text.Json.SourceGeneration.targets +++ b/src/libraries/System.Text.Json/gen/System.Text.Json.SourceGeneration.targets @@ -49,6 +49,7 @@ +