Skip to content

Commit

Permalink
Support ConfigurationBinder.GetValue in source-gen (#86076)
Browse files Browse the repository at this point in the history
* Support ConfigurationBinder.GetValue in source-gen

* Address feedback
  • Loading branch information
layomia authored May 11, 2023
1 parent c7eeb33 commit 844c2a9
Show file tree
Hide file tree
Showing 11 changed files with 361 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public static class Identifier
public const string binderOptions = nameof(binderOptions);
public const string configureActions = nameof(configureActions);
public const string configuration = nameof(configuration);
public const string defaultValue = nameof(defaultValue);
public const string element = nameof(element);
public const string enumValue = nameof(enumValue);
public const string exception = nameof(exception);
Expand Down Expand Up @@ -126,7 +127,7 @@ public Emitter(SourceProductionContext context, SourceGenerationSpec generationS

public void Emit()
{
if (!_generationSpec.ShouldEmitMethods(MethodSpecifier.GetMethods | MethodSpecifier.BindMethods | MethodSpecifier.Configure))
if (!_generationSpec.HasRootMethods())
{
return;
}
Expand Down Expand Up @@ -160,6 +161,7 @@ public void Emit()

_writer.WriteBlockStart($"internal static class {Identifier.Helpers}");
EmitGetCoreMethod();
EmitGetValueCoreMethod();
EmitBindCoreMethods();
EmitHelperMethods();

Expand Down Expand Up @@ -217,7 +219,7 @@ private void EmitGetMethods()
{
EmitBlankLineIfRequired();
_writer.WriteLine($"public static T? {Identifier.Get}<T>(this {FullyQualifiedDisplayName.IConfiguration} {Identifier.configuration}, {FullyQualifiedDisplayName.ActionOfBinderOptions}? {Identifier.configureActions}) => " +
$"(T?)((T){expressionForGetCore}({Identifier.configuration}, typeof(T), {Identifier.configureActions}) ?? default(T));");
$"(T?)({expressionForGetCore}({Identifier.configuration}, typeof(T), {Identifier.configureActions}) ?? default(T));");
}

if (_generationSpec.ShouldEmitMethods(MethodSpecifier.Get_TypeOf))
Expand All @@ -231,38 +233,40 @@ private void EmitGetMethods()
{
EmitBlankLineIfRequired();
_writer.WriteLine($"public static object? {Identifier.Get}(this {FullyQualifiedDisplayName.IConfiguration} {Identifier.configuration}, {FullyQualifiedDisplayName.Type} {Identifier.type}, {FullyQualifiedDisplayName.ActionOfBinderOptions}? {Identifier.configureActions}) => " +
$"{expressionForGetCore}({Identifier.configuration}, type, {Identifier.configureActions});");
$"{expressionForGetCore}({Identifier.configuration}, {Identifier.type}, {Identifier.configureActions});");
}
}

private void EmitGetValueMethods()
{
const string expressionForGetValueCore = $"{FullyQualifiedDisplayName.Helpers}.{Identifier.GetValueCore}";

if (_generationSpec.ShouldEmitMethods(MethodSpecifier.GetValue_T_key))
{
EmitBlankLineIfRequired();
_writer.WriteLine($"public static T? {Identifier.GetValue}<T>(this {FullyQualifiedDisplayName.IConfiguration} {Identifier.configuration}, string {Identifier.key}) => " +
$"throw new {FullyQualifiedDisplayName.NotSupportedException}();");
$"(T?)({expressionForGetValueCore}({Identifier.configuration}, typeof(T), {Identifier.key}) ?? default(T));");
}

if (_generationSpec.ShouldEmitMethods(MethodSpecifier.GetValue_T_key_defaultValue))
{
EmitBlankLineIfRequired();
_writer.WriteLine($"public static T? {Identifier.GetValue}<T>(this {FullyQualifiedDisplayName.IConfiguration} {Identifier.configuration}, {FullyQualifiedDisplayName.ActionOfBinderOptions}? {Identifier.configureActions}) => " +
$"throw new NotSupportedException();");
_writer.WriteLine($"public static T? {Identifier.GetValue}<T>(this {FullyQualifiedDisplayName.IConfiguration} {Identifier.configuration}, string {Identifier.key}, T {Identifier.defaultValue}) => " +
$"(T?)({expressionForGetValueCore}({Identifier.configuration}, typeof(T), {Identifier.key}) ?? {Identifier.defaultValue});");
}

if (_generationSpec.ShouldEmitMethods(MethodSpecifier.GetValue_TypeOf_key))
{
EmitBlankLineIfRequired();
_writer.WriteLine($"public static object? {Identifier.GetValue}(this {FullyQualifiedDisplayName.IConfiguration} {Identifier.configuration}, {FullyQualifiedDisplayName.Type} {Identifier.type}) => " +
$"throw new NotSupportedException();");
_writer.WriteLine($"public static object? {Identifier.GetValue}(this {FullyQualifiedDisplayName.IConfiguration} {Identifier.configuration}, {FullyQualifiedDisplayName.Type} {Identifier.type}, string {Identifier.key}) => " +
$"{expressionForGetValueCore}({Identifier.configuration}, {Identifier.type}, {Identifier.key});");
}

if (_generationSpec.ShouldEmitMethods(MethodSpecifier.GetValue_TypeOf_key_defaultValue))
{
EmitBlankLineIfRequired();
_writer.WriteLine($"public static object? {Identifier.GetValue}(this {FullyQualifiedDisplayName.IConfiguration} {Identifier.configuration}, {FullyQualifiedDisplayName.Type} {Identifier.type}, {FullyQualifiedDisplayName.ActionOfBinderOptions}? {Identifier.configureActions}) =>" +
$"throw new NotSupportedException();");
_writer.WriteLine($"public static object? {Identifier.GetValue}(this {FullyQualifiedDisplayName.IConfiguration} {Identifier.configuration}, {FullyQualifiedDisplayName.Type} {Identifier.type}, string {Identifier.key}, object? {Identifier.defaultValue}) =>" +
$"{expressionForGetValueCore}({Identifier.configuration}, {Identifier.type}, {Identifier.key}) ?? {Identifier.defaultValue};");
}
}

Expand Down Expand Up @@ -325,7 +329,7 @@ private void EmitGetCoreMethod()
return;
}

_writer.WriteBlockStart($"public static object {Identifier.GetCore}(this {Identifier.IConfiguration} {Identifier.configuration}, Type {Identifier.type}, Action<{Identifier.BinderOptions}>? {Identifier.configureActions})");
_writer.WriteBlockStart($"public static object? {Identifier.GetCore}(this {Identifier.IConfiguration} {Identifier.configuration}, Type {Identifier.type}, Action<{Identifier.BinderOptions}>? {Identifier.configureActions})");

EmitCheckForNullArgument_WithBlankLine(Identifier.configuration);

Expand All @@ -351,8 +355,50 @@ private void EmitGetCoreMethod()
_precedingBlockExists = true;
}

private void EmitGetValueCoreMethod()
{
if (!_generationSpec.ShouldEmitMethods(MethodSpecifier.GetValueMethods))
{
return;
}

EmitBlankLineIfRequired();

_writer.WriteBlockStart($"public static object? {Identifier.GetValueCore}(this {Identifier.IConfiguration} {Identifier.configuration}, Type {Identifier.type}, string {Identifier.key})");

EmitCheckForNullArgument_WithBlankLine(Identifier.configuration);

_writer.WriteLine($"{Identifier.IConfigurationSection} {Identifier.section} = {Identifier.configuration}.{Identifier.GetSection}({Identifier.key});");
_writer.WriteLine($"object? {Identifier.obj};");

_writer.WriteBlankLine();

foreach (TypeSpec type in _generationSpec.RootConfigTypes[MethodSpecifier.GetValueMethods])
{
TypeSpec effectiveType = (type as NullableSpec)?.UnderlyingType ?? type;
_writer.WriteBlockStart($"if (type == typeof({type.MinimalDisplayString}))");
EmitBindLogicFromString(
(ParsableFromStringTypeSpec)effectiveType,
Identifier.obj,
Expression.sectionValue,
Expression.sectionPath,
writeOnSuccess: () => _writer.WriteLine($"return {Identifier.obj};"));
_writer.WriteBlockEnd();
_writer.WriteBlankLine();
}

_writer.WriteLine("return null;");
_writer.WriteBlockEnd();
_precedingBlockExists = true;
}

private void EmitBindCoreMethods()
{
if (!_generationSpec.ShouldEmitMethods(MethodSpecifier.BindCore))
{
return;
}

foreach (TypeSpec type in _generationSpec.RootConfigTypes[MethodSpecifier.BindCore])
{
if (type.SpecKind is TypeSpecKind.ParsableFromString)
Expand Down Expand Up @@ -1047,7 +1093,7 @@ private void EmitCastToIConfigurationSection()

private void EmitIConfigurationHasValueOrChildrenCheck(bool voidReturn)
{
string returnPostfix = voidReturn ? "" : " default!";
string returnPostfix = voidReturn ? string.Empty : " null";

_writer.WriteBlock($$"""
if (!{{GetHelperMethodDisplayString(Identifier.HasValueOrChildren)}}({{Identifier.configuration}}))
Expand Down Expand Up @@ -1078,6 +1124,9 @@ private void EmitBlankLineIfRequired()
private void Emit_NotSupportedException_TypeNotDetectedAsInput() =>
_writer.WriteLine(@$"throw new global::System.NotSupportedException($""{string.Format(ExceptionMessages.TypeNotDetectedAsInput, "{type}")}"");");

private void Emit_NotSupportedExceptionTypeNotSupportedAsInput() =>
_writer.WriteLine(@$"throw new global::System.NotSupportedException($""{string.Format(ExceptionMessages.TypeNotSupportedAsInput, "{type}")}"");");

private void EmitCheckForNullArgument_WithBlankLine_IfRequired(bool isValueType)
{
if (!isValueType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using Microsoft.CodeAnalysis;

namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
Expand Down
Loading

0 comments on commit 844c2a9

Please sign in to comment.