Skip to content

Implement proper parsing for primitives in config binding gen #84154

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

Merged
merged 2 commits into from
Apr 12, 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 @@ -22,11 +22,18 @@ public CollectionSpec(ITypeSymbol type) : base(type)
public CollectionSpec? ConcreteType { get; init; }
}

internal sealed record ArraySpec : CollectionSpec
{
public ArraySpec(ITypeSymbol type) : base(type) { }

public override TypeSpecKind SpecKind => TypeSpecKind.Array;
}

internal sealed record EnumerableSpec : CollectionSpec
{
public EnumerableSpec(ITypeSymbol type) : base(type) { }

public override TypeSpecKind SpecKind { get; init; } = TypeSpecKind.Enumerable;
public override TypeSpecKind SpecKind => TypeSpecKind.Enumerable;
}

internal sealed record DictionarySpec : CollectionSpec
Expand All @@ -35,6 +42,6 @@ public DictionarySpec(INamedTypeSymbol type) : base(type) { }

public override TypeSpecKind SpecKind => TypeSpecKind.Dictionary;

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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,26 @@ public sealed partial class ConfigurationBindingSourceGenerator
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);

// Unlike sourcegen warnings, exception messages should not be localized so we keep them in source.
private static class ExceptionMessages
private static class NotSupportedReason
{
public const string TypeNotSupported = "Unable to bind to type '{0}': '{1}'";
public const string AbstractOrInterfaceNotSupported = "Abstract or interface types are not supported";
public const string NeedPublicParameterlessConstructor = "Only objects with public parameterless ctors are supported";
public const string CollectionNotSupported = "The collection type is not supported";
public const string DictionaryKeyNotSupported = "The dictionary key type is not supported";
public const string ElementTypeNotSupported = "The collection element type is not supported";
public const string MultiDimArraysNotSupported = "Multidimensional arrays are not supported.";
public const string NullableUnderlyingTypeNotSupported = "Nullable underlying type is not supported";
public const string TypeNotDetectedAsInput = "Generator parser did not detect the type as input";
public const string TypeNotSupported = "The type is not supported";
}

private static class Identifier
{
public const string configuration = nameof(configuration);
public const string element = nameof(element);
public const string enumValue = nameof(enumValue);
public const string exception = nameof(exception);
public const string getPath = nameof(getPath);
public const string key = nameof(key);
public const string obj = nameof(obj);
public const string originalCount = nameof(originalCount);
Expand All @@ -56,14 +65,15 @@ private static class Identifier

public const string Add = nameof(Add);
public const string Any = nameof(Any);
public const string ArgumentNullException = nameof(ArgumentNullException);
public const string Array = nameof(Array);
public const string Bind = nameof(Bind);
public const string BindCore = nameof(BindCore);
public const string Configure = nameof(Configure);
public const string CopyTo = nameof(CopyTo);
public const string ContainsKey = nameof(ContainsKey);
public const string Count = nameof(Count);
public const string CultureInfo = nameof(CultureInfo);
public const string CultureNotFoundException = nameof(CultureNotFoundException);
public const string Enum = nameof(Enum);
public const string GeneratedConfigurationBinder = nameof(GeneratedConfigurationBinder);
public const string Get = nameof(Get);
Expand All @@ -76,42 +86,45 @@ private static class Identifier
public const string IConfiguration = nameof(IConfiguration);
public const string IConfigurationSection = nameof(IConfigurationSection);
public const string Int32 = "int";
public const string InvalidOperationException = nameof(InvalidOperationException);
public const string InvariantCulture = nameof(InvariantCulture);
public const string Length = nameof(Length);
public const string Parse = nameof(Parse);
public const string Path = nameof(Path);
public const string Resize = nameof(Resize);
public const string TryCreate = nameof(TryCreate);
public const string TryGetValue = nameof(TryGetValue);
public const string TryParse = nameof(TryParse);
public const string Uri = nameof(Uri);
public const string Value = nameof(Value);
}

private static class NotSupportedReason
{
public const string AbstractOrInterfaceNotSupported = "Abstract or interface types are not supported";
public const string NeedPublicParameterlessConstructor = "Only objects with public parameterless ctors are supported";
public const string CollectionNotSupported = "The collection type is not supported";
public const string DictionaryKeyNotSupported = "The dictionary key type is not supported";
public const string ElementTypeNotSupported = "The collection element type is not supported";
public const string MultiDimArraysNotSupported = "Multidimensional arrays are not supported.";
public const string NullableUnderlyingTypeNotSupported = "Nullable underlying type is not supported";
public const string TypeNotDetectedAsInput = "Generator parser did not detect the type as input";
public const string TypeNotSupported = "The type is not supported";
}

private static class TypeFullName
{
public const string ConfigurationKeyNameAttribute = "Microsoft.Extensions.Configuration.ConfigurationKeyNameAttribute";
public const string CultureInfo = "System.Globalization.CultureInfo";
public const string DateOnly = "System.DateOnly";
public const string DateTimeOffset = "System.DateTimeOffset";
public const string Dictionary = "System.Collections.Generic.Dictionary`2";
public const string GenericIDictionary = "System.Collections.Generic.IDictionary`2";
public const string Guid = "System.Guid";
public const string Half = "System.Half";
public const string HashSet = "System.Collections.Generic.HashSet`1";
public const string IConfiguration = "Microsoft.Extensions.Configuration.IConfiguration";
public const string IConfigurationSection = "Microsoft.Extensions.Configuration.IConfigurationSection";
public const string IDictionary = "System.Collections.Generic.IDictionary";
public const string Int128 = "System.Int128";
public const string ISet = "System.Collections.Generic.ISet`1";
public const string IServiceCollection = "Microsoft.Extensions.DependencyInjection.IServiceCollection";
public const string List = "System.Collections.Generic.List`1";
public const string TimeOnly = "System.TimeOnly";
public const string TimeSpan = "System.TimeSpan";
public const string UInt128 = "System.UInt128";
public const string Uri = "System.Uri";
public const string Version = "System.Version";
}

private static bool TypesAreEqual(ITypeSymbol first, ITypeSymbol second)
private static bool TypesAreEqual(ITypeSymbol first, ITypeSymbol? second)
=> first.Equals(second, SymbolEqualityComparer.Default);
}
}
Loading