rename IChildSettings to ISettingsNode & update Settings.Get<T>()
This commit is contained in:
@@ -1,6 +0,0 @@
|
||||
namespace Pilz.Configuration;
|
||||
|
||||
public interface IChildSettings
|
||||
{
|
||||
void Reset();
|
||||
}
|
||||
@@ -5,8 +5,8 @@ namespace Pilz.Configuration;
|
||||
|
||||
public interface ISettings
|
||||
{
|
||||
IReadOnlyCollection<IChildSettings> Childs { get; }
|
||||
T Get<T>() where T : IChildSettings, ISettingsIdentifier;
|
||||
IReadOnlyCollection<ISettingsNode> Childs { get; }
|
||||
T Get<T>() where T : ISettingsNode, ISettingsIdentifier;
|
||||
void Reset();
|
||||
string Save(JsonSerializerSettings serializer);
|
||||
bool Load(JsonSerializerSettings serializer, string raw);
|
||||
|
||||
11
Pilz.Configuration/ISettingsNode.cs
Normal file
11
Pilz.Configuration/ISettingsNode.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using System;
|
||||
|
||||
namespace Pilz.Configuration;
|
||||
|
||||
public interface ISettingsNode
|
||||
{
|
||||
void Reset();
|
||||
}
|
||||
|
||||
[Obsolete("Use ISettingsNode instead!")]
|
||||
public interface IChildSettings : ISettingsNode;
|
||||
@@ -8,32 +8,48 @@ namespace Pilz.Configuration;
|
||||
public class Settings : ISettings
|
||||
{
|
||||
protected readonly Dictionary<string, JObject> settingsJson = [];
|
||||
protected readonly Dictionary<string, IChildSettings> settings = [];
|
||||
protected readonly Dictionary<string, ISettingsNode> settings = [];
|
||||
protected JsonSerializerSettings serializerSettings;
|
||||
|
||||
public virtual IReadOnlyCollection<IChildSettings> Childs => settings.Values;
|
||||
public virtual IReadOnlyCollection<ISettingsNode> Childs => settings.Values;
|
||||
|
||||
public virtual T Get<T>() where T : IChildSettings, ISettingsIdentifier
|
||||
public virtual T Get<T>() where T : ISettingsNode, ISettingsIdentifier
|
||||
{
|
||||
// Find existing one
|
||||
if (settings.TryGetValue(T.Identifier, out var valueExisting) && valueExisting is T settingsExisting)
|
||||
return settingsExisting;
|
||||
|
||||
if (settingsJson.TryGetValue(T.Identifier, out var valueRaw)
|
||||
&& valueRaw.ToObject<T>(JsonSerializer.CreateDefault(serializerSettings)) is T settingsDeserialized)
|
||||
{
|
||||
if (!settings.TryAdd(T.Identifier, settingsDeserialized))
|
||||
settings[T.Identifier] = settingsDeserialized;
|
||||
return settingsDeserialized;
|
||||
}
|
||||
|
||||
// Create new & reset
|
||||
T instance = default;
|
||||
if (Activator.CreateInstance<T>() is T settingsNew)
|
||||
{
|
||||
settingsNew.Reset();
|
||||
settings.Add(T.Identifier, settingsNew);
|
||||
return settingsNew;
|
||||
settings[T.Identifier] = settingsNew;
|
||||
instance = settingsNew;
|
||||
}
|
||||
else
|
||||
settingsNew = default;
|
||||
|
||||
// Try deserialize
|
||||
if (settingsJson.TryGetValue(T.Identifier, out var valueRaw))
|
||||
{
|
||||
var serializer = JsonSerializer.CreateDefault(serializerSettings);
|
||||
|
||||
// Populate
|
||||
if (settingsNew != null)
|
||||
serializer.Populate(valueRaw.CreateReader(), instance);
|
||||
|
||||
// Deserialize (fallback)
|
||||
else if (valueRaw.ToObject<T>() is T settingsDeserialized)
|
||||
settingsNew = settingsDeserialized;
|
||||
}
|
||||
|
||||
return default;
|
||||
// Remember
|
||||
if (settingsNew != null)
|
||||
settings[T.Identifier] = settingsNew;
|
||||
|
||||
// Return
|
||||
return settingsNew;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
|
||||
Reference in New Issue
Block a user