forked from RehanSaeed/Schema.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValues{T1,T2,T3}.cs
More file actions
462 lines (408 loc) · 17.3 KB
/
Values{T1,T2,T3}.cs
File metadata and controls
462 lines (408 loc) · 17.3 KB
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
namespace Schema.NET;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
/// <summary>
/// A single or list of values which can be any of the specified types.
/// </summary>
/// <typeparam name="T1">The first type the values can take.</typeparam>
/// <typeparam name="T2">The second type the values can take.</typeparam>
/// <typeparam name="T3">The third type the values can take.</typeparam>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
[DebuggerTypeProxy(typeof(IValuesDebugView))]
[CollectionBuilder(typeof(ValuesBuilder), nameof(ValuesBuilder.Create))]
public readonly struct Values<T1, T2, T3> : IReadOnlyCollection<object?>, IValues, IEquatable<Values<T1, T2, T3>>
{
/// <summary>
/// Initializes a new instance of the <see cref="Values{T1,T2,T3}"/> struct.
/// </summary>
/// <param name="value">The value of type <typeparamref name="T1"/>.</param>
public Values(OneOrMany<T1> value)
{
this.Value1 = value;
this.Value2 = default;
this.Value3 = default;
this.HasValue1 = value.Count > 0;
this.HasValue2 = false;
this.HasValue3 = false;
}
/// <summary>
/// Initializes a new instance of the <see cref="Values{T1,T2,T3}"/> struct.
/// </summary>
/// <param name="value">The value of type <typeparamref name="T2"/>.</param>
public Values(OneOrMany<T2> value)
{
this.Value1 = default;
this.Value2 = value;
this.Value3 = default;
this.HasValue1 = false;
this.HasValue2 = value.Count > 0;
this.HasValue3 = false;
}
/// <summary>
/// Initializes a new instance of the <see cref="Values{T1,T2,T3}"/> struct.
/// </summary>
/// <param name="value">The value of type <typeparamref name="T3"/>.</param>
public Values(OneOrMany<T3> value)
{
this.Value1 = default;
this.Value2 = default;
this.Value3 = value;
this.HasValue1 = false;
this.HasValue2 = false;
this.HasValue3 = value.Count > 0;
}
/// <summary>
/// Initializes a new instance of the <see cref="Values{T1,T2,T3}"/> struct.
/// </summary>
/// <param name="items">The items.</param>
public Values(IEnumerable<object?> items)
{
ArgumentNullException.ThrowIfNull(items);
List<T1>? items1 = null;
List<T2>? items2 = null;
List<T3>? items3 = null;
foreach (var item in items)
{
if (item is T3 itemT3)
{
items3 ??= [];
items3.Add(itemT3);
}
else if (item is T2 itemT2)
{
items2 ??= [];
items2.Add(itemT2);
}
else if (item is T1 itemT1)
{
items1 ??= [];
items1.Add(itemT1);
}
}
this.Value1 = items1 == null ? default : (OneOrMany<T1>)items1!;
this.Value2 = items2 == null ? default : (OneOrMany<T2>)items2!;
this.Value3 = items3 == null ? default : (OneOrMany<T3>)items3!;
this.HasValue1 = this.Value1.Count > 0;
this.HasValue2 = this.Value2.Count > 0;
this.HasValue3 = this.Value3.Count > 0;
}
/// <summary>
/// Initializes a new instance of the <see cref="Values{T1,T2,T3}"/> struct.
/// </summary>
/// <param name="items">The items.</param>
public Values(params ReadOnlySpan<object?> items)
{
List<T1>? items1 = null;
List<T2>? items2 = null;
List<T3>? items3 = null;
foreach (var item in items)
{
if (item is T3 itemT3)
{
items3 ??= [];
items3.Add(itemT3);
}
else if (item is T2 itemT2)
{
items2 ??= [];
items2.Add(itemT2);
}
else if (item is T1 itemT1)
{
items1 ??= [];
items1.Add(itemT1);
}
}
this.Value1 = items1 == null ? default : (OneOrMany<T1>)items1!;
this.Value2 = items2 == null ? default : (OneOrMany<T2>)items2!;
this.Value3 = items3 == null ? default : (OneOrMany<T3>)items3!;
this.HasValue1 = this.Value1.Count > 0;
this.HasValue2 = this.Value2.Count > 0;
this.HasValue3 = this.Value3.Count > 0;
}
/// <summary>
/// Gets the number of elements contained in the <see cref="Values{T1,T2,T3}"/>.
/// </summary>
public int Count => this.Value1.Count + this.Value2.Count + this.Value3.Count;
/// <summary>
/// Gets a value indicating whether this instance has a value.
/// </summary>
public bool HasValue => this.HasValue1 || this.HasValue2 || this.HasValue3;
/// <summary>
/// Gets a value indicating whether the value of type <typeparamref name="T1" /> has a value.
/// </summary>
public bool HasValue1 { get; }
/// <summary>
/// Gets a value indicating whether the value of type <typeparamref name="T2" /> has a value.
/// </summary>
public bool HasValue2 { get; }
/// <summary>
/// Gets a value indicating whether the value of type <typeparamref name="T3" /> has a value.
/// </summary>
public bool HasValue3 { get; }
/// <summary>
/// Gets the value of type <typeparamref name="T1" />.
/// </summary>
public OneOrMany<T1> Value1 { get; }
/// <summary>
/// Gets the value of type <typeparamref name="T2" />.
/// </summary>
public OneOrMany<T2> Value2 { get; }
/// <summary>
/// Gets the value of type <typeparamref name="T3" />.
/// </summary>
public OneOrMany<T3> Value3 { get; }
#pragma warning disable CA2225 // Operator overloads have named alternates
#pragma warning disable CA1002 // Do not expose generic lists
/// <summary>
/// Performs an implicit conversion from <typeparamref name="T1"/> to <see cref="Values{T1,T2}"/>.
/// </summary>
/// <param name="item">The single item value.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator Values<T1, T2, T3>(T1? item) => new(item);
/// <summary>
/// Performs an implicit conversion from <typeparamref name="T2"/> to <see cref="Values{T1,T2}"/>.
/// </summary>
/// <param name="item">The single item value.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator Values<T1, T2, T3>(T2? item) => new(item);
/// <summary>
/// Performs an implicit conversion from <typeparamref name="T3"/> to <see cref="Values{T1,T2}"/>.
/// </summary>
/// <param name="item">The single item value.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator Values<T1, T2, T3>(T3? item) => new(item);
/// <summary>
/// Performs an implicit conversion from <typeparamref name="T1[]"/> to <see cref="Values{T1,T2,T3}"/>.
/// </summary>
/// <param name="array">The array of values.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator Values<T1, T2, T3>(T1?[] array) => new(array);
/// <summary>
/// Performs an implicit conversion from <typeparamref name="T2[]"/> to <see cref="Values{T1,T2,T3}"/>.
/// </summary>
/// <param name="array">The array of values.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator Values<T1, T2, T3>(T2?[] array) => new(array);
/// <summary>
/// Performs an implicit conversion from <typeparamref name="T3[]"/> to <see cref="Values{T1,T2,T3}"/>.
/// </summary>
/// <param name="array">The array of values.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator Values<T1, T2, T3>(T3?[] array) => new(array);
/// <summary>
/// Performs an implicit conversion from <see cref="List{T1}"/> to <see cref="Values{T1,T2}"/>.
/// </summary>
/// <param name="list">The list of values.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator Values<T1, T2, T3>(List<T1?> list) => new(list);
/// <summary>
/// Performs an implicit conversion from <see cref="List{T2}"/> to <see cref="Values{T1,T2}"/>.
/// </summary>
/// <param name="list">The list of values.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator Values<T1, T2, T3>(List<T2?> list) => new(list);
/// <summary>
/// Performs an implicit conversion from <see cref="List{T3}"/> to <see cref="Values{T1,T2}"/>.
/// </summary>
/// <param name="list">The list of values.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator Values<T1, T2, T3>(List<T3?> list) => new(list);
/// <summary>
/// Performs an implicit conversion from <see cref="object"/> array to <see cref="Values{T1,T2,T3}"/>.
/// </summary>
/// <param name="array">The array of values.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator Values<T1, T2, T3>(object[] array) => new(array);
/// <summary>
/// Performs an implicit conversion from <see cref="List{Object}"/> to <see cref="Values{T1,T2,T3}"/>.
/// </summary>
/// <param name="list">The list of values.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator Values<T1, T2, T3>(List<object> list) => new(list);
/// <summary>
/// Performs an implicit conversion from <see cref="Values{T1, T2, T3}"/> to the first item of type <typeparamref name="T1"/>.
/// </summary>
/// <param name="values">The values.</param>
/// <returns>
/// The result of the conversion.
/// </returns>
public static implicit operator T1?(Values<T1, T2, T3> values) => values.Value1.FirstOrDefault();
/// <summary>
/// Performs an implicit conversion from <see cref="Values{T1, T2, T3}"/> to the first item of type <typeparamref name="T2"/>.
/// </summary>
/// <param name="values">The values.</param>
/// <returns>
/// The result of the conversion.
/// </returns>
public static implicit operator T2?(Values<T1, T2, T3> values) => values.Value2.FirstOrDefault();
/// <summary>
/// Performs an implicit conversion from <see cref="Values{T1, T2, T3}"/> to the first item of type <typeparamref name="T3"/>.
/// </summary>
/// <param name="values">The values.</param>
/// <returns>
/// The result of the conversion.
/// </returns>
public static implicit operator T3?(Values<T1, T2, T3> values) => values.Value3.FirstOrDefault();
/// <summary>
/// Performs an implicit conversion from <see cref="Values{T1, T2, T3}"/> to an array of <typeparamref name="T1"/>.
/// </summary>
/// <param name="values">The values.</param>
/// <returns>
/// The result of the conversion.
/// </returns>
public static implicit operator T1[](Values<T1, T2, T3> values) => values.Value1.ToArray();
/// <summary>
/// Performs an implicit conversion from <see cref="Values{T1, T2, T3}"/> to <see cref="List{T1}"/>.
/// </summary>
/// <param name="values">The values.</param>
/// <returns>
/// The result of the conversion.
/// </returns>
public static implicit operator List<T1>(Values<T1, T2, T3> values) => values.Value1.ToList();
/// <summary>
/// Performs an implicit conversion from <see cref="Values{T1, T2, T3}"/> to an array of <typeparamref name="T2"/>.
/// </summary>
/// <param name="values">The values.</param>
/// <returns>
/// The result of the conversion.
/// </returns>
public static implicit operator T2[](Values<T1, T2, T3> values) => values.Value2.ToArray();
/// <summary>
/// Performs an implicit conversion from <see cref="Values{T1, T2, T3}"/> to <see cref="List{T2}"/>.
/// </summary>
/// <param name="values">The values.</param>
/// <returns>
/// The result of the conversion.
/// </returns>
public static implicit operator List<T2>(Values<T1, T2, T3> values) => values.Value2.ToList();
/// <summary>
/// Performs an implicit conversion from <see cref="Values{T1, T2, T3}"/> to an array of <typeparamref name="T3"/>.
/// </summary>
/// <param name="values">The values.</param>
/// <returns>
/// The result of the conversion.
/// </returns>
public static implicit operator T3[](Values<T1, T2, T3> values) => values.Value3.ToArray();
/// <summary>
/// Performs an implicit conversion from <see cref="Values{T1, T2, T3}"/> to <see cref="List{T3}"/>.
/// </summary>
/// <param name="values">The values.</param>
/// <returns>
/// The result of the conversion.
/// </returns>
public static implicit operator List<T3>(Values<T1, T2, T3> values) => values.Value3.ToList();
#pragma warning restore CA1002 // Do not expose generic lists
#pragma warning restore CA2225 // Operator overloads have named alternates
/// <summary>
/// Implements the operator ==.
/// </summary>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static bool operator ==(Values<T1, T2, T3> left, Values<T1, T2, T3> right) => left.Equals(right);
/// <summary>
/// Implements the operator !=.
/// </summary>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static bool operator !=(Values<T1, T2, T3> left, Values<T1, T2, T3> right) => !(left == right);
/// <summary>Deconstructs the specified items.</summary>
/// <param name="items1">The items from value 1.</param>
/// <param name="items2">The items from value 2.</param>
/// <param name="items3">The items from value 3.</param>
public void Deconstruct(out IEnumerable<T1> items1, out IEnumerable<T2> items2, out IEnumerable<T3> items3)
{
items1 = this.Value1;
items2 = this.Value2;
items3 = this.Value3;
}
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>An enumerator that can be used to iterate through the collection.</returns>
public IEnumerator<object?> GetEnumerator()
{
if (this.HasValue1)
{
foreach (var item1 in this.Value1)
{
yield return item1;
}
}
if (this.HasValue2)
{
foreach (var item2 in this.Value2)
{
yield return item2;
}
}
if (this.HasValue3)
{
foreach (var item3 in this.Value3)
{
yield return item3;
}
}
}
/// <summary>Returns an enumerator that iterates through a collection.</summary>
/// <returns>An <see cref="IEnumerator"/> object that can be used to iterate through the collection.</returns>
IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator();
/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns>
/// true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.
/// </returns>
public bool Equals(Values<T1, T2, T3> other)
{
if (!other.HasValue && !this.HasValue)
{
return true;
}
return this.Value1.Equals(other.Value1) &&
this.Value2.Equals(other.Value2) &&
this.Value3.Equals(other.Value3);
}
/// <summary>
/// Determines whether the specified <see cref="object" />, is equal to this instance.
/// </summary>
/// <param name="obj">The <see cref="object" /> to compare with this instance.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="object" /> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
public override bool Equals(object? obj) => obj is Values<T1, T2, T3> values && this.Equals(values);
/// <summary>
/// Returns a hash code for this instance.
/// </summary>
/// <returns>
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
/// </returns>
public override int GetHashCode() => HashCode.Of(this.Value1).And(this.Value2).And(this.Value3);
/// <summary>
/// Gets the string to display in the debugger watches window for this instance.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string DebuggerDisplay => $"Length = {this.Count}";
}
public static partial class ValuesBuilder
{
/// <summary>
/// Initializes a new instance of the <see cref="Values{T1,T2,T3}"/> struct.
/// </summary>
/// <param name="items">A single or list of values which can be any of the specified types.</param>
/// <typeparam name="T1">The first type the values can take.</typeparam>
/// <typeparam name="T2">The second type the values can take.</typeparam>
/// <typeparam name="T3">The third type the values can take.</typeparam>
public static Values<T1, T2, T3> Create<T1, T2, T3>(ReadOnlySpan<object> items) => new(items);
}