-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTinyValue.cs
140 lines (113 loc) · 5.13 KB
/
TinyValue.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using System.Globalization;
namespace Tiny
{
public enum TinyTokenType
{
Null,
Boolean,
Integer,
Float,
String,
Object,
Array,
Invalid,
}
public class TinyValue : TinyToken
{
private readonly object value;
private readonly TinyTokenType type;
public override bool IsInline => true;
public override bool IsEmpty => value == null;
public override TinyTokenType Type => type;
internal TinyValue(object value, TinyTokenType type)
{
this.value = value;
this.type = type;
switch (type)
{
case TinyTokenType.Object:
case TinyTokenType.Array:
case TinyTokenType.Invalid:
throw new ArgumentOutOfRangeException(nameof(type), type.ToString());
}
}
public TinyValue(object value) : this(value, FindValueType(value)) { }
public TinyValue(string value) : this(value, TinyTokenType.String) { }
public TinyValue(bool value) : this(value, TinyTokenType.Boolean) { }
public TinyValue(bool? value) : this(value, TinyTokenType.Boolean) { }
public TinyValue(sbyte value) : this(value, TinyTokenType.Integer) { }
public TinyValue(sbyte? value) : this(value, TinyTokenType.Integer) { }
public TinyValue(byte value) : this(value, TinyTokenType.Integer) { }
public TinyValue(byte? value) : this(value, TinyTokenType.Integer) { }
public TinyValue(short value) : this(value, TinyTokenType.Integer) { }
public TinyValue(short? value) : this(value, TinyTokenType.Integer) { }
public TinyValue(ushort value) : this(value, TinyTokenType.Integer) { }
public TinyValue(ushort? value) : this(value, TinyTokenType.Integer) { }
public TinyValue(int value) : this(value, TinyTokenType.Integer) { }
public TinyValue(int? value) : this(value, TinyTokenType.Integer) { }
public TinyValue(uint value) : this(value, TinyTokenType.Integer) { }
public TinyValue(uint? value) : this(value, TinyTokenType.Integer) { }
public TinyValue(long? value) : this(value, TinyTokenType.Integer) { }
public TinyValue(long value) : this(value, TinyTokenType.Integer) { }
public TinyValue(ulong value) : this(value, TinyTokenType.Integer) { }
public TinyValue(ulong? value) : this(value, TinyTokenType.Integer) { }
public TinyValue(float value) : this(value, TinyTokenType.Float) { }
public TinyValue(float? value) : this(value, TinyTokenType.Float) { }
public TinyValue(double value) : this(value, TinyTokenType.Float) { }
public TinyValue(double? value) : this(value, TinyTokenType.Float) { }
public TinyValue(decimal value) : this(value, TinyTokenType.Float) { }
public TinyValue(decimal? value) : this(value, TinyTokenType.Float) { }
public override T Value<T>(object key)
{
if (key != null)
throw new ArgumentException($"Key must be null, was {key}", "key");
if (value is T typedValue)
return typedValue;
var targetType = typeof(T);
if (targetType == typeof(object))
return (T)value;
if (targetType == typeof(TinyValue) || targetType == typeof(TinyToken))
return (T)(object)this;
if (type == TinyTokenType.Null)
{
if (targetType == typeof(TinyArray))
return (T)(object)new TinyArray();
else if (targetType == typeof(TinyObject))
return (T)(object)new TinyObject();
}
if (targetType.IsEnum && (type == TinyTokenType.String || type == TinyTokenType.Integer))
{
if (value == null)
return default;
return (T)Enum.Parse(targetType, value.ToString());
}
if (targetType.IsGenericType && targetType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
if (value == null)
return default;
targetType = Nullable.GetUnderlyingType(targetType);
}
return (T)Convert.ChangeType(value, targetType, CultureInfo.InvariantCulture);
}
public static TinyTokenType FindValueType(object value)
{
if (value == null)
return TinyTokenType.Null;
if (value is string)
return TinyTokenType.String;
if (value is sbyte || value is byte ||
value is short || value is ushort ||
value is int || value is uint ||
value is long || value is ulong ||
value is Enum)
return TinyTokenType.Integer;
if (value is float || value is double || value is decimal)
return TinyTokenType.Float;
if (value is bool)
return TinyTokenType.Boolean;
return TinyTokenType.Invalid;
}
public override string ToString() => $"{value} ({type})";
}
}