Skip to content

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
layomia committed Jul 11, 2023
1 parent b4e2921 commit b65b0a4
Show file tree
Hide file tree
Showing 37 changed files with 775 additions and 699 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@

using System;
using System.Diagnostics;
using System.Reflection;
using System.Text.RegularExpressions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;

namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
{
Expand All @@ -17,20 +15,13 @@ private sealed partial class Emitter
private readonly SourceProductionContext _context;
private readonly SourceGenerationSpec _sourceGenSpec;

// Postfix for stringValueX variables used to save config value indexer
// results e.g. if (configuration["Key"] is string stringValue0) { ... }
private int _parseValueCount;

private bool _emitBlankLineBeforeNextStatement;

private readonly SourceWriter _writer = new();
private bool _useFullyQualifiedNames;
private int _valueSuffixIndex;

private static readonly Regex s_arrayBracketsRegex = new(Regex.Escape("[]"));

private static readonly AssemblyName s_assemblyName = typeof(Emitter).Assembly.GetName();
private static readonly string s_generatedCodeAttributeSource = $@"[global::System.CodeDom.Compiler.GeneratedCodeAttribute(""{s_assemblyName.Name}"", ""{s_assemblyName.Version}"")]";

public bool _useFullyQualifiedNames { get; private set; }
private readonly SourceWriter _writer = new();

public Emitter(SourceProductionContext context, SourceGenerationSpec sourceGenSpec)
{
Expand Down Expand Up @@ -113,30 +104,29 @@ void EmitObjectInit(string objExpression, InitializationKind initKind)
}
}

public void EmitBindLogicFromString(
private void EmitBindLogicFromString(
ParsableFromStringSpec type,
string sectionValueExpr,
string sectionPathExpr,
Action<string>? writeOnSuccess = null,
bool checkForNullSectionValue = false)
Action<string>? writeOnSuccess,
bool checkForNullSectionValue,
bool useIncrementalStringValueIdentifier)
{
StringParsableTypeKind typeKind = type.StringParsableTypeKind;
Debug.Assert(typeKind is not StringParsableTypeKind.None);

string stringValueIdentifier = checkForNullSectionValue ?
GetIncrementalIdentifier(Identifier.stringValue)
: sectionValueExpr;
string nonNull_StringValue_Identifier = useIncrementalStringValueIdentifier ? GetIncrementalIdentifier(Identifier.value) : Identifier.value;
string stringValueToParse_Expr = checkForNullSectionValue ? nonNull_StringValue_Identifier : sectionValueExpr;

string parsedValueExpr;

if (typeKind is StringParsableTypeKind.AssignFromSectionValue)
{
parsedValueExpr = stringValueIdentifier;
parsedValueExpr = stringValueToParse_Expr;
}
else
{
string helperMethodDisplayString = GetHelperMethodDisplayString(type.ParseMethodName);
parsedValueExpr = $"{helperMethodDisplayString}({stringValueIdentifier}, () => {sectionPathExpr})";
parsedValueExpr = $"{helperMethodDisplayString}({stringValueToParse_Expr}, () => {sectionPathExpr})";
}

if (!checkForNullSectionValue)
Expand All @@ -145,7 +135,7 @@ public void EmitBindLogicFromString(
}
else
{
_writer.WriteBlockStart($"if ({sectionValueExpr} is string {stringValueIdentifier})");
_writer.WriteBlockStart($"if ({sectionValueExpr} is string {nonNull_StringValue_Identifier})");
writeOnSuccess?.Invoke(parsedValueExpr);
_writer.WriteBlockEnd();
}
Expand Down Expand Up @@ -217,7 +207,7 @@ private bool EmitObjectInit(TypeSpec type, string memberAccessExpr, Initializati
return true;
}

public void EmitCastToIConfigurationSection()
private void EmitCastToIConfigurationSection()
{
string sectionTypeDisplayString;
string exceptionTypeDisplayString;
Expand All @@ -240,7 +230,7 @@ public void EmitCastToIConfigurationSection()
""");
}

public void EmitIConfigurationHasValueOrChildrenCheck(bool voidReturn)
private void EmitIConfigurationHasValueOrChildrenCheck(bool voidReturn)
{
string returnPostfix = voidReturn ? string.Empty : " null";
string methodDisplayString = GetHelperMethodDisplayString(Identifier.HasValueOrChildren);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ private DictionarySpec CreateDictionarySpec(INamedTypeSymbol type, Location? loc
return null;
}

RegisterHasChildrenHelperForGenIfRequired(elementSpec);
Register_AsConfigWithChildren_HelperForGen_IfRequired(elementSpec);

EnumerableSpec spec = new(type)
{
Expand Down Expand Up @@ -615,7 +615,7 @@ private DictionarySpec CreateDictionarySpec(INamedTypeSymbol type, Location? loc
{
PropertySpec spec = new(property) { Type = propertyTypeSpec, ConfigurationKeyName = configKeyName };
objectSpec.Properties[propertyName] = spec;
RegisterHasChildrenHelperForGenIfRequired(propertyTypeSpec);
Register_AsConfigWithChildren_HelperForGen_IfRequired(propertyTypeSpec);
}
}
}
Expand Down Expand Up @@ -687,14 +687,14 @@ private DictionarySpec CreateDictionarySpec(INamedTypeSymbol type, Location? loc
return objectSpec;
}

private void RegisterHasChildrenHelperForGenIfRequired(TypeSpec type)
private void Register_AsConfigWithChildren_HelperForGen_IfRequired(TypeSpec type)
{
if (type.SpecKind is TypeSpecKind.Object or
TypeSpecKind.Enumerable or
TypeSpecKind.Dictionary)
{

_sourceGenSpec.ShouldEmitHasChildren = true;
_sourceGenSpec.MethodsToGen_CoreBindingHelper |= MethodsToGen_CoreBindingHelper.AsConfigWithChildren;
}
}

Expand Down
Loading

0 comments on commit b65b0a4

Please sign in to comment.