Working on this data setup logic
This commit is contained in:
parent
384cc3c6fd
commit
447b27f69b
16 changed files with 409 additions and 211 deletions
|
|
@ -2,7 +2,7 @@ using System.Data;
|
|||
using Insight.Database;
|
||||
|
||||
namespace Configuration.Core;
|
||||
public class ConfigurationRepository : IConfigurationRepository
|
||||
public class ConfigurationRepository : IConfigurationRepository
|
||||
{
|
||||
private readonly IDbConnection _connection;
|
||||
|
||||
|
|
@ -16,8 +16,8 @@ namespace Configuration.Core;
|
|||
const string sql = @"
|
||||
SELECT id, key, value, label, content_type, valid_from, expires_at, created_at, modified_at, etag
|
||||
FROM prod.app_configuration
|
||||
WHERE (expires_at IS NULL OR expires_at > CURRENT_TIMESTAMP)
|
||||
AND (valid_from IS NULL OR valid_from <= CURRENT_TIMESTAMP)";
|
||||
WHERE (expires_at IS NULL OR expires_at <= CURRENT_TIMESTAMP)
|
||||
AND (valid_from IS NULL OR valid_from >= CURRENT_TIMESTAMP)";
|
||||
|
||||
return await _connection.QueryAsync<AppConfiguration>(sql);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,56 +1,80 @@
|
|||
using Newtonsoft.Json.Linq;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using System.Data;
|
||||
|
||||
namespace Configuration.Core;
|
||||
public class JsonConfigurationSection : IConfigurationSection
|
||||
namespace Configuration.Core
|
||||
{
|
||||
private readonly JObject _data;
|
||||
private readonly string _path;
|
||||
|
||||
public JsonConfigurationSection(JObject data, string path)
|
||||
public class JsonConfigurationSection : IConfigurationSection
|
||||
{
|
||||
_data = data;
|
||||
_path = path;
|
||||
}
|
||||
private readonly JObject _data;
|
||||
private readonly string _path;
|
||||
private readonly string _normalizedPath;
|
||||
|
||||
public string this[string key]
|
||||
{
|
||||
get => _data.SelectToken($"{_path.Replace(":", ".")}.{key.Replace(":", ".")}")?.ToString();
|
||||
set => throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public string Key => _path.Split(':').Last();
|
||||
public string Path => _path;
|
||||
public string Value
|
||||
{
|
||||
get => _data.SelectToken(_path.Replace(":", "."))?.ToString();
|
||||
set => throw new NotImplementedException();
|
||||
}
|
||||
|
||||
|
||||
public IConfigurationSection GetSection(string key) =>
|
||||
new JsonConfigurationSection(_data, string.IsNullOrEmpty(_path) ? key : $"{_path}:{key}");
|
||||
|
||||
public JToken GetToken() => _data.SelectToken(_path.Replace(":", "."));
|
||||
|
||||
public IEnumerable<IConfigurationSection> GetChildren()
|
||||
{
|
||||
var token = _data.SelectToken(_path.Replace(":", "."));
|
||||
if (token is JObject obj)
|
||||
public JsonConfigurationSection(JObject data, string path)
|
||||
{
|
||||
return obj.Properties()
|
||||
.Select(p => new JsonConfigurationSection(_data,
|
||||
string.IsNullOrEmpty(_path) ? p.Name : $"{_path}:{p.Name}"));
|
||||
_data = data ?? throw new ArgumentNullException(nameof(data));
|
||||
_path = path ?? throw new ArgumentNullException(nameof(path));
|
||||
_normalizedPath = NormalizePath(_path);
|
||||
}
|
||||
return Enumerable.Empty<IConfigurationSection>();
|
||||
}
|
||||
public T Get<T>() where T : class
|
||||
{
|
||||
var token = _data.SelectToken(_path.Replace(":", "."));
|
||||
return token?.ToObject<T>();
|
||||
}
|
||||
|
||||
public IChangeToken GetReloadToken() => new ConfigurationReloadToken();
|
||||
public string this[string key]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (string.IsNullOrEmpty(key))
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
|
||||
var token = _data.SelectToken($"{_normalizedPath}.{NormalizePath(key)}");
|
||||
return token?.ToString();
|
||||
}
|
||||
set => throw new NotImplementedException("Setting values is not supported.");
|
||||
}
|
||||
|
||||
public string Key => _path.Split(':').Last();
|
||||
public string Path => _path;
|
||||
|
||||
public string Value
|
||||
{
|
||||
get
|
||||
{
|
||||
var token = _data.SelectToken(_normalizedPath);
|
||||
return token?.ToString();
|
||||
}
|
||||
set => throw new NotImplementedException("Setting values is not supported.");
|
||||
}
|
||||
|
||||
public IConfigurationSection GetSection(string key)
|
||||
{
|
||||
if (string.IsNullOrEmpty(key))
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
|
||||
return new JsonConfigurationSection(_data, string.IsNullOrEmpty(_path) ? key : $"{_path}:{key}");
|
||||
}
|
||||
|
||||
public JToken GetToken() => _data.SelectToken(_normalizedPath);
|
||||
|
||||
public IEnumerable<IConfigurationSection> GetChildren()
|
||||
{
|
||||
var token = _data.SelectToken(_normalizedPath);
|
||||
if (token is JObject obj)
|
||||
{
|
||||
return obj.Properties()
|
||||
.Select(p => new JsonConfigurationSection(_data, string.IsNullOrEmpty(_path) ? p.Name : $"{_path}:{p.Name}"));
|
||||
}
|
||||
return Enumerable.Empty<IConfigurationSection>();
|
||||
}
|
||||
|
||||
public T Get<T>() where T : class
|
||||
{
|
||||
var token = _data.SelectToken(_normalizedPath);
|
||||
return token?.ToObject<T>();
|
||||
}
|
||||
|
||||
public IChangeToken GetReloadToken() => new ConfigurationReloadToken();
|
||||
|
||||
private static string NormalizePath(string path)
|
||||
{
|
||||
return path?.Replace(":", ".", StringComparison.Ordinal) ?? string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -2,55 +2,102 @@ using Newtonsoft.Json;
|
|||
using Newtonsoft.Json.Linq;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Configuration.Core;
|
||||
public class KeyValueConfigurationBuilder
|
||||
namespace Configuration.Core
|
||||
{
|
||||
private readonly IConfigurationRepository _repository;
|
||||
private readonly JObject _rootObject = new();
|
||||
private ConfigurationReloadToken _reloadToken = new();
|
||||
private IConfiguration _configuration;
|
||||
|
||||
public KeyValueConfigurationBuilder(IConfigurationRepository repository)
|
||||
public class KeyValueConfigurationBuilder
|
||||
{
|
||||
_repository = repository;
|
||||
}
|
||||
private readonly IConfigurationRepository _repository;
|
||||
private readonly JObject _rootObject = new();
|
||||
private ConfigurationReloadToken _reloadToken = new();
|
||||
private IConfiguration _configuration;
|
||||
private readonly object _configurationLock = new();
|
||||
|
||||
public async Task LoadConfiguration()
|
||||
{
|
||||
var configurations = await _repository.GetActiveConfigurations();
|
||||
foreach (var config in configurations)
|
||||
public KeyValueConfigurationBuilder(IConfigurationRepository repository)
|
||||
{
|
||||
AddKeyValue(config.Key, config.Value);
|
||||
_repository = repository ?? throw new ArgumentNullException(nameof(repository));
|
||||
}
|
||||
OnReload();
|
||||
}
|
||||
|
||||
public void AddKeyValue(string key, string jsonValue)
|
||||
{
|
||||
var valueObject = JsonConvert.DeserializeObject<JObject>(jsonValue);
|
||||
var parts = key.Split(':');
|
||||
|
||||
JObject current = _rootObject;
|
||||
for (int i = 0; i < parts.Length - 1; i++)
|
||||
/// <summary>
|
||||
/// Loads configurations from the repository and builds the configuration tree.
|
||||
/// </summary>
|
||||
public async Task LoadConfiguration()
|
||||
{
|
||||
var part = parts[i];
|
||||
if (!current.ContainsKey(part))
|
||||
try
|
||||
{
|
||||
current[part] = new JObject();
|
||||
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);
|
||||
}
|
||||
current = (JObject)current[part];
|
||||
}
|
||||
|
||||
current[parts[^1]] = valueObject;
|
||||
}
|
||||
/// <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)
|
||||
{
|
||||
if (string.IsNullOrEmpty(key))
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
if (string.IsNullOrEmpty(jsonValue))
|
||||
throw new ArgumentNullException(nameof(jsonValue));
|
||||
|
||||
private void OnReload()
|
||||
{
|
||||
var previousToken = Interlocked.Exchange(ref _reloadToken, new ConfigurationReloadToken());
|
||||
previousToken.OnReload();
|
||||
_configuration = null;
|
||||
}
|
||||
try
|
||||
{
|
||||
var valueObject = JsonConvert.DeserializeObject<JObject>(jsonValue);
|
||||
var parts = key.Split(':');
|
||||
|
||||
public IConfiguration Build() =>
|
||||
_configuration ??= new JsonConfiguration(_rootObject, _reloadToken);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnReload()
|
||||
{
|
||||
var previousToken = Interlocked.Exchange(ref _reloadToken, new ConfigurationReloadToken());
|
||||
previousToken.OnReload();
|
||||
_configuration = null; // Reset the configuration to force a rebuild
|
||||
}
|
||||
|
||||
/// <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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue