Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Simplify GenerateX computation
  • Loading branch information
layomia committed Jun 25, 2021
commit 83221f1901d04fa1e715b7c5866c94bbd0407308
19 changes: 2 additions & 17 deletions src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,24 +349,9 @@ private static bool TryGetClassDeclarationList(INamedTypeSymbol typeSymbol, [Not
typeGenerationSpec.TypeInfoPropertyName = typeInfoPropertyName;
}

switch (generationMode)
if (generationMode != default)
{
case JsonSourceGenerationMode.Default:
break;
case JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization:
typeGenerationSpec.GenerateSerializationLogic = typeGenerationSpec.FastPathIsSupported();
typeGenerationSpec.GenerateMetadata = true;
break;
case JsonSourceGenerationMode.Metadata:
typeGenerationSpec.GenerateMetadata = true;
typeGenerationSpec.GenerateSerializationLogic = false;
break;
case JsonSourceGenerationMode.Serialization:
typeGenerationSpec.GenerateSerializationLogic = typeGenerationSpec.FastPathIsSupported();
typeGenerationSpec.GenerateMetadata = false;
break;
default:
throw new InvalidOperationException();
typeGenerationSpec.GenerationMode = generationMode;
}

return typeGenerationSpec;
Expand Down
26 changes: 7 additions & 19 deletions src/libraries/System.Text.Json/gen/TypeGenerationSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ namespace System.Text.Json.SourceGeneration
[DebuggerDisplay("Type={Type}, ClassType={ClassType}")]
internal class TypeGenerationSpec
{
private JsonSourceGenerationMode _generationMode;

/// <summary>
/// Fully qualified assembly name, prefixed with "global::", e.g. global::System.Numerics.BigInteger.
/// </summary>
Expand All @@ -25,21 +23,11 @@ internal class TypeGenerationSpec
/// </summary>
public string TypeInfoPropertyName { get; set; }

private bool? _generateMetadata;
public bool GenerateMetadata
{
get => _generateMetadata ??= GenerationModeIsSpecified(JsonSourceGenerationMode.Metadata);
// Optionally set during type metadata computation.
set => _generateMetadata = value;
}
public JsonSourceGenerationMode GenerationMode { get; set; }

private bool? _generateSerializationLogic;
public bool GenerateSerializationLogic
{
get => _generateSerializationLogic ??= GenerationModeIsSpecified(JsonSourceGenerationMode.Serialization) && FastPathIsSupported();
// Optionally set during type metadata computation.
set => _generateSerializationLogic = value;
}
public bool GenerateMetadata => GenerationModeIsSpecified(JsonSourceGenerationMode.Metadata);

public bool GenerateSerializationLogic => GenerationModeIsSpecified(JsonSourceGenerationMode.Serialization) && FastPathIsSupported();

public Type Type { get; private set; }

Expand Down Expand Up @@ -81,7 +69,7 @@ public void Initialize(
TypeGenerationSpec? nullableUnderlyingTypeMetadata,
string? converterInstantiationLogic)
{
_generationMode = generationMode;
GenerationMode = generationMode;
TypeRef = $"global::{typeRef}";
TypeInfoPropertyName = typeInfoPropertyName;
Type = type;
Expand All @@ -98,7 +86,7 @@ public void Initialize(
ConverterInstantiationLogic = converterInstantiationLogic;
}

public bool FastPathIsSupported()
private bool FastPathIsSupported()
{
if (ClassType == ClassType.Object)
{
Expand All @@ -118,6 +106,6 @@ public bool FastPathIsSupported()
return false;
}

private bool GenerationModeIsSpecified(JsonSourceGenerationMode mode) => _generationMode == JsonSourceGenerationMode.Default || (mode & _generationMode) != 0;
private bool GenerationModeIsSpecified(JsonSourceGenerationMode mode) => GenerationMode == JsonSourceGenerationMode.Default || (mode & GenerationMode) != 0;
}
}