-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
DynamicData.Operators.cs
389 lines (364 loc) · 15.1 KB
/
DynamicData.Operators.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Azure.Core.Json;
namespace Azure.Core.Serialization
{
public partial class DynamicData
{
// Operators that cast from DynamicData to another type
private static readonly Dictionary<Type, MethodInfo> CastFromOperators = typeof(DynamicData)
.GetMethods(BindingFlags.Public | BindingFlags.Static)
.Where(method => method.Name == "op_Explicit" || method.Name == "op_Implicit")
.ToDictionary(method => method.ReturnType);
/// <summary>
/// Converts the value to a <see cref="bool"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
public static implicit operator bool(DynamicData value)
{
try
{
return value._element.GetBoolean();
}
catch (InvalidOperationException e)
{
throw new InvalidCastException(GetInvalidKindExceptionText(typeof(bool), value._element), e);
}
}
/// <summary>
/// Converts the value to a <see cref="string"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
public static implicit operator string?(DynamicData value)
{
try
{
return value._element.GetString();
}
catch (InvalidOperationException e)
{
throw new InvalidCastException(GetInvalidKindExceptionText(typeof(string), value._element), e);
}
}
/// <summary>
/// Converts the value to a <see cref="byte"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
public static implicit operator byte(DynamicData value)
{
try
{
return value._element.GetByte();
}
catch (InvalidOperationException e)
{
throw new InvalidCastException(GetInvalidKindExceptionText(typeof(byte), value._element), e);
}
catch (FormatException formatException)
{
throw new InvalidCastException(GetInvalidFormatExceptionText(typeof(byte), value._element), formatException);
}
}
/// <summary>
/// Converts the value to a <see cref="sbyte"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
public static implicit operator sbyte(DynamicData value)
{
try
{
return value._element.GetSByte();
}
catch (InvalidOperationException e)
{
throw new InvalidCastException(GetInvalidKindExceptionText(typeof(sbyte), value._element), e);
}
catch (FormatException formatException)
{
throw new InvalidCastException(GetInvalidFormatExceptionText(typeof(sbyte), value._element), formatException);
}
}
/// <summary>
/// Converts the value to a <see cref="short"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
public static implicit operator short(DynamicData value)
{
try
{
return value._element.GetInt16();
}
catch (InvalidOperationException e)
{
throw new InvalidCastException(GetInvalidKindExceptionText(typeof(short), value._element), e);
}
catch (FormatException formatException)
{
throw new InvalidCastException(GetInvalidFormatExceptionText(typeof(short), value._element), formatException);
}
}
/// <summary>
/// Converts the value to a <see cref="ushort"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
public static implicit operator ushort(DynamicData value)
{
try
{
return value._element.GetUInt16();
}
catch (InvalidOperationException e)
{
throw new InvalidCastException(GetInvalidKindExceptionText(typeof(ushort), value._element), e);
}
catch (FormatException formatException)
{
throw new InvalidCastException(GetInvalidFormatExceptionText(typeof(ushort), value._element), formatException);
}
}
/// <summary>
/// Converts the value to a <see cref="int"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
public static implicit operator int(DynamicData value)
{
try
{
return value._element.GetInt32();
}
catch (InvalidOperationException e)
{
throw new InvalidCastException(GetInvalidKindExceptionText(typeof(int), value._element), e);
}
catch (FormatException formatException)
{
throw new InvalidCastException(GetInvalidFormatExceptionText(typeof(int), value._element), formatException);
}
}
/// <summary>
/// Converts the value to a <see cref="uint"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
public static implicit operator uint(DynamicData value)
{
try
{
return value._element.GetUInt32();
}
catch (InvalidOperationException e)
{
throw new InvalidCastException(GetInvalidKindExceptionText(typeof(uint), value._element), e);
}
catch (FormatException formatException)
{
throw new InvalidCastException(GetInvalidFormatExceptionText(typeof(uint), value._element), formatException);
}
}
/// <summary>
/// Converts the value to a <see cref="long"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
public static implicit operator long(DynamicData value)
{
try
{
return value._element.GetInt64();
}
catch (InvalidOperationException e)
{
throw new InvalidCastException(GetInvalidKindExceptionText(typeof(long), value._element), e);
}
catch (FormatException formatException)
{
throw new InvalidCastException(GetInvalidFormatExceptionText(typeof(long), value._element), formatException);
}
}
/// <summary>
/// Converts the value to a <see cref="ulong"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
public static implicit operator ulong(DynamicData value)
{
try
{
return value._element.GetUInt64();
}
catch (InvalidOperationException e)
{
throw new InvalidCastException(GetInvalidKindExceptionText(typeof(ulong), value._element), e);
}
catch (FormatException formatException)
{
throw new InvalidCastException(GetInvalidFormatExceptionText(typeof(ulong), value._element), formatException);
}
}
/// <summary>
/// Converts the value to a <see cref="float"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
public static implicit operator float(DynamicData value)
{
try
{
return value._element.GetSingle();
}
catch (InvalidOperationException e)
{
throw new InvalidCastException(GetInvalidKindExceptionText(typeof(float), value._element), e);
}
catch (FormatException formatException)
{
throw new InvalidCastException(GetInvalidFormatExceptionText(typeof(float), value._element), formatException);
}
}
/// <summary>
/// Converts the value to a <see cref="double"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
public static implicit operator double(DynamicData value)
{
try
{
return value._element.GetDouble();
}
catch (InvalidOperationException e)
{
throw new InvalidCastException(GetInvalidKindExceptionText(typeof(double), value._element), e);
}
catch (FormatException formatException)
{
throw new InvalidCastException(GetInvalidFormatExceptionText(typeof(double), value._element), formatException);
}
}
/// <summary>
/// Converts the value to a <see cref="decimal"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
public static implicit operator decimal(DynamicData value)
{
try
{
return value._element.GetDecimal();
}
catch (InvalidOperationException e)
{
throw new InvalidCastException(GetInvalidKindExceptionText(typeof(decimal), value._element), e);
}
catch (FormatException formatException)
{
throw new InvalidCastException(GetInvalidFormatExceptionText(typeof(decimal), value._element), formatException);
}
}
/// <summary>
/// Converts the value to a <see cref="DateTime"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
public static explicit operator DateTime(DynamicData value)
{
try
{
if (value._options.DateTimeFormat.Equals(UnixFormat, StringComparison.InvariantCultureIgnoreCase))
{
return value.ConvertTo<DateTime>();
}
return value._element.GetDateTime();
}
catch (InvalidOperationException e)
{
throw new InvalidCastException(GetInvalidKindExceptionText(typeof(DateTime), value._element), e);
}
catch (FormatException formatException)
{
throw new InvalidCastException(GetInvalidFormatExceptionText(typeof(DateTime), value._element), formatException);
}
}
/// <summary>
/// Converts the value to a <see cref="DateTimeOffset"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
public static explicit operator DateTimeOffset(DynamicData value)
{
try
{
if (value._options.DateTimeFormat.Equals(UnixFormat, StringComparison.InvariantCultureIgnoreCase))
{
return value.ConvertTo<DateTimeOffset>();
}
return value._element.GetDateTimeOffset();
}
catch (InvalidOperationException e)
{
throw new InvalidCastException(GetInvalidKindExceptionText(typeof(DateTimeOffset), value._element), e);
}
catch (FormatException formatException)
{
throw new InvalidCastException(GetInvalidFormatExceptionText(typeof(DateTimeOffset), value._element), formatException);
}
}
/// <summary>
/// Converts the value to a <see cref="Guid"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
public static explicit operator Guid(DynamicData value)
{
try
{
return value._element.GetGuid();
}
catch (InvalidOperationException e)
{
throw new InvalidCastException(GetInvalidKindExceptionText(typeof(Guid), value._element), e);
}
catch (FormatException formatException)
{
throw new InvalidCastException(GetInvalidFormatExceptionText(typeof(Guid), value._element), formatException);
}
}
/// <summary>
/// Determines whether the specified <see cref="DynamicData"/> and <see cref="object"/> have the same value.
/// </summary>
/// <remarks>
/// This operator calls through to <see cref="DynamicData.Equals(object?)"/> when DynamicData is on the left-hand
/// side of the operation. <see cref="DynamicData.Equals(object?)"/> has value semantics when the DynamicData represents
/// a JSON primitive, i.e. string, bool, number, or null, and reference semantics otherwise, i.e. for objects and arrays.
///
/// Please note that if DynamicData is on the right-hand side of a <c>==</c> operation, this operator will not be invoked.
/// Because of this the result of a <c>==</c> comparison with <c>null</c> on the left and a DynamicData instance on the right will return <c>false</c>.
/// </remarks>
/// <param name="left">The <see cref="DynamicData"/> to compare.</param>
/// <param name="right">The <see cref="object"/> to compare.</param>
/// <returns><c>true</c> if the value of <paramref name="left"/> is the same as the value of <paramref name="right"/>; otherwise, <c>false</c>.</returns>
public static bool operator ==(DynamicData? left, object? right)
{
if (left is null)
{
return right is null;
}
return left.Equals(right);
}
/// <summary>
/// Determines whether the specified <see cref="DynamicData"/> and <see cref="object"/> have different values.
/// </summary>
/// <remarks>
/// This operator calls through to <see cref="DynamicData.Equals(object?)"/> when DynamicData is on the left-hand
/// side of the operation. <see cref="DynamicData.Equals(object?)"/> has value semantics when the DynamicData represents
/// a JSON primitive, i.e. string, bool, number, or null, and reference semantics otherwise, i.e. for objects and arrays.
/// </remarks>
/// <param name="left">The <see cref="DynamicData"/> to compare.</param>
/// <param name="right">The <see cref="object"/> to compare.</param>
/// <returns><c>true</c> if the value of <paramref name="left"/> is different from the value of <paramref name="right"/>; otherwise, <c>false</c>.</returns>
public static bool operator !=(DynamicData? left, object? right) => !(left == right);
private static string GetInvalidKindExceptionText(Type target, MutableJsonElement element)
{
return $"Unable to cast element to '{target}'. Element has kind '{element.ValueKind}'.";
}
private static string GetInvalidFormatExceptionText(Type target, MutableJsonElement element)
{
return $"Unable to cast element to '{target}'. Element has value '{element}'.";
}
}
}