-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTinyObject.cs
112 lines (89 loc) · 3.87 KB
/
TinyObject.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace Tiny
{
public class TinyObject : TinyToken, IEnumerable<KeyValuePair<string, TinyToken>>
{
private readonly KeyValuePairCollection properties = new KeyValuePairCollection();
public override bool IsInline => false;
public override bool IsEmpty => properties.Count == 0;
public override TinyTokenType Type => TinyTokenType.Object;
public TinyToken this[string key]
{
get => properties[key].Value;
set => properties.Set(key, value);
}
public int Count => properties.Count;
public void Add(string key, object value) => properties.Add(key, ToToken(value));
public void Add(string key, TinyToken value) => properties.Add(key, value);
public void Add(KeyValuePair<string, TinyToken> item) => properties.Add(item.Key, item.Value);
public IEnumerator<KeyValuePair<string, TinyToken>> GetEnumerator() => properties.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => properties.GetEnumerator();
public override T Value<T>(object key)
{
if (key == null)
return (T)(object)this;
if (key is string k)
{
if (properties.TryGetValue(k, out var property))
return property.Value.Value<T>();
else return default;
}
else if (key is int index)
return properties[index].Value.Value<T>();
throw new ArgumentException($"Key must be an integer or a string, was {key}", "key");
}
public override string ToString() => string.Join(", ", properties);
private class KeyValuePairCollection : Collection<KeyValuePair<string, TinyToken>>
{
private readonly Dictionary<string, KeyValuePair<string, TinyToken>> pairs = new Dictionary<string, KeyValuePair<string, TinyToken>>();
public KeyValuePair<string, TinyToken> this[string key] => pairs[key];
public void Add(string key, TinyToken value)
=> Add(new KeyValuePair<string, TinyToken>(key, value));
public void Set(string key, TinyToken value)
=> Set(new KeyValuePair<string, TinyToken>(key, value));
public void Set(KeyValuePair<string, TinyToken> value)
{
if (pairs.TryGetValue(value.Key, out var existingValue))
this[Items.IndexOf(value)] = value;
else Add(value);
}
public bool TryGetValue(string key, out KeyValuePair<string, TinyToken> value)
=> pairs.TryGetValue(key, out value);
#region Collection<T>
protected override void ClearItems()
{
pairs.Clear();
base.ClearItems();
}
protected override void InsertItem(int index, KeyValuePair<string, TinyToken> item)
{
pairs[item.Key] = item;
base.InsertItem(index, item);
}
protected override void RemoveItem(int index)
{
var indexKey = Items[index].Key;
pairs.Remove(indexKey);
base.RemoveItem(index);
}
protected override void SetItem(int index, KeyValuePair<string, TinyToken> item)
{
var itemKey = item.Key;
var indexKey = Items[index].Key;
if (indexKey == itemKey)
pairs[itemKey] = item;
else
{
pairs[itemKey] = item;
if (indexKey != null)
pairs.Remove(indexKey);
}
base.SetItem(index, item);
}
#endregion
}
}
}