File: Generated\Basic.CompilerLog.Util\Basic.CompilerLog.Util.Impl.BasicGeneratedFilesAnalyzerReference\BindingExtensions.g.cs
Project: ..\..\..\src\Libraries\Microsoft.AspNetCore.HeaderParsing\Microsoft.AspNetCore.HeaderParsing.csproj (Microsoft.AspNetCore.HeaderParsing)
// <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", "9.0.14.31522")]
    [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", "9.0.14.31522")]
    file static class BindingExtensions
    {
        #region OptionsBuilder<TOptions> extensions.
        /// <summary>Registers a configuration instance which <typeparamref name="TOptions"/> will bind against.</summary>
        [InterceptsLocation(1, "XP+3kfD7pUpHFYwBUL8HPAkMAABIZWFkZXJQYXJzaW5nU2VydmljZUNvbGxlY3Rpb25FeHRlbnNpb25zLmNz")] // /_/src/Libraries/Microsoft.AspNetCore.HeaderParsing/HeaderParsingServiceCollectionExtensions.cs(71,14)
        [InterceptsLocation(1, "XP+3kfD7pUpHFYwBUL8HPKcMAABIZWFkZXJQYXJzaW5nU2VydmljZUNvbGxlY3Rpb25FeHRlbnNpb25zLmNz")] // /_/src/Libraries/Microsoft.AspNetCore.HeaderParsing/HeaderParsingServiceCollectionExtensions.cs(75,14)
        public static OptionsBuilder<TOptions> Bind<TOptions>(this OptionsBuilder<TOptions> optionsBuilder, IConfiguration config) where TOptions : class
        {
            return Bind(optionsBuilder, config, configureBinder: null);
        }
 
        /// <summary>Registers a configuration instance which <typeparamref name="TOptions"/> will bind against.</summary>
        public static OptionsBuilder<TOptions> Bind<TOptions>(this OptionsBuilder<TOptions> optionsBuilder, IConfiguration config, Action<BinderOptions>? configureBinder) where TOptions : class
        {
            ArgumentNullException.ThrowIfNull(optionsBuilder);
 
            Configure<TOptions>(optionsBuilder.Services, optionsBuilder.Name, config, configureBinder);
            return optionsBuilder;
        }
        #endregion OptionsBuilder<TOptions> extensions.
 
        #region IServiceCollection extensions.
        /// <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_HeaderParsingOptions = new(() => new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "DefaultValues", "DefaultMaxCachedValuesPerHeader", "MaxCachedValuesPerHeader" });
 
        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.AspNetCore.HeaderParsing.HeaderParsingOptions))
            {
                var temp = (global::Microsoft.AspNetCore.HeaderParsing.HeaderParsingOptions)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::Microsoft.Extensions.Primitives.StringValues instance, bool defaultValueIfNotFound, BinderOptions? binderOptions)
        {
            ICollection<string> temp = instance;
 
            foreach (IConfigurationSection section in configuration.GetChildren())
            {
                if (section.Value is string value)
                {
                    temp.Add(value);
                }
            }
        }
 
        public static void BindCore(IConfiguration configuration, ref global::System.Collections.Generic.IDictionary<string, global::Microsoft.Extensions.Primitives.StringValues> instance, bool defaultValueIfNotFound, BinderOptions? binderOptions)
        {
            foreach (IConfigurationSection section in configuration.GetChildren())
            {
                if (!(instance.TryGetValue(section.Key, out global::Microsoft.Extensions.Primitives.StringValues element)))
                {
                    element = new global::Microsoft.Extensions.Primitives.StringValues();
                }
                var temp1 = new global::Microsoft.Extensions.Primitives.StringValues();
                BindCore(section, ref temp1, defaultValueIfNotFound: false, binderOptions);
                instance[section.Key] = element;
            }
        }
 
        public static void BindCore(IConfiguration configuration, ref global::System.Collections.Generic.IDictionary<string, int> instance, bool defaultValueIfNotFound, BinderOptions? binderOptions)
        {
            foreach (IConfigurationSection section in configuration.GetChildren())
            {
                if (section.Value is string value && !string.IsNullOrEmpty(value))
                {
                    instance[section.Key] = ParseInt(value, section.Path);
                }
            }
        }
 
        public static void BindCore(IConfiguration configuration, ref global::Microsoft.AspNetCore.HeaderParsing.HeaderParsingOptions instance, bool defaultValueIfNotFound, BinderOptions? binderOptions)
        {
            ValidateConfigurationKeys(typeof(global::Microsoft.AspNetCore.HeaderParsing.HeaderParsingOptions), s_configKeys_HeaderParsingOptions, configuration, binderOptions);
 
            if (AsConfigWithChildren(configuration.GetSection("DefaultValues")) is IConfigurationSection section2)
            {
                global::System.Collections.Generic.IDictionary<string, global::Microsoft.Extensions.Primitives.StringValues>? temp4 = instance.DefaultValues;
                temp4 ??= (global::System.Collections.Generic.IDictionary<string, global::Microsoft.Extensions.Primitives.StringValues>)new Dictionary<string, global::Microsoft.Extensions.Primitives.StringValues>();
                BindCore(section2, ref temp4, defaultValueIfNotFound: false, binderOptions);
                instance.DefaultValues = temp4;
            }
            else
            {
                instance.DefaultValues = instance.DefaultValues;
            }
 
            if (configuration["DefaultMaxCachedValuesPerHeader"] is string value5 && !string.IsNullOrEmpty(value5))
            {
                instance.DefaultMaxCachedValuesPerHeader = ParseInt(value5, configuration.GetSection("DefaultMaxCachedValuesPerHeader").Path);
            }
            else if (defaultValueIfNotFound)
            {
                instance.DefaultMaxCachedValuesPerHeader = instance.DefaultMaxCachedValuesPerHeader;
            }
 
            if (AsConfigWithChildren(configuration.GetSection("MaxCachedValuesPerHeader")) is IConfigurationSection section6)
            {
                global::System.Collections.Generic.IDictionary<string, int>? temp8 = instance.MaxCachedValuesPerHeader;
                temp8 ??= (global::System.Collections.Generic.IDictionary<string, int>)new Dictionary<string, int>();
                BindCore(section6, ref temp8, defaultValueIfNotFound: false, binderOptions);
                instance.MaxCachedValuesPerHeader = temp8;
            }
            else
            {
                instance.MaxCachedValuesPerHeader = instance.MaxCachedValuesPerHeader;
            }
        }
 
 
        /// <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 int ParseInt(string value, string? path)
        {
            try
            {
                return int.Parse(value, NumberStyles.Integer, CultureInfo.InvariantCulture);
            }
            catch (Exception exception)
            {
                throw new InvalidOperationException($"Failed to convert configuration value at '{path}' to type '{typeof(int)}'.", exception);
            }
        }
        #endregion Core binding extensions.
    }
}