Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename a couple model specs for config binding generator #86459

Merged
merged 1 commit into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ private void EmitBindMethods()
#region Helper class in source-generation namespace.
private void EmitHelperUsingStatements()
{
foreach (string @namespace in _generationSpec.Namespaces)
foreach (string @namespace in _generationSpec.TypeNamespaces)
{
_writer.WriteLine($"using {@namespace};");
}
Expand Down Expand Up @@ -287,7 +287,7 @@ private void EmitGetValueCoreMethod()
TypeSpec effectiveType = (type as NullableSpec)?.UnderlyingType ?? type;
_writer.WriteBlockStart($"if (type == typeof({type.MinimalDisplayString}))");
EmitBindLogicFromString(
(ParsableFromStringTypeSpec)effectiveType,
(ParsableFromStringSpec)effectiveType,
Identifier.obj,
Expression.sectionValue,
Expression.sectionPath,
Expand Down Expand Up @@ -354,7 +354,7 @@ private void EmitHelperMethods()

if (_generationSpec.PrimitivesForHelperGen.Count > 0)
{
foreach (ParsableFromStringTypeSpec type in _generationSpec.PrimitivesForHelperGen)
foreach (ParsableFromStringSpec type in _generationSpec.PrimitivesForHelperGen)
{
EmitBlankLineIfRequired();
EmitPrimitiveParseMethod(type);
Expand Down Expand Up @@ -413,7 +413,7 @@ private void EmitGetBinderOptionsHelper()
""");
}

private void EmitPrimitiveParseMethod(ParsableFromStringTypeSpec type)
private void EmitPrimitiveParseMethod(ParsableFromStringSpec type)
{
string innerExceptionTypeDisplayString;
string cultureInfoTypeDisplayString;
Expand Down Expand Up @@ -589,7 +589,7 @@ private void EmitPopulationImplForEnumerableWithAdd(EnumerableSpec type)

if (elementType.SpecKind is TypeSpecKind.ParsableFromString)
{
ParsableFromStringTypeSpec stringParsableType = (ParsableFromStringTypeSpec)elementType;
ParsableFromStringSpec stringParsableType = (ParsableFromStringSpec)elementType;
if (stringParsableType.StringParsableTypeKind is StringParsableTypeKind.ConfigValue)
{
string tempVarName = GetIncrementalVarName(Identifier.stringValue);
Expand Down Expand Up @@ -620,7 +620,7 @@ private void EmitBindCoreImplForDictionary(DictionarySpec type)

_writer.WriteBlockStart($"foreach ({Identifier.IConfigurationSection} {Identifier.section} in {Identifier.configuration}.{Identifier.GetChildren}())");

ParsableFromStringTypeSpec keyType = type.KeyType;
ParsableFromStringSpec keyType = type.KeyType;
TypeSpec elementType = type.ElementType;

// Parse key
Expand All @@ -644,7 +644,7 @@ void Emit_BindAndAddLogic_ForElement()
{
if (elementType.SpecKind == TypeSpecKind.ParsableFromString)
{
ParsableFromStringTypeSpec stringParsableType = (ParsableFromStringTypeSpec)elementType;
ParsableFromStringSpec stringParsableType = (ParsableFromStringSpec)elementType;
if (stringParsableType.StringParsableTypeKind is StringParsableTypeKind.ConfigValue)
{
string tempVarName = GetIncrementalVarName(Identifier.stringValue);
Expand Down Expand Up @@ -786,7 +786,7 @@ private void EmitBindCoreImplForProperty(PropertySpec property, TypeSpec propert
if (canSet)
{
EmitBindLogicFromString(
(ParsableFromStringTypeSpec)propertyType,
(ParsableFromStringSpec)propertyType,
expressionForPropertyAccess,
expressionForConfigValueIndexer,
expressionForConfigValuePath: Expression.sectionPath);
Expand Down Expand Up @@ -839,7 +839,7 @@ private void EmitBindLogicFromRootMethod(TypeSpec type, string expressionForMemb
{
EmitCastToIConfigurationSection();
}
EmitBindLogicFromString((ParsableFromStringTypeSpec)type, expressionForMemberAccess, Expression.sectionValue, Expression.sectionPath);
EmitBindLogicFromString((ParsableFromStringSpec)type, expressionForMemberAccess, Expression.sectionValue, Expression.sectionPath);
}
else
{
Expand Down Expand Up @@ -944,7 +944,7 @@ private void EmitBindCoreCallForProperty(
}

private void EmitBindLogicFromString(
ParsableFromStringTypeSpec type,
ParsableFromStringSpec type,
string expressionForMemberAccess,
string expressionForConfigStringValue,
string expressionForConfigValuePath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ private sealed class Parser
private readonly HashSet<ITypeSymbol> _unsupportedTypes = new(SymbolEqualityComparer.Default);
private readonly Dictionary<ITypeSymbol, TypeSpec?> _createdSpecs = new(SymbolEqualityComparer.Default);

private readonly HashSet<ParsableFromStringTypeSpec> _primitivesForHelperGen = new();
private readonly HashSet<string> _namespaces = new()
private readonly HashSet<ParsableFromStringSpec> _primitivesForHelperGen = new();
private readonly HashSet<string> _typeNamespaces = new()
{
"System",
"System.Globalization",
Expand Down Expand Up @@ -86,7 +86,7 @@ public Parser(SourceProductionContext context, KnownTypeSymbols typeSymbols)
_rootConfigTypes,
_methodsToGen,
_primitivesForHelperGen,
_namespaces);
_typeNamespaces);
}

private void ProcessBindCall(BinderInvocationOperation binderOperation)
Expand Down Expand Up @@ -352,7 +352,7 @@ private void ProcessConfigureCall(BinderInvocationOperation binderOperation)
}
else if (IsParsableFromString(type, out StringParsableTypeKind specialTypeKind))
{
ParsableFromStringTypeSpec stringParsableSpec = new(type)
ParsableFromStringSpec stringParsableSpec = new(type)
{
Location = location,
StringParsableTypeKind = specialTypeKind
Expand All @@ -377,7 +377,7 @@ private void ProcessConfigureCall(BinderInvocationOperation binderOperation)
}
else if (SymbolEqualityComparer.Default.Equals(type, _typeSymbols.IConfigurationSection))
{
spec = new ConfigurationSectionTypeSpec(type) { Location = location };
spec = new ConfigurationSectionSpec(type) { Location = location };
}
else if (type is INamedTypeSymbol namedType)
{
Expand All @@ -394,7 +394,7 @@ private void ProcessConfigureCall(BinderInvocationOperation binderOperation)
string @namespace = spec.Namespace;
if (@namespace is not null and not "<global namespace>")
{
_namespaces.Add(@namespace);
_typeNamespaces.Add(@namespace);
}

_createdSpecs[type] = spec;
Expand Down Expand Up @@ -657,7 +657,7 @@ private DictionarySpec CreateDictionarySpec(INamedTypeSymbol type, Location? loc
constructionStrategy = ConstructionStrategy.ToEnumerableMethod;
populationStrategy = CollectionPopulationStrategy.Cast_Then_Add;
toEnumerableMethodCall = "ToDictionary(pair => pair.Key, pair => pair.Value)";
_namespaces.Add("System.Linq");
_typeNamespaces.Add("System.Linq");
}
else
{
Expand All @@ -668,7 +668,7 @@ private DictionarySpec CreateDictionarySpec(INamedTypeSymbol type, Location? loc
DictionarySpec spec = new(type)
{
Location = location,
KeyType = (ParsableFromStringTypeSpec)keySpec,
KeyType = (ParsableFromStringSpec)keySpec,
ElementType = elementSpec,
ConstructionStrategy = constructionStrategy,
PopulationStrategy = populationStrategy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@
<Compile Include="Helpers\SourceWriter.cs" />
<Compile Include="Model\CollectionSpec.cs" />
<Compile Include="Model\ConstructionStrategy.cs" />
<Compile Include="Model\ConfigurationSectionTypeSpec.cs" />
<Compile Include="Model\ConfigurationSectionSpec.cs" />
<Compile Include="Model\NullableSpec.cs" />
<Compile Include="Model\ObjectSpec.cs" />
<Compile Include="Model\ParsableFromStringTypeSpec.cs" />
<Compile Include="Model\ParsableFromStringSpec.cs" />
<Compile Include="Model\PropertySpec.cs" />
<Compile Include="Model\TypeSpecKind.cs" />
<Compile Include="Model\TypeSpec.cs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public DictionarySpec(INamedTypeSymbol type) : base(type) { }

public override TypeSpecKind SpecKind => TypeSpecKind.Dictionary;

public required ParsableFromStringTypeSpec KeyType { get; init; }
public required ParsableFromStringSpec KeyType { get; init; }
}

internal enum CollectionPopulationStrategy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
{
internal sealed record ConfigurationSectionTypeSpec : TypeSpec
internal sealed record ConfigurationSectionSpec : TypeSpec
{
public ConfigurationSectionTypeSpec(ITypeSymbol type) : base(type) { }
public ConfigurationSectionSpec(ITypeSymbol type) : base(type) { }

public override TypeSpecKind SpecKind => TypeSpecKind.IConfigurationSection;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
{
internal sealed record ParsableFromStringTypeSpec : TypeSpec
internal sealed record ParsableFromStringSpec : TypeSpec
{
public ParsableFromStringTypeSpec(ITypeSymbol type) : base(type) { }
public ParsableFromStringSpec(ITypeSymbol type) : base(type) { }

public override TypeSpecKind SpecKind => TypeSpecKind.ParsableFromString;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
internal sealed record SourceGenerationSpec(
Dictionary<BinderMethodSpecifier, HashSet<TypeSpec>> RootConfigTypes,
BinderMethodSpecifier MethodsToGen,
HashSet<ParsableFromStringTypeSpec> PrimitivesForHelperGen,
HashSet<string> Namespaces)
HashSet<ParsableFromStringSpec> PrimitivesForHelperGen,
HashSet<string> TypeNamespaces)
{
public bool HasRootMethods() =>
ShouldEmitMethods(BinderMethodSpecifier.Get | BinderMethodSpecifier.Bind | BinderMethodSpecifier.Configure | BinderMethodSpecifier.GetValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,16 @@ public TypeSpec(ITypeSymbol type)
/// </summary>
public required Location? Location { get; init; }
}

internal enum TypeSpecKind
{
Unknown = 0,
ParsableFromString = 1,
Object = 2,
Array = 3,
Enumerable = 4,
Dictionary = 5,
IConfigurationSection = 6,
Nullable = 7,
}
}

This file was deleted.