Skip to content

Commit

Permalink
Make the soure generator format enums using their identifiers rather …
Browse files Browse the repository at this point in the history
…than numeric values. (#87557)

* Make the soure generator format enums using their identifiers rather than numeric values.

* Address feedback
  • Loading branch information
eiriktsarpalis authored Jun 15, 2023
1 parent 87aa3e9 commit a2f4a94
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
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);
}
}
}
23 changes: 8 additions & 15 deletions src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}>";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<Compile Include="..\Common\ThrowHelper.cs" Link="Common\System\Text\Json\ThrowHelper.cs" />
<Compile Include="$(CommonPath)\Roslyn\GetBestTypeByMetadataName.cs" Link="Common\Roslyn\GetBestTypeByMetadataName.cs" />
<Compile Include="Helpers\DiagnosticInfo.cs" />
<Compile Include="Helpers\SourceGeneratorHelpers.cs" />
<Compile Include="Helpers\ImmutableEquatableArray.cs" />
<Compile Include="Helpers\KnownTypeSymbols.cs" />
<Compile Include="Helpers\RoslynExtensions.cs" />
Expand Down

0 comments on commit a2f4a94

Please sign in to comment.