File: Generated\Basic.CompilerLog.Util\Basic.CompilerLog.Util.Impl.BasicGeneratedFilesAnalyzerReference\BindingExtensions.g.cs
Project: ..\..\..\src\Libraries\Microsoft.Extensions.Diagnostics.Testing\Microsoft.Extensions.Diagnostics.Testing.csproj (Microsoft.Extensions.Diagnostics.Testing)
// <auto-generated/>
 
#nullable enable annotations
#nullable disable warnings
 
// Suppress warnings about [Obsolete] member usage in generated code.
#pragma warning disable CS0612, CS0618
 
namespace System.Runtime.CompilerServices
{
    using System;
    using System.CodeDom.Compiler;
 
    [GeneratedCode("Microsoft.Extensions.Configuration.Binder.SourceGeneration", "10.0.14.32716")]
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
    file sealed class InterceptsLocationAttribute : Attribute
    {
        public InterceptsLocationAttribute(int version, string data)
        {
        }
    }
}
 
namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
{
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Options;
    using System;
    using System.CodeDom.Compiler;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Runtime.CompilerServices;
 
    [GeneratedCode("Microsoft.Extensions.Configuration.Binder.SourceGeneration", "10.0.14.32716")]
    file static class BindingExtensions
    {
        #region IServiceCollection extensions.
        /// <summary>Registers a configuration instance which TOptions will bind against.</summary>
        [InterceptsLocation(1, "74ahqZGP0nBQ7H3fqDNO/u0EAABGYWtlTG9nZ2VyQnVpbGRlckV4dGVuc2lvbnMuY3M=")] // /_/src/Libraries/Microsoft.Extensions.Diagnostics.Testing/Logging/FakeLoggerBuilderExtensions.cs(31,30)
        public static IServiceCollection Configure<TOptions>(this IServiceCollection services, IConfiguration config) where TOptions : class
        {
            return Configure<TOptions>(services, string.Empty, config, configureOptions: null);
        }
 
        /// <summary>Registers a configuration instance which TOptions will bind against.</summary>
        public static IServiceCollection Configure<TOptions>(this IServiceCollection services, string? name, IConfiguration config, Action<BinderOptions>? configureOptions) where TOptions : class
        {
            ArgumentNullException.ThrowIfNull(services);
 
            ArgumentNullException.ThrowIfNull(config);
 
            OptionsServiceCollectionExtensions.AddOptions(services);
            services.AddSingleton<IOptionsChangeTokenSource<TOptions>>(new ConfigurationChangeTokenSource<TOptions>(name, config));
            return services.AddSingleton<IConfigureOptions<TOptions>>(new ConfigureNamedOptions<TOptions>(name, instance => BindCoreMain(config, instance, typeof(TOptions), configureOptions)));
        }
        #endregion IServiceCollection extensions.
 
        #region Core binding extensions.
        private readonly static Lazy<HashSet<string>> s_configKeys_TimeZoneInfo = new(() => new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "BaseUtcOffset", "DaylightName", "DisplayName", "HasIanaId", "Id", "Local", "StandardName", "SupportsDaylightSavingTime", "Utc" });
        private readonly static Lazy<HashSet<string>> s_configKeys_TimeProvider = new(() => new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "LocalTimeZone", "System", "TimestampFrequency" });
        private readonly static Lazy<HashSet<string>> s_configKeys_FakeLogCollectorOptions = new(() => new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "FilteredCategories", "FilteredLevels", "CollectRecordsForDisabledLogLevels", "TimeProvider" });
 
        public static void BindCoreMain(IConfiguration configuration, object instance, Type type, Action<BinderOptions>? configureOptions)
        {
            if (instance is null)
            {
                return;
            }
 
            if (!HasValueOrChildren(configuration))
            {
                return;
            }
 
            BinderOptions? binderOptions = GetBinderOptions(configureOptions);
 
            if (type == typeof(global::Microsoft.Extensions.Logging.Testing.FakeLogCollectorOptions))
            {
                var temp = (global::Microsoft.Extensions.Logging.Testing.FakeLogCollectorOptions)instance;
                BindCore(configuration, ref temp, defaultValueIfNotFound: false, binderOptions);
                return;
            }
 
            throw new NotSupportedException($"Unable to bind to type '{type}': generator did not detect the type as input.");
        }
 
        public static void BindCore(IConfiguration configuration, ref global::System.Collections.Generic.ISet<string> instance, bool defaultValueIfNotFound, BinderOptions? binderOptions)
        {
            foreach (IConfigurationSection section in configuration.GetChildren())
            {
                if (TryGetConfigurationValue(section, key: null, out string? value))
                {
                    instance.Add(value);
                }
            }
        }
 
        public static void BindCore(IConfiguration configuration, ref global::System.Collections.Generic.ISet<global::Microsoft.Extensions.Logging.LogLevel> instance, bool defaultValueIfNotFound, BinderOptions? binderOptions)
        {
            foreach (IConfigurationSection section in configuration.GetChildren())
            {
                if (TryGetConfigurationValue(section, key: null, out string? value))
                {
                    if (!string.IsNullOrEmpty(value))
                    {
                        instance.Add(ParseEnum<global::Microsoft.Extensions.Logging.LogLevel>(value, section.Path));
                    }
                }
            }
        }
 
        public static void BindCore(IConfiguration configuration, ref global::System.TimeZoneInfo instance, bool defaultValueIfNotFound, BinderOptions? binderOptions)
        {
            ValidateConfigurationKeys(typeof(global::System.TimeZoneInfo), s_configKeys_TimeZoneInfo, configuration, binderOptions);
 
            var value1 = configuration.GetSection("Local");
            if (AsConfigWithChildren(value1) is IConfigurationSection section2)
            {
                global::System.TimeZoneInfo? temp4 = global::System.TimeZoneInfo.Local;
                if (temp4 is not null)
                {
                    BindCore(section2, ref temp4, defaultValueIfNotFound: false, binderOptions);
                }
                else
                {
                    throw new InvalidOperationException("Cannot create instance of type 'System.TimeZoneInfo' because it is missing a public instance constructor.");
                }
            }
 
            var value5 = configuration.GetSection("Utc");
            if (AsConfigWithChildren(value5) is IConfigurationSection section6)
            {
                global::System.TimeZoneInfo? temp8 = global::System.TimeZoneInfo.Utc;
                if (temp8 is not null)
                {
                    BindCore(section6, ref temp8, defaultValueIfNotFound: false, binderOptions);
                }
                else
                {
                    throw new InvalidOperationException("Cannot create instance of type 'System.TimeZoneInfo' because it is missing a public instance constructor.");
                }
            }
        }
 
        public static void BindCore(IConfiguration configuration, ref global::System.TimeProvider instance, bool defaultValueIfNotFound, BinderOptions? binderOptions)
        {
            ValidateConfigurationKeys(typeof(global::System.TimeProvider), s_configKeys_TimeProvider, configuration, binderOptions);
 
            var value9 = configuration.GetSection("LocalTimeZone");
            if (AsConfigWithChildren(value9) is IConfigurationSection section10)
            {
                global::System.TimeZoneInfo? temp12 = instance.LocalTimeZone;
                if (temp12 is not null)
                {
                    BindCore(section10, ref temp12, defaultValueIfNotFound: false, binderOptions);
                }
                else
                {
                    throw new InvalidOperationException("Cannot create instance of type 'System.TimeZoneInfo' because it is missing a public instance constructor.");
                }
            }
 
            var value13 = configuration.GetSection("System");
            if (AsConfigWithChildren(value13) is IConfigurationSection section14)
            {
                global::System.TimeProvider? temp16 = global::System.TimeProvider.System;
                if (temp16 is not null)
                {
                    BindCore(section14, ref temp16, defaultValueIfNotFound: false, binderOptions);
                }
                else
                {
                    throw new InvalidOperationException("Cannot create instance of type 'System.TimeProvider' because it is missing a public instance constructor.");
                }
            }
        }
 
        public static void BindCore(IConfiguration configuration, ref global::Microsoft.Extensions.Logging.Testing.FakeLogCollectorOptions instance, bool defaultValueIfNotFound, BinderOptions? binderOptions)
        {
            ValidateConfigurationKeys(typeof(global::Microsoft.Extensions.Logging.Testing.FakeLogCollectorOptions), s_configKeys_FakeLogCollectorOptions, configuration, binderOptions);
 
            var value17 = configuration.GetSection("FilteredCategories");
            if (AsConfigWithChildren(value17) is IConfigurationSection section18)
            {
                global::System.Collections.Generic.ISet<string>? temp20 = instance.FilteredCategories;
                temp20 ??= (global::System.Collections.Generic.ISet<string>)new HashSet<string>();
                BindCore(section18, ref temp20, defaultValueIfNotFound: false, binderOptions);
                instance.FilteredCategories = temp20;
            }
            else
            {
                instance.FilteredCategories = instance.FilteredCategories;
            }
 
            var value21 = configuration.GetSection("FilteredLevels");
            if (AsConfigWithChildren(value21) is IConfigurationSection section22)
            {
                global::System.Collections.Generic.ISet<global::Microsoft.Extensions.Logging.LogLevel>? temp24 = instance.FilteredLevels;
                temp24 ??= (global::System.Collections.Generic.ISet<global::Microsoft.Extensions.Logging.LogLevel>)new HashSet<global::Microsoft.Extensions.Logging.LogLevel>();
                BindCore(section22, ref temp24, defaultValueIfNotFound: false, binderOptions);
                instance.FilteredLevels = temp24;
            }
            else
            {
                instance.FilteredLevels = instance.FilteredLevels;
            }
 
            if (TryGetConfigurationValue(configuration, key: "CollectRecordsForDisabledLogLevels", out string? value25))
            {
                if (!string.IsNullOrEmpty(value25))
                {
                    instance.CollectRecordsForDisabledLogLevels = ParseBool(value25, configuration.GetSection("CollectRecordsForDisabledLogLevels").Path);
                }
            }
            else if (defaultValueIfNotFound)
            {
                instance.CollectRecordsForDisabledLogLevels = instance.CollectRecordsForDisabledLogLevels;
            }
 
            var value26 = configuration.GetSection("TimeProvider");
            if (AsConfigWithChildren(value26) is IConfigurationSection section27)
            {
                global::System.TimeProvider? temp29 = instance.TimeProvider;
                if (temp29 is not null)
                {
                    BindCore(section27, ref temp29, defaultValueIfNotFound: false, binderOptions);
                    instance.TimeProvider = temp29;
                }
                else
                {
                    throw new InvalidOperationException("Cannot create instance of type 'System.TimeProvider' because it is missing a public instance constructor.");
                }
            }
        }
 
        /// <summary>Tries to get the configuration value for the specified key.</summary>
        public static bool TryGetConfigurationValue(IConfiguration configuration, string key, out string? value)
        {
            if (configuration is ConfigurationSection section)
            {
                return section.TryGetValue(key, out value);
            }
        
            value = key != null ? configuration[key] : configuration is IConfigurationSection sec ? sec.Value : null;
            return value != null;
        }
 
 
        /// <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)
        {
            if (binderOptions?.ErrorOnUnknownConfiguration is true)
            {
                List<string>? temp = null;
        
                foreach (IConfigurationSection section in configuration.GetChildren())
                {
                    if (!keys.Value.Contains(section.Key))
                    {
                        (temp ??= new List<string>()).Add($"'{section.Key}'");
                    }
                }
        
                if (temp is not null)
                {
                    throw new InvalidOperationException($"'ErrorOnUnknownConfiguration' was set on the provided BinderOptions, but the following properties were not found on the instance of {type}: {string.Join(", ", temp)}");
                }
            }
        }
 
        public static bool HasValueOrChildren(IConfiguration configuration)
        {
            if ((configuration as IConfigurationSection)?.Value is not null)
            {
                return true;
            }
            return AsConfigWithChildren(configuration) is not null;
        }
 
        public static IConfiguration? AsConfigWithChildren(IConfiguration configuration)
        {
            foreach (IConfigurationSection _ in configuration.GetChildren())
            {
                return configuration;
            }
            return null;
        }
 
        public static BinderOptions? GetBinderOptions(Action<BinderOptions>? configureOptions)
        {
            if (configureOptions is null)
            {
                return null;
            }
        
            BinderOptions binderOptions = new();
            configureOptions(binderOptions);
        
            if (binderOptions.BindNonPublicProperties)
            {
                throw new NotSupportedException($"The configuration binding source generator does not support 'BinderOptions.BindNonPublicProperties'.");
            }
        
            return binderOptions;
        }
 
        public static T ParseEnum<T>(string value, string? path) where T : struct
        {
            try
            {
                return Enum.Parse<T>(value, ignoreCase: true);
            }
            catch (Exception exception)
            {
                throw new InvalidOperationException($"Failed to convert configuration value '{value ?? "null"}' at '{path}' to type '{typeof(T)}'.", exception);
            }
        }
 
        public static bool ParseBool(string value, string? path)
        {
            try
            {
                return bool.Parse(value);
            }
            catch (Exception exception)
            {
                throw new InvalidOperationException($"Failed to convert configuration value '{value ?? "null"}' at '{path}' to type '{typeof(bool)}'.", exception);
            }
        }
 
        public static global::System.TimeSpan ParseSystemTimeSpan(string value, string? path)
        {
            try
            {
                return global::System.TimeSpan.Parse(value, CultureInfo.InvariantCulture);
            }
            catch (Exception exception)
            {
                throw new InvalidOperationException($"Failed to convert configuration value '{value ?? "null"}' at '{path}' to type '{typeof(global::System.TimeSpan)}'.", exception);
            }
        }
 
        public static long ParseLong(string value, string? path)
        {
            try
            {
                return long.Parse(value, NumberStyles.Integer, CultureInfo.InvariantCulture);
            }
            catch (Exception exception)
            {
                throw new InvalidOperationException($"Failed to convert configuration value '{value ?? "null"}' at '{path}' to type '{typeof(long)}'.", exception);
            }
        }
        #endregion Core binding extensions.
    }
}