2024-02-03 17:30:01 -07:00

82 lines
3.3 KiB
C#

using System.Text.Json;
using System.Text.Json.Serialization;
namespace Parsing_Packets.Models.Binder;
public class AppSettings
{
public string? BuildNumber { get; set; }
public string? Company { get; set; }
public string? GitCommitSeven { get; set; }
public string? Helper { get; set; }
public int? MillisecondsDelay { get; set; }
public override string ToString()
{
string result = JsonSerializer.Serialize(this, BinderAppSettingsSourceGenerationContext.Default.AppSettings);
return result;
}
private static void PreVerify(IConfigurationRoot configurationRoot, AppSettings? appSettings)
{
if (appSettings?.BuildNumber is null)
{
foreach (IConfigurationProvider configurationProvider in configurationRoot.Providers)
{
if (configurationProvider is not Microsoft.Extensions.Configuration.Json.JsonConfigurationProvider jsonConfigurationProvider)
continue;
if (jsonConfigurationProvider.Source.FileProvider is not Microsoft.Extensions.FileProviders.PhysicalFileProvider physicalFileProvider)
continue;
if (!physicalFileProvider.Root.Contains("UserSecrets"))
continue;
throw new NotSupportedException(physicalFileProvider.Root);
}
throw new NotSupportedException("Not Found!");
}
}
private static void Verify(AppSettings _)
{
}
private static Models.AppSettings Get(AppSettings? appSettings,
Models.PhysicalAddressConfiguration physicalAddressConfiguration)
{
Models.AppSettings result;
if (appSettings is null) throw new NullReferenceException(nameof(appSettings));
if (appSettings.BuildNumber is null) throw new NullReferenceException(nameof(BuildNumber));
if (appSettings.Company is null) throw new NullReferenceException(nameof(Company));
if (appSettings.GitCommitSeven is null) throw new NullReferenceException(nameof(GitCommitSeven));
if (appSettings.Helper is null) throw new NullReferenceException(nameof(Helper));
if (appSettings.MillisecondsDelay is null) throw new NullReferenceException(nameof(MillisecondsDelay));
Verify(appSettings);
result = new(physicalAddressConfiguration,
appSettings.BuildNumber,
appSettings.Company,
appSettings.GitCommitSeven,
appSettings.Helper,
appSettings.MillisecondsDelay.Value);
return result;
}
public static Models.AppSettings Get(IConfigurationRoot configurationRoot,
Models.PhysicalAddressConfiguration physicalAddressConfiguration)
{
Models.AppSettings result;
#pragma warning disable IL3050, IL2026
AppSettings? appSettings = configurationRoot.Get<AppSettings>();
#pragma warning restore IL3050, IL2026
PreVerify(configurationRoot, appSettings);
result = Get(appSettings,
physicalAddressConfiguration);
return result;
}
}
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(AppSettings))]
internal partial class BinderAppSettingsSourceGenerationContext : JsonSerializerContext
{
}