qamp/Qrakhen.Qamp.Core/Values/Objects/Structure.cs

37 lines
1.1 KiB
C#

namespace Qrakhen.Qamp.Core.Values.Objects;
public class Structure(IEnumerable<KeyValuePair<string, Value>> data) : ItemProvider<string>(ValueType.Structure)
{
public bool Sealed;
public Dictionary<string, Value> Data = new(data);
protected override void InnerSet(string index, Value value)
{
Data[index] = value;
}
protected override Value InnerGet(string index)
{
return Data[index];
}
protected override void AssertValidAccesor(string index)
{
if (Sealed && !Data.ContainsKey(index))
throw new ItemProviderException($"Can not set non-existent key '{index}' of sealed structure.", this);
}
protected override string ExtractIndex(Value value)
{
string index;
if (value.IsString)
index = value.Ptr.As<String>()!.Value!;
else
throw new ItemProviderException($"Can not use {value} as an accessor, as it is not a string.", this);
return index;
}
public override string ToString() => ToString(true);
public string ToString(bool detail) => detail ? $"{{{string.Join("\n,", Data)}}}" : $"Structure{{{Data.Count}}}";
}