deployment/Deployment/ConfigurationBootstrapper.cs

162 lines
7.2 KiB
C#
Raw Normal View History

2024-10-24 16:05:50 -04:00
using System;
using System.Dynamic;
using System.Reflection;
using System.Runtime.CompilerServices;
using Newtonsoft.Json;
[assembly: InternalsVisibleTo("deployment.tests")]
namespace greyn.Deployment
{
public static class ConfigurationBootstrapper
{
private const string confpath = "appsettings.json";
public static T Load<T>() where T : new()
{
if (File.Exists("appsettings.json"))
{
/* "aaaahhh this is the most abstract and reflective thing in the goddamn world aaaahhhhh next you'll make an ECS on a dynamic aaaahhh"
* Ok, yes. I share these fears. but.
* this way we can read a *very* arbitrary configuration file.
* some random external file's configuration values is a good reason to aim for very high flexibility, imo
* if the configuration expects new values we write them in,
* if you left other junk for whatever reason, we don't delete them.
*/
var actualConfig = JsonConvert.DeserializeObject<ExpandoObject>(File.ReadAllText(confpath)) ?? new ExpandoObject();
var toReturn = new T();
2024-10-30 17:43:40 -04:00
PopulateExpando(toReturn, ref actualConfig);
2024-10-24 16:05:50 -04:00
File.WriteAllText(confpath, JsonConvert.SerializeObject(actualConfig, Formatting.Indented));
readFromExpando(ref toReturn, actualConfig);
return toReturn;
}
else
{
var toReturn = new T();
File.WriteAllText(confpath, JsonConvert.SerializeObject(toReturn));
return toReturn;
}
}
2024-10-30 16:46:57 -04:00
//TODO: make private but get tests to cooperate... I think that's a c# limitation
2024-10-30 17:43:40 -04:00
internal static void PopulateExpando<T>(T config, ref ExpandoObject fromFile)
2024-10-24 16:05:50 -04:00
{
if (config == null) return;
if (fromFile == null)
{
fromFile = new ExpandoObject();
2024-10-24 16:05:50 -04:00
}
var dictionaryFromExpandoFromFile = (IDictionary<string, object?>)fromFile;
foreach (var memberInfo in config.GetType().GetMembers())
{
if(memberInfo.DeclaringType == typeof(System.Object))
{
continue;
}
if(memberInfo.MemberType != MemberTypes.Field && memberInfo.MemberType != MemberTypes.Property)
{
continue;
}
2024-10-30 16:46:57 -04:00
if (dictionaryFromExpandoFromFile.TryGetValue(memberInfo.Name, out object? valueFromDict))
2024-10-24 16:05:50 -04:00
{
2024-10-30 16:46:57 -04:00
if (valueFromDict != null)
2024-10-24 16:05:50 -04:00
{
2024-10-30 16:46:57 -04:00
switch (memberInfo.MemberType)
2024-10-24 16:05:50 -04:00
{
2024-10-30 16:46:57 -04:00
case MemberTypes.Field:
var asField = (FieldInfo)memberInfo;
if (dictionaryFromExpandoFromFile[asField.Name] is ExpandoObject childMemberField)
2024-10-30 16:46:57 -04:00
{
2024-10-30 17:43:40 -04:00
PopulateExpando(asField.GetValue(config), ref childMemberField);
2024-10-30 16:46:57 -04:00
}
break;
case MemberTypes.Property:
var asProperty = (PropertyInfo)memberInfo;
if (dictionaryFromExpandoFromFile[asProperty.Name] is ExpandoObject childMemberProperty)
2024-10-30 16:46:57 -04:00
{
2024-10-30 17:43:40 -04:00
PopulateExpando(asProperty.GetValue(config), ref childMemberProperty);
2024-10-30 16:46:57 -04:00
}
break;
default:
break;
2024-10-24 16:05:50 -04:00
}
2024-10-30 16:46:57 -04:00
2024-10-24 16:05:50 -04:00
}
}
else
{
2024-10-30 16:46:57 -04:00
switch (memberInfo.MemberType)
{
case MemberTypes.Field:
fromFile.TryAdd(memberInfo.Name, ((FieldInfo)memberInfo).GetValue(config));
2024-10-30 16:46:57 -04:00
break;
case MemberTypes.Property:
fromFile.TryAdd(memberInfo.Name, ((PropertyInfo)memberInfo).GetValue(config));
2024-10-30 16:46:57 -04:00
break;
default:
break;
}
2024-10-24 16:05:50 -04:00
}
}
}
2024-10-30 16:46:57 -04:00
internal static void readFromExpando<T>(ref T config, ExpandoObject readFromFile)
2024-10-24 16:05:50 -04:00
{
//TODO: read from Expando
}
// private static T sync<T>(ref T config, ref ExpandoObject readFromFile)
// {
// var fieldsFoundThisLevel = new List<string>();
// if(config == null)
// {
// //TODO: it's entirely possible this is null.
// throw new ArgumentNullException();
// }
// if(readFromFile == null)
// {
// //TODO: it's entirely possible this is null.
// throw new ArgumentNullException();
// }
// var readUnExpandoed = (IDictionary<string, object?>)readFromFile;
// //if it's present on what was read
// //set it on Config
// //if it's not found on what was read, or type is different
// foreach (var prop in config.GetType().GetProperties())
// {
// fieldsFoundThisLevel.Add(prop.Name);
// if(readUnExpandoed.TryGetValue(prop.Name, out object? valueFromFile)) //so a configuration file can *set* a value *to null*
// {
// if(valueFromFile != null)
// {
// var defaultConfigValue = prop.GetValue(config);
// if (defaultConfigValue != null)
// {
// //expandofy, since we objectified it.
// var expandoFromFile = new ExpandoObject();
// var dictionaryFromExpandoFromFile = (IDictionary<string, object?>)expandoFromFile;
// foreach (var property in valueFromFile.GetType().GetProperties())
// dictionaryFromExpandoFromFile.Add(property.Name, property.GetValue(valueFromFile));
// expandoFromFile = (ExpandoObject)dictionaryFromExpandoFromFile;
// //dive in, repeat
// sync(ref defaultConfigValue, ref expandoFromFile);
// //we've now Sync'd expandoFromFile and defaultConfigValue
// prop.SetValue(readFromFile, expandoFromFile);
// prop.SetValue(config, expandoFromFile);
// }
// }
// }
// else
// {
// readFromFile.TryAdd(prop.Name, prop.GetValue(config));
// }
// }
// }
}
}