Adds Configuration Manager + tests
This commit is contained in:
parent
55e65a1b21
commit
384cc3c6fd
16 changed files with 657 additions and 137 deletions
|
|
@ -0,0 +1,56 @@
|
|||
using Newtonsoft.Json.Linq;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using System.Data;
|
||||
|
||||
namespace Configuration.Core;
|
||||
public class JsonConfigurationSection : IConfigurationSection
|
||||
{
|
||||
private readonly JObject _data;
|
||||
private readonly string _path;
|
||||
|
||||
public JsonConfigurationSection(JObject data, string path)
|
||||
{
|
||||
_data = data;
|
||||
_path = path;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
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(_path.Replace(":", "."));
|
||||
return token?.ToObject<T>();
|
||||
}
|
||||
|
||||
public IChangeToken GetReloadToken() => new ConfigurationReloadToken();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue