1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-04-20 00:43:36 +03:00
Octobot/src/Data/Options/Option.cs
Octol1ttle fe4793f5f9
AuTo-pRoPeRtY CaN Be MaDe gEt-oNlY
Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
2023-07-11 00:41:51 +05:00

44 lines
1.5 KiB
C#

using System.Text.Json.Nodes;
using Remora.Results;
namespace Boyfriend.Data.Options;
/// <summary>
/// Represents an per-guild option.
/// </summary>
/// <typeparam name="T">The type of the option.</typeparam>
public class Option<T> : IOption {
internal readonly T DefaultValue;
public Option(string name, T defaultValue) {
Name = name;
DefaultValue = defaultValue;
}
public string Name { get; }
public object GetAsObject(JsonNode settings) {
return Get(settings)!;
}
/// <summary>
/// Sets the value of the option from a <see cref="string" /> to the provided JsonNode.
/// </summary>
/// <param name="settings">The <see cref="JsonNode" /> to set the value to.</param>
/// <param name="from">The string from which the new value of the option will be parsed.</param>
/// <returns>A value setting result which may or may not have succeeded.</returns>
public virtual Result Set(JsonNode settings, string from) {
settings[Name] = from;
return Result.FromSuccess();
}
/// <summary>
/// Gets the value of the option from the provided <paramref name="settings" />.
/// </summary>
/// <param name="settings">The <see cref="JsonNode" /> to get the value from.</param>
/// <returns>The value of the option.</returns>
public virtual T Get(JsonNode settings) {
var property = settings[Name];
return property != null ? property.GetValue<T>() : DefaultValue;
}
}