Skip to content

Commit

Permalink
fix merge issues
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrobsaila committed Aug 27, 2023
1 parent 135be17 commit e80c9ae
Show file tree
Hide file tree
Showing 35 changed files with 896 additions and 775 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@
#nullable enable
#pragma warning disable CS0612, CS0618 // Suppress warnings about [Obsolete] member usage in generated code.

/// <summary>Generated helper providing an AOT and linking compatible implementation for configuration binding.</summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Configuration.Binder.SourceGeneration", "42.42.42.42")]
internal static class GeneratedConfigurationBinder
namespace System.Runtime.CompilerServices
{
/// <summary>Attempts to bind the configuration instance to a new instance of type T.</summary>
public static T? Get<T>(this global::Microsoft.Extensions.Configuration.IConfiguration configuration) => (T?)(global::Microsoft.Extensions.Configuration.Binder.SourceGeneration.CoreBindingHelper.GetCore(configuration, typeof(T), configureOptions: null) ?? default(T));
using System;
using System.CodeDom.Compiler;

[GeneratedCode("Microsoft.Extensions.Configuration.Binder.SourceGeneration", "42.42.42.42")]
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
file sealed class InterceptsLocationAttribute : Attribute
{
public InterceptsLocationAttribute(string filePath, int line, int column)
{
}
}
}

namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
Expand All @@ -18,11 +25,18 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;

/// <summary>Provide core binding logic.</summary>
[GeneratedCode("Microsoft.Extensions.Configuration.Binder.SourceGeneration", "42.42.42.42")]
file static class CoreBindingHelper
file static class BindingExtensions
{
#region IConfiguration extensions.
/// <summary>Attempts to bind the configuration instance to a new instance of type T.</summary>
[InterceptsLocationAttribute(@"src-0.cs", 12, 17)]
public static T? Get<T>(this IConfiguration configuration) => (T?)(GetCore(configuration, typeof(T), configureOptions: null) ?? default(T));
#endregion IConfiguration extensions.

#region Core binding extensions.
private readonly static Lazy<HashSet<string>> s_configKeys_ProgramMyClassWithCustomCollections = new(() => new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "CustomDictionary", "CustomList", "IReadOnlyList", "IReadOnlyDictionary" });

public static object? GetCore(this IConfiguration configuration, Type type, Action<BinderOptions>? configureOptions)
Expand All @@ -46,16 +60,11 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
return obj;
}

throw new global::System.NotSupportedException($"Unable to bind to type '{type}': generator did not detect the type as input.");
throw new NotSupportedException($"Unable to bind to type '{type}': generator did not detect the type as input.");
}

public static void BindCore(IConfiguration configuration, ref Program.CustomDictionary<string, int> obj, BinderOptions? binderOptions)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}

foreach (IConfigurationSection section in configuration.GetChildren())
{
if (section.Value is string value)
Expand All @@ -67,11 +76,6 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration

public static void BindCore(IConfiguration configuration, ref Program.CustomList obj, BinderOptions? binderOptions)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}

foreach (IConfigurationSection section in configuration.GetChildren())
{
if (section.Value is string value)
Expand All @@ -83,11 +87,6 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration

public static void BindCore(IConfiguration configuration, ref List<int> obj, BinderOptions? binderOptions)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}

foreach (IConfigurationSection section in configuration.GetChildren())
{
if (section.Value is string value)
Expand All @@ -99,11 +98,6 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration

public static void BindCore(IConfiguration configuration, ref ICollection<int> obj, BinderOptions? binderOptions)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}

foreach (IConfigurationSection section in configuration.GetChildren())
{
if (section.Value is string value)
Expand All @@ -115,11 +109,6 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration

public static void BindCore(IConfiguration configuration, ref IReadOnlyList<int> obj, BinderOptions? binderOptions)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}

if (obj is not ICollection<int> temp)
{
return;
Expand All @@ -136,11 +125,6 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration

public static void BindCore(IConfiguration configuration, ref Dictionary<string, int> obj, BinderOptions? binderOptions)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}

foreach (IConfigurationSection section in configuration.GetChildren())
{
if (section.Value is string value)
Expand All @@ -152,11 +136,6 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration

public static void BindCore(IConfiguration configuration, ref IDictionary<string, int> obj, BinderOptions? binderOptions)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}

foreach (IConfigurationSection section in configuration.GetChildren())
{
if (section.Value is string value)
Expand All @@ -168,11 +147,6 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration

public static void BindCore(IConfiguration configuration, ref IReadOnlyDictionary<string, int> obj, BinderOptions? binderOptions)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}

if (obj is not IDictionary<string, int> temp)
{
return;
Expand All @@ -189,11 +163,6 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration

public static void BindCore(IConfiguration configuration, ref Program.MyClassWithCustomCollections obj, BinderOptions? binderOptions)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}

ValidateConfigurationKeys(typeof(Program.MyClassWithCustomCollections), s_configKeys_ProgramMyClassWithCustomCollections, configuration, binderOptions);

if (AsConfigWithChildren(configuration.GetSection("CustomDictionary")) is IConfigurationSection section1)
Expand Down Expand Up @@ -229,6 +198,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
}
}


/// <summary>If required by the binder options, validates that there are no unknown keys in the input configuration object.</summary>
public static void ValidateConfigurationKeys(Type type, Lazy<HashSet<string>> keys, IConfiguration configuration, BinderOptions? binderOptions)
{
Expand Down Expand Up @@ -281,7 +251,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration

if (binderOptions.BindNonPublicProperties)
{
throw new global::System.NotSupportedException($"The configuration binding source generator does not support 'BinderOptions.BindNonPublicProperties'.");
throw new NotSupportedException($"The configuration binding source generator does not support 'BinderOptions.BindNonPublicProperties'.");
}

return binderOptions;
Expand All @@ -298,5 +268,6 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
throw new InvalidOperationException($"Failed to convert configuration value at '{getPath()}' to type '{typeof(int)}'.", exception);
}
}
#endregion Core binding extensions.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
public static void Bind_ProgramMyClass(this IConfiguration configuration, object? obj)
{
ArgumentNullException.ThrowIfNull(configuration);

ArgumentNullException.ThrowIfNull(obj);

var typedObj = (Program.MyClass)obj;
Expand All @@ -45,15 +46,9 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
[InterceptsLocationAttribute(@"src-0.cs", 14, 24)]
public static void Bind_ProgramMyClass(this IConfiguration configuration, object? obj, Action<BinderOptions>? configureOptions)
{
if (configuration is null)
{
throw new ArgumentNullException(nameof(configuration));
}
ArgumentNullException.ThrowIfNull(configuration);

if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
ArgumentNullException.ThrowIfNull(obj);

var typedObj = (Program.MyClass)obj;
BindCore(configuration, ref typedObj, GetBinderOptions(configureOptions));
Expand All @@ -64,6 +59,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
public static void Bind_ProgramMyClass(this IConfiguration configuration, string key, object? obj)
{
ArgumentNullException.ThrowIfNull(configuration);

ArgumentNullException.ThrowIfNull(obj);

var typedObj = (Program.MyClass)obj;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
#nullable enable
#pragma warning disable CS0612, CS0618 // Suppress warnings about [Obsolete] member usage in generated code.

/// <summary>Generated helper providing an AOT and linking compatible implementation for configuration binding.</summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Configuration.Binder.SourceGeneration", "42.42.42.42")]
internal static class GeneratedConfigurationBinder
namespace System.Runtime.CompilerServices
{
/// <summary>Attempts to bind the given object instance to configuration values by matching property names against configuration keys recursively.</summary>
public static void Bind(this global::Microsoft.Extensions.Configuration.IConfiguration configuration, global::Program.MyClass obj) => global::Microsoft.Extensions.Configuration.Binder.SourceGeneration.CoreBindingHelper.BindCore(configuration, ref obj, binderOptions: null);

/// <summary>Attempts to bind the given object instance to configuration values by matching property names against configuration keys recursively.</summary>
public static void Bind(this global::Microsoft.Extensions.Configuration.IConfiguration configuration, global::Program.MyClass obj, global::System.Action<global::Microsoft.Extensions.Configuration.BinderOptions>? configureOptions) => global::Microsoft.Extensions.Configuration.Binder.SourceGeneration.CoreBindingHelper.BindCore(configuration, ref obj, global::Microsoft.Extensions.Configuration.Binder.SourceGeneration.CoreBindingHelper.GetBinderOptions(configureOptions));
using System;
using System.CodeDom.Compiler;

/// <summary>Attempts to bind the given object instance to configuration values by matching property names against configuration keys recursively.</summary>
public static void Bind(this global::Microsoft.Extensions.Configuration.IConfiguration configuration, string key, global::Program.MyClass obj) => global::Microsoft.Extensions.Configuration.Binder.SourceGeneration.CoreBindingHelper.BindCore(configuration.GetSection(key), ref obj, binderOptions: null);
[GeneratedCode("Microsoft.Extensions.Configuration.Binder.SourceGeneration", "42.42.42.42")]
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
file sealed class InterceptsLocationAttribute : Attribute
{
public InterceptsLocationAttribute(string filePath, int line, int column)
{
}
}
}

namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
Expand All @@ -23,20 +24,72 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.CompilerServices;

/// <summary>Provide core binding logic.</summary>
[GeneratedCode("Microsoft.Extensions.Configuration.Binder.SourceGeneration", "42.42.42.42")]
file static class CoreBindingHelper
file static class BindingExtensions
{
private readonly static Lazy<HashSet<string>> s_configKeys_ProgramMyClass = new(() => new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "MyString", "MyInt", "MyList", "MyDictionary", "MyComplexDictionary" });
#region IConfiguration extensions.
/// <summary>Attempts to bind the given object instance to configuration values by matching property names against configuration keys recursively.</summary>
[InterceptsLocationAttribute(@"src-0.cs", 13, 18)]
public static void Bind_ProgramMyClass(this IConfiguration configuration, object? obj)
{
if (configuration is null)
{
throw new ArgumentNullException(nameof(configuration));
}

public static void BindCore(IConfiguration configuration, ref List<int> obj, BinderOptions? binderOptions)
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}

var typedObj = (Program.MyClass)obj;
BindCore(configuration, ref typedObj, binderOptions: null);
}

/// <summary>Attempts to bind the given object instance to configuration values by matching property names against configuration keys recursively.</summary>
[InterceptsLocationAttribute(@"src-0.cs", 14, 24)]
public static void Bind_ProgramMyClass(this IConfiguration configuration, object? obj, Action<BinderOptions>? configureOptions)
{
if (configuration is null)
{
throw new ArgumentNullException(nameof(configuration));
}

if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}

var typedObj = (Program.MyClass)obj;
BindCore(configuration, ref typedObj, GetBinderOptions(configureOptions));
}

/// <summary>Attempts to bind the given object instance to configuration values by matching property names against configuration keys recursively.</summary>
[InterceptsLocationAttribute(@"src-0.cs", 15, 24)]
public static void Bind_ProgramMyClass(this IConfiguration configuration, string key, object? obj)
{
if (configuration is null)
{
throw new ArgumentNullException(nameof(configuration));
}

if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}

var typedObj = (Program.MyClass)obj;
BindCore(configuration.GetSection(key), ref typedObj, binderOptions: null);
}
#endregion IConfiguration extensions.

#region Core binding extensions.
private readonly static Lazy<HashSet<string>> s_configKeys_ProgramMyClass = new(() => new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "MyString", "MyInt", "MyList", "MyDictionary", "MyComplexDictionary" });

public static void BindCore(IConfiguration configuration, ref List<int> obj, BinderOptions? binderOptions)
{
foreach (IConfigurationSection section in configuration.GetChildren())
{
if (section.Value is string value)
Expand All @@ -48,11 +101,6 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration

public static void BindCore(IConfiguration configuration, ref Dictionary<string, string> obj, BinderOptions? binderOptions)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}

foreach (IConfigurationSection section in configuration.GetChildren())
{
if (section.Value is string value)
Expand All @@ -64,11 +112,6 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration

public static void BindCore(IConfiguration configuration, ref Dictionary<string, Program.MyClass2> obj, BinderOptions? binderOptions)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}

foreach (IConfigurationSection section in configuration.GetChildren())
{
if (!(obj.TryGetValue(section.Key, out Program.MyClass2? element) && element is not null))
Expand All @@ -81,11 +124,6 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration

public static void BindCore(IConfiguration configuration, ref Program.MyClass obj, BinderOptions? binderOptions)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}

ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions);

obj.MyString = configuration["MyString"]!;
Expand Down Expand Up @@ -120,6 +158,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
}
}


/// <summary>If required by the binder options, validates that there are no unknown keys in the input configuration object.</summary>
public static void ValidateConfigurationKeys(Type type, Lazy<HashSet<string>> keys, IConfiguration configuration, BinderOptions? binderOptions)
{
Expand Down Expand Up @@ -163,7 +202,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration

if (binderOptions.BindNonPublicProperties)
{
throw new global::System.NotSupportedException($"The configuration binding source generator does not support 'BinderOptions.BindNonPublicProperties'.");
throw new NotSupportedException($"The configuration binding source generator does not support 'BinderOptions.BindNonPublicProperties'.");
}

return binderOptions;
Expand All @@ -180,5 +219,6 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
throw new InvalidOperationException($"Failed to convert configuration value at '{getPath()}' to type '{typeof(int)}'.", exception);
}
}
#endregion Core binding extensions.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
public static void Bind_ProgramMyClass(this IConfiguration configuration, object? obj)
{
ArgumentNullException.ThrowIfNull(configuration);

ArgumentNullException.ThrowIfNull(obj);

var typedObj = (Program.MyClass)obj;
Expand Down
Loading

0 comments on commit e80c9ae

Please sign in to comment.