2025-01-26 22:57:27 +01:00
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
|
2025-01-28 14:51:09 +01:00
|
|
|
namespace Configuration.Core
|
2025-01-26 22:57:27 +01:00
|
|
|
{
|
2025-01-28 14:51:09 +01:00
|
|
|
public class KeyValueConfigurationBuilder
|
2025-01-26 22:57:27 +01:00
|
|
|
{
|
2025-01-28 14:51:09 +01:00
|
|
|
private readonly IConfigurationRepository _repository;
|
|
|
|
|
private readonly JObject _rootObject = new();
|
|
|
|
|
private ConfigurationReloadToken _reloadToken = new();
|
|
|
|
|
private IConfiguration _configuration;
|
|
|
|
|
private readonly object _configurationLock = new();
|
2025-01-26 22:57:27 +01:00
|
|
|
|
2025-01-28 14:51:09 +01:00
|
|
|
public KeyValueConfigurationBuilder(IConfigurationRepository repository)
|
2025-01-26 22:57:27 +01:00
|
|
|
{
|
2025-01-28 14:51:09 +01:00
|
|
|
_repository = repository ?? throw new ArgumentNullException(nameof(repository));
|
2025-01-26 22:57:27 +01:00
|
|
|
}
|
|
|
|
|
|
2025-01-28 14:51:09 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Loads configurations from the repository and builds the configuration tree.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public async Task LoadConfiguration()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var configurations = await _repository.GetActiveConfigurations();
|
|
|
|
|
foreach (var config in configurations)
|
|
|
|
|
{
|
|
|
|
|
AddKeyValue(config.Key, config.Value);
|
|
|
|
|
}
|
|
|
|
|
OnReload();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
// Log the exception or handle it as needed
|
|
|
|
|
throw new InvalidOperationException("Failed to load configurations.", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-26 22:57:27 +01:00
|
|
|
|
2025-01-28 14:51:09 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Adds a key-value pair to the configuration tree.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="key">The key to add.</param>
|
|
|
|
|
/// <param name="jsonValue">The JSON value to add.</param>
|
|
|
|
|
public void AddKeyValue(string key, string jsonValue)
|
2025-01-26 22:57:27 +01:00
|
|
|
{
|
2025-01-28 14:51:09 +01:00
|
|
|
if (string.IsNullOrEmpty(key))
|
|
|
|
|
throw new ArgumentNullException(nameof(key));
|
|
|
|
|
if (string.IsNullOrEmpty(jsonValue))
|
|
|
|
|
throw new ArgumentNullException(nameof(jsonValue));
|
|
|
|
|
|
|
|
|
|
try
|
2025-01-26 22:57:27 +01:00
|
|
|
{
|
2025-01-28 14:51:09 +01:00
|
|
|
var valueObject = JsonConvert.DeserializeObject<JObject>(jsonValue);
|
|
|
|
|
var parts = key.Split(':');
|
|
|
|
|
|
|
|
|
|
JObject current = _rootObject;
|
|
|
|
|
for (int i = 0; i < parts.Length - 1; i++)
|
|
|
|
|
{
|
|
|
|
|
var part = parts[i];
|
|
|
|
|
if (!current.ContainsKey(part))
|
|
|
|
|
{
|
|
|
|
|
current[part] = new JObject();
|
|
|
|
|
}
|
|
|
|
|
current = (JObject)current[part];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
current[parts[^1]] = valueObject;
|
|
|
|
|
}
|
|
|
|
|
catch (JsonException ex)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Invalid JSON value.", nameof(jsonValue), ex);
|
2025-01-26 22:57:27 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-28 14:51:09 +01:00
|
|
|
private void OnReload()
|
|
|
|
|
{
|
|
|
|
|
var previousToken = Interlocked.Exchange(ref _reloadToken, new ConfigurationReloadToken());
|
|
|
|
|
previousToken.OnReload();
|
|
|
|
|
_configuration = null; // Reset the configuration to force a rebuild
|
|
|
|
|
}
|
2025-01-26 22:57:27 +01:00
|
|
|
|
2025-01-28 14:51:09 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Builds the configuration instance.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>The built <see cref="IConfiguration"/> instance.</returns>
|
|
|
|
|
public IConfiguration Build()
|
|
|
|
|
{
|
|
|
|
|
if (_configuration == null)
|
|
|
|
|
{
|
|
|
|
|
lock (_configurationLock)
|
|
|
|
|
{
|
|
|
|
|
if (_configuration == null)
|
|
|
|
|
{
|
|
|
|
|
_configuration = new JsonConfiguration(_rootObject, _reloadToken);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return _configuration;
|
|
|
|
|
}
|
2025-01-26 22:57:27 +01:00
|
|
|
}
|
|
|
|
|
}
|