• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loresoft / FluentCommand / 8973726646

06 May 2024 06:14PM UTC coverage: 53.644% (-0.8%) from 54.48%
8973726646

push

github

pwelter34
fix test snapshots

1165 of 2845 branches covered (40.95%)

Branch coverage included in aggregate %.

3686 of 6198 relevant lines covered (59.47%)

697.06 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

25.21
/src/FluentCommand/Extensions/ConvertExtensions.cs
1
using System.Globalization;
2

3
#nullable enable
4

5
namespace FluentCommand.Extensions;
6

7

8
/// <summary>
9
/// Converts a string data type to another base data type using a safe conversion method.
10
/// </summary>
11
public static class ConvertExtensions
12
{
13
    /// <summary>
14
    /// Converts the specified string representation of a logical value to its Boolean equivalent.
15
    /// </summary>
16
    /// <param name="value">A string that contains the value of either <see cref="F:System.Boolean.TrueString"/> or <see cref="F:System.Boolean.FalseString"/>.</param>
17
    /// <returns>
18
    /// true if <paramref name="value"/> equals <see cref="F:System.Boolean.TrueString"/>, or false if <paramref name="value"/> equals <see cref="F:System.Boolean.FalseString"/> or null.
19
    /// </returns>
20
    public static bool ToBoolean(this string? value)
21
    {
22
        if (value == null)
12!
23
            return false;
×
24

25
        if (bool.TryParse(value, out var result))
12✔
26
            return result;
9✔
27

28
        string v = value.Trim();
3✔
29

30
        if (string.Equals(v, "t", StringComparison.OrdinalIgnoreCase)
3!
31
            || string.Equals(v, "true", StringComparison.OrdinalIgnoreCase)
3✔
32
            || string.Equals(v, "y", StringComparison.OrdinalIgnoreCase)
3✔
33
            || string.Equals(v, "yes", StringComparison.OrdinalIgnoreCase)
3✔
34
            || string.Equals(v, "1", StringComparison.OrdinalIgnoreCase)
3✔
35
            || string.Equals(v, "x", StringComparison.OrdinalIgnoreCase)
3✔
36
            || string.Equals(v, "on", StringComparison.OrdinalIgnoreCase))
3✔
37
            return true;
×
38

39
        return false;
3✔
40
    }
41

42
    /// <summary>
43
    /// Converts the specified string representation of a number to an equivalent 8-bit unsigned integer.
44
    /// </summary>
45
    /// <param name="value">A string that contains the number to convert.</param>
46
    /// <returns>
47
    /// An 8-bit unsigned integer that is equivalent to <paramref name="value"/>, or zero if <paramref name="value"/> is null.
48
    /// </returns>
49
    public static byte? ToByte(this string? value)
50
    {
51
        if (value == null)
×
52
            return null;
×
53

54
        if (byte.TryParse(value, out var result))
×
55
            return result;
×
56

57
        return null;
×
58
    }
59

60
    /// <summary>
61
    /// Converts the specified string representation of a number to an equivalent 8-bit unsigned integer, using specified culture-specific formatting information.
62
    /// </summary>
63
    /// <param name="value">A string that contains the number to convert.</param>
64
    /// <param name="provider">An object that supplies culture-specific formatting information.</param>
65
    /// <returns>
66
    /// An 8-bit unsigned integer that is equivalent to <paramref name="value"/>, or zero if <paramref name="value"/> is null.
67
    /// </returns>
68
    public static byte? ToByte(this string? value, IFormatProvider provider)
69
    {
70
        if (value == null)
×
71
            return null;
×
72

73
        if (byte.TryParse(value, NumberStyles.Integer, provider, out var result))
×
74
            return result;
×
75

76
        return null;
×
77
    }
78

79
    /// <summary>
80
    /// Converts the specified string representation of a date and time to an equivalent date and time value.
81
    /// </summary>
82
    /// <param name="value">The string representation of a date and time.</param>
83
    /// <returns>
84
    /// The date and time equivalent of the value of <paramref name="value"/>, or the date and time equivalent of <see cref="F:System.DateTime.MinValue"/> if <paramref name="value"/> is null.
85
    /// </returns>
86
    public static DateTime? ToDateTime(this string? value)
87
    {
88
        if (value == null)
×
89
            return null;
×
90

91
        if (DateTime.TryParse(value, out var result))
×
92
            return result;
×
93

94
        if (DateTime.TryParseExact(value, "M/d/yyyy hh:mm:ss tt", CultureInfo.CurrentCulture, DateTimeStyles.None, out result))
×
95
            return result;
×
96

97
        if (DateTime.TryParseExact(value, "M/d/yyyy", CultureInfo.CurrentCulture, DateTimeStyles.None, out result))
×
98
            return result;
×
99

100
        return null;
×
101
    }
102

103
    /// <summary>
104
    /// Converts the specified string representation of a number to an equivalent date and time, using the specified culture-specific formatting information.
105
    /// </summary>
106
    /// <param name="value">A string that contains a date and time to convert.</param>
107
    /// <param name="provider">An object that supplies culture-specific formatting information.</param>
108
    /// <returns>
109
    /// The date and time equivalent of the value of <paramref name="value"/>, or the date and time equivalent of <see cref="F:System.DateTime.MinValue"/> if <paramref name="value"/> is null.
110
    /// </returns>
111
    public static DateTime? ToDateTime(this string? value, IFormatProvider provider)
112
    {
113
        if (value == null)
×
114
            return null;
×
115

116
        if (DateTime.TryParse(value, out var result))
×
117
            return result;
×
118

119
        if (DateTime.TryParseExact(value, "M/d/yyyy hh:mm:ss tt", provider, DateTimeStyles.None, out result))
×
120
            return result;
×
121

122
        if (DateTime.TryParseExact(value, "M/d/yyyy", provider, DateTimeStyles.None, out result))
×
123
            return result;
×
124

125
        return null;
×
126
    }
127

128
    /// <summary>
129
    /// Converts the specified string representation of a number to an equivalent decimal number.
130
    /// </summary>
131
    /// <param name="value">A string that contains a number to convert.</param>
132
    /// <returns>
133
    /// A decimal number that is equivalent to the number in <paramref name="value"/>, or 0 (zero) if <paramref name="value"/> is null.
134
    /// </returns>
135
    public static decimal? ToDecimal(this string? value)
136
    {
137
        if (value == null)
×
138
            return null;
×
139

140
        if (decimal.TryParse(value, NumberStyles.Currency, CultureInfo.CurrentCulture, out var result))
×
141
            return result;
×
142

143
        return null;
×
144
    }
145

146
    /// <summary>
147
    /// Converts the specified string representation of a number to an equivalent decimal number, using the specified culture-specific formatting information.
148
    /// </summary>
149
    /// <param name="value">A string that contains a number to convert.</param>
150
    /// <param name="provider">An object that supplies culture-specific formatting information.</param>
151
    /// <returns>
152
    /// A decimal number that is equivalent to the number in <paramref name="value"/>, or 0 (zero) if <paramref name="value"/> is null.
153
    /// </returns>
154
    public static decimal? ToDecimal(this string? value, IFormatProvider provider)
155
    {
156
        if (value == null)
×
157
            return null;
×
158

159
        if (decimal.TryParse(value, NumberStyles.Currency, provider, out var result))
×
160
            return result;
×
161

162
        return null;
×
163
    }
164

165
    /// <summary>
166
    /// Converts the specified string representation of a number to an equivalent double-precision floating-point number.
167
    /// </summary>
168
    /// <param name="value">A string that contains the number to convert.</param>
169
    /// <returns>
170
    /// A double-precision floating-point number that is equivalent to the number in <paramref name="value"/>, or 0 (zero) if <paramref name="value"/> is null.
171
    /// </returns>
172
    public static double? ToDouble(this string? value)
173
    {
174
        if (value == null)
×
175
            return null;
×
176

177
        if (double.TryParse(value, out var result))
×
178
            return result;
×
179

180
        return null;
×
181
    }
182

183
    /// <summary>
184
    /// Converts the specified string representation of a number to an equivalent double-precision floating-point number, using the specified culture-specific formatting information.
185
    /// </summary>
186
    /// <param name="value">A string that contains the number to convert.</param>
187
    /// <param name="provider">An object that supplies culture-specific formatting information.</param>
188
    /// <returns>
189
    /// A double-precision floating-point number that is equivalent to the number in <paramref name="value"/>, or 0 (zero) if <paramref name="value"/> is null.
190
    /// </returns>
191
    public static double? ToDouble(this string? value, IFormatProvider provider)
192
    {
193
        if (value == null)
×
194
            return null;
×
195

196
        if (double.TryParse(value, NumberStyles.Float | NumberStyles.AllowThousands, provider, out var result))
×
197
            return result;
×
198

199
        return null;
×
200
    }
201

202
    /// <summary>
203
    /// Converts the specified string representation of a number to an equivalent 16-bit signed integer.
204
    /// </summary>
205
    /// <param name="value">A string that contains the number to convert.</param>
206
    /// <returns>
207
    /// A 16-bit signed integer that is equivalent to the number in <paramref name="value"/>, or 0 (zero) if <paramref name="value"/> is null.
208
    /// </returns>
209
    public static short? ToInt16(this string? value)
210
    {
211
        if (value == null)
×
212
            return null;
×
213

214
        if (short.TryParse(value, out var result))
×
215
            return result;
×
216

217
        return null;
×
218
    }
219

220
    /// <summary>
221
    /// Converts the specified string representation of a number to an equivalent 16-bit signed integer, using the specified culture-specific formatting information.
222
    /// </summary>
223
    /// <param name="value">A string that contains the number to convert.</param>
224
    /// <param name="provider">An object that supplies culture-specific formatting information.</param>
225
    /// <returns>
226
    /// A 16-bit signed integer that is equivalent to the number in <paramref name="value"/>, or 0 (zero) if <paramref name="value"/> is null.
227
    /// </returns>
228
    public static short? ToInt16(this string? value, IFormatProvider provider)
229
    {
230
        if (value == null)
×
231
            return null;
×
232

233
        if (short.TryParse(value, NumberStyles.Integer, provider, out var result))
×
234
            return result;
×
235

236
        return null;
×
237
    }
238

239
    /// <summary>
240
    /// Converts the specified string representation of a number to an equivalent 32-bit signed integer.
241
    /// </summary>
242
    /// <param name="value">A string that contains the number to convert.</param>
243
    /// <returns>
244
    /// A 32-bit signed integer that is equivalent to the number in <paramref name="value"/>, or 0 (zero) if <paramref name="value"/> is null.
245
    /// </returns>
246
    public static int? ToInt32(this string? value)
247
    {
248
        if (value == null)
9!
249
            return null;
×
250

251
        if (int.TryParse(value, out var result))
9✔
252
            return result;
6✔
253

254
        return null;
3✔
255
    }
256

257
    /// <summary>
258
    /// Converts the specified string representation of a number to an equivalent 32-bit signed integer, using the specified culture-specific formatting information.
259
    /// </summary>
260
    /// <param name="value">A string that contains the number to convert.</param>
261
    /// <param name="provider">An object that supplies culture-specific formatting information.</param>
262
    /// <returns>
263
    /// A 32-bit signed integer that is equivalent to the number in <paramref name="value"/>, or 0 (zero) if <paramref name="value"/> is null.
264
    /// </returns>
265
    public static int? ToInt32(this string? value, IFormatProvider provider)
266
    {
267
        if (value == null)
×
268
            return null;
×
269

270
        if (int.TryParse(value, NumberStyles.Integer, provider, out var result))
×
271
            return result;
×
272

273
        return null;
×
274
    }
275

276
    /// <summary>
277
    /// Converts the specified string representation of a number to an equivalent 64-bit signed integer.
278
    /// </summary>
279
    /// <param name="value">A string that contains a number to convert.</param>
280
    /// <returns>
281
    /// A 64-bit signed integer that is equivalent to the number in <paramref name="value"/>, or 0 (zero) if <paramref name="value"/> is null.
282
    /// </returns>
283
    public static long? ToInt64(this string? value)
284
    {
285
        if (value == null)
×
286
            return null;
×
287

288
        if (long.TryParse(value, out var result))
×
289
            return result;
×
290

291
        return null;
×
292
    }
293

294
    /// <summary>
295
    /// Converts the specified string representation of a number to an equivalent 64-bit signed integer, using the specified culture-specific formatting information.
296
    /// </summary>
297
    /// <param name="value">A string that contains the number to convert.</param>
298
    /// <param name="provider">An object that supplies culture-specific formatting information.</param>
299
    /// <returns>
300
    /// A 64-bit signed integer that is equivalent to the number in <paramref name="value"/>, or 0 (zero) if <paramref name="value"/> is null.
301
    /// </returns>
302
    public static long? ToInt64(this string? value, IFormatProvider provider)
303
    {
304
        if (value == null)
×
305
            return null;
×
306

307
        if (long.TryParse(value, NumberStyles.Integer, provider, out var result))
×
308
            return result;
×
309

310
        return null;
×
311
    }
312

313
    /// <summary>
314
    /// Converts the specified string representation of a number to an equivalent single-precision floating-point number.
315
    /// </summary>
316
    /// <param name="value">A string that contains the number to convert.</param>
317
    /// <returns>
318
    /// A single-precision floating-point number that is equivalent to the number in <paramref name="value"/>, or 0 (zero) if <paramref name="value"/> is null.
319
    /// </returns>
320
    public static float? ToSingle(this string? value)
321
    {
322
        if (value == null)
×
323
            return null;
×
324

325
        if (float.TryParse(value, out var result))
×
326
            return result;
×
327

328
        return null;
×
329
    }
330

331
    /// <summary>
332
    /// Converts the specified string representation of a number to an equivalent single-precision floating-point number, using the specified culture-specific formatting information.
333
    /// </summary>
334
    /// <param name="value">A string that contains the number to convert.</param>
335
    /// <param name="provider">An object that supplies culture-specific formatting information.</param>
336
    /// <returns>
337
    /// A single-precision floating-point number that is equivalent to the number in <paramref name="value"/>, or 0 (zero) if <paramref name="value"/> is null.
338
    /// </returns>
339
    public static float? ToSingle(this string? value, IFormatProvider provider)
340
    {
341
        if (value == null)
×
342
            return null;
×
343

344
        if (float.TryParse(value, NumberStyles.Float | NumberStyles.AllowThousands, provider, out var result))
×
345
            return result;
×
346

347
        return null;
×
348
    }
349

350
    /// <summary>
351
    /// Converts the specified string representation of a number to an equivalent 16-bit unsigned integer.
352
    /// </summary>
353
    /// <param name="value">A string that contains the number to convert.</param>
354
    /// <returns>
355
    /// A 16-bit unsigned integer that is equivalent to the number in <paramref name="value"/>, or 0 (zero) if <paramref name="value"/> is null.
356
    /// </returns>
357
    public static ushort? ToUInt16(this string? value)
358
    {
359
        if (value == null)
×
360
            return null;
×
361

362
        if (ushort.TryParse(value, out var result))
×
363
            return result;
×
364

365
        return null;
×
366
    }
367

368
    /// <summary>
369
    /// Converts the specified string representation of a number to an equivalent 16-bit unsigned integer, using the specified culture-specific formatting information.
370
    /// </summary>
371
    /// <param name="value">A string that contains the number to convert.</param>
372
    /// <param name="provider">An object that supplies culture-specific formatting information.</param>
373
    /// <returns>
374
    /// A 16-bit unsigned integer that is equivalent to the number in <paramref name="value"/>, or 0 (zero) if <paramref name="value"/> is null.
375
    /// </returns>
376
    public static ushort? ToUInt16(this string? value, IFormatProvider provider)
377
    {
378
        if (value == null)
×
379
            return null;
×
380

381
        if (ushort.TryParse(value, NumberStyles.Integer, provider, out var result))
×
382
            return result;
×
383

384
        return null;
×
385
    }
386

387
    /// <summary>
388
    /// Converts the specified string representation of a number to an equivalent 32-bit unsigned integer.
389
    /// </summary>
390
    /// <param name="value">A string that contains the number to convert.</param>
391
    /// <returns>
392
    /// A 32-bit unsigned integer that is equivalent to the number in <paramref name="value"/>, or 0 (zero) if <paramref name="value"/> is null.
393
    /// </returns>
394
    public static uint? ToUInt32(this string? value)
395
    {
396
        if (value == null)
×
397
            return null;
×
398

399
        if (uint.TryParse(value, out var result))
×
400
            return result;
×
401

402
        return null;
×
403
    }
404

405
    /// <summary>
406
    /// Converts the specified string representation of a number to an equivalent 32-bit unsigned integer, using the specified culture-specific formatting information.
407
    /// </summary>
408
    /// <param name="value">A string that contains the number to convert.</param>
409
    /// <param name="provider">An object that supplies culture-specific formatting information.</param>
410
    /// <returns>
411
    /// A 32-bit unsigned integer that is equivalent to the number in <paramref name="value"/>, or 0 (zero) if <paramref name="value"/> is null.
412
    /// </returns>
413
    public static uint? ToUInt32(this string? value, IFormatProvider provider)
414
    {
415
        if (value == null)
×
416
            return null;
×
417

418
        if (uint.TryParse(value, NumberStyles.Integer, provider, out var result))
×
419
            return result;
×
420

421
        return null;
×
422
    }
423

424
    /// <summary>
425
    /// Converts the specified string representation of a number to an equivalent 64-bit unsigned integer.
426
    /// </summary>
427
    /// <param name="value">A string that contains the number to convert.</param>
428
    /// <returns>
429
    /// A 64-bit signed integer that is equivalent to the number in <paramref name="value"/>, or 0 (zero) if <paramref name="value"/> is null.
430
    /// </returns>
431
    public static ulong? ToUInt64(this string? value)
432
    {
433
        if (value == null)
×
434
            return null;
×
435

436
        if (ulong.TryParse(value, out var result))
×
437
            return result;
×
438

439
        return null;
×
440
    }
441

442
    /// <summary>
443
    /// Converts the specified string representation of a number to an equivalent 64-bit unsigned integer, using the specified culture-specific formatting information.
444
    /// </summary>
445
    /// <param name="value">A string that contains the number to convert.</param>
446
    /// <param name="provider">An object that supplies culture-specific formatting information.</param>
447
    /// <returns>
448
    /// A 64-bit unsigned integer that is equivalent to the number in <paramref name="value"/>, or 0 (zero) if <paramref name="value"/> is null.
449
    /// </returns>
450
    public static ulong? ToUInt64(this string? value, IFormatProvider provider)
451
    {
452
        if (value == null)
×
453
            return null;
×
454

455
        if (ulong.TryParse(value, NumberStyles.Integer, provider, out var result))
×
456
            return result;
×
457

458
        return null;
×
459
    }
460

461
    /// <summary>
462
    /// Converts the specified string to an equivalent <see cref="TimeSpan"/> value.
463
    /// </summary>
464
    /// <param name="value">The string representation of a <see cref="TimeSpan"/>.</param>
465
    /// <returns>
466
    /// The <see cref="TimeSpan"/> equivalent of the <paramref name="value"/>, or <see cref="F:System.TimeSpan.Zero"/> if <paramref name="value"/> is null.
467
    /// </returns>
468
    public static TimeSpan? ToTimeSpan(this string? value)
469
    {
470
        if (value == null)
×
471
            return null;
×
472

473
        if (TimeSpan.TryParse(value, out var result))
×
474
            return result;
×
475

476
        return null;
×
477
    }
478

479
    /// <summary>
480
    /// Converts the specified string to an equivalent <see cref="Guid"/> value.
481
    /// </summary>
482
    /// <param name="value">The string representation of a <see cref="Guid"/>.</param>
483
    /// <returns>
484
    /// The <see cref="Guid"/> equivalent of the <paramref name="value"/>, or <see cref="F:System.Guid.Empty"/> if <paramref name="value"/> is null.
485
    /// </returns>
486
    public static Guid? ToGuid(this string? value)
487
    {
488
        if (value == null)
15!
489
            return null;
×
490

491
        if (Guid.TryParse(value, out var result))
15!
492
            return result;
15✔
493

494
        return null;
×
495
    }
496

497
#if NET6_0_OR_GREATER
498
    /// <summary>
499
    /// Converts the specified string to an equivalent <see cref="TimeSpan"/> value.
500
    /// </summary>
501
    /// <param name="value">The string representation of a <see cref="TimeSpan"/>.</param>
502
    /// <returns>
503
    /// The <see cref="TimeSpan"/> equivalent of the <paramref name="value"/>, or <see cref="F:System.TimeSpan.Zero"/> if <paramref name="value"/> is null.
504
    /// </returns>
505
    public static DateOnly? ToDateOnly(this string? value)
506
    {
507
        if (value == null)
9!
508
            return null;
×
509

510
        if (DateOnly.TryParse(value, out var dateOnly))
9✔
511
            return dateOnly;
6✔
512

513
        if (DateTime.TryParse(value, out var dateTime))
3!
514
            return DateOnly.FromDateTime(dateTime);
×
515

516
        if (DateTimeOffset.TryParse(value, out var dateTimeOffset))
3!
517
            return DateOnly.FromDateTime(dateTimeOffset.DateTime);
×
518

519
        return null;
3✔
520
    }
521

522
    /// <summary>
523
    /// Converts the specified string to an equivalent <see cref="TimeSpan"/> value.
524
    /// </summary>
525
    /// <param name="value">The string representation of a <see cref="TimeSpan"/>.</param>
526
    /// <returns>
527
    /// The <see cref="TimeSpan"/> equivalent of the <paramref name="value"/>, or <see cref="F:System.TimeSpan.Zero"/> if <paramref name="value"/> is null.
528
    /// </returns>
529
    public static TimeOnly? ToTimeOnly(this string? value)
530
    {
531
        if (value == null)
×
532
            return null;
×
533

534
        if (TimeOnly.TryParse(value, out var timeOnly))
×
535
            return timeOnly;
×
536

537
        if (TimeSpan.TryParse(value, out var timeSpan))
×
538
            return TimeOnly.FromTimeSpan(timeSpan);
×
539

540
        if (DateTime.TryParse(value, out var dateTime))
×
541
            return TimeOnly.FromDateTime(dateTime);
×
542

543
        if (DateTimeOffset.TryParse(value, out var dateTimeOffset))
×
544
            return TimeOnly.FromDateTime(dateTimeOffset.DateTime);
×
545

546
        return null;
×
547
    }
548
#endif
549

550

551
    /// <summary>
552
    /// Tries to convert the <paramref name="input"/> to the specified <paramref name="type"/>.
553
    /// </summary>
554
    /// <param name="input">The input to convert.</param>
555
    /// <param name="type">The type to convert to.</param>
556
    /// <returns>The converted value.</returns>
557
    public static object? SafeConvert(Type type, string? input)
558
    {
559
        if (type is null)
87!
560
            throw new ArgumentNullException(nameof(type));
×
561

562
        // first try string
563
        if (type == typeof(string))
87✔
564
        {
565
            return input;
30✔
566
        }
567

568
        var isNullable = type.IsNullable();
57✔
569
        if ((input?.IsNullOrEmpty() != false) && isNullable)
57!
570
        {
571
            return null;
12✔
572
        }
573

574
        input = input?.Trim();
45!
575
        var underlyingType = type.GetUnderlyingType();
45✔
576

577
        // convert by type
578
        if (underlyingType == typeof(bool))
45✔
579
        {
580
            return input.ToBoolean();
12✔
581
        }
582
        if (underlyingType == typeof(byte))
33!
583
        {
584
            var value = input.ToByte();
×
585
            return value.HasValue ? value.Value : isNullable ? null : 0;
×
586
        }
587
        if (underlyingType == typeof(DateTime))
33!
588
        {
589
            var value = input.ToDateTime();
×
590
            return value.HasValue ? value.Value : isNullable ? null : DateTime.MinValue;
×
591
        }
592
        if (underlyingType == typeof(decimal))
33!
593
        {
594
            var value = input.ToDecimal();
×
595
            return value.HasValue ? value.Value : isNullable ? null : 0;
×
596
        }
597
        if (underlyingType == typeof(double))
33!
598
        {
599
            var value = input.ToDouble();
×
600
            return value.HasValue ? value.Value : isNullable ? null : 0;
×
601
        }
602
        if (underlyingType == typeof(short))
33!
603
        {
604
            var value = input.ToInt16();
×
605
            return value.HasValue ? value.Value : isNullable ? null : 0;
×
606
        }
607
        if (underlyingType == typeof(int))
33✔
608
        {
609
            var value = input.ToInt32();
9✔
610
            return value.HasValue ? value.Value : isNullable ? null : 0;
9!
611
        }
612
        if (underlyingType == typeof(long))
24!
613
        {
614
            var value = input.ToInt64();
×
615
            return value.HasValue ? value.Value : isNullable ? null : 0;
×
616
        }
617
        if (underlyingType == typeof(float))
24!
618
        {
619
            var value = input.ToSingle();
×
620
            return value.HasValue ? value.Value : isNullable ? null : 0;
×
621
        }
622
        if (underlyingType == typeof(ushort))
24!
623
        {
624
            var value = input.ToUInt16();
×
625
            return value.HasValue ? value.Value : isNullable ? null : 0;
×
626
        }
627
        if (underlyingType == typeof(uint))
24!
628
        {
629
            var value = input.ToUInt32();
×
630
            return value.HasValue ? value.Value : isNullable ? null : 0;
×
631
        }
632
        if (underlyingType == typeof(ulong))
24!
633
        {
634
            var value = input.ToUInt64();
×
635
            return value.HasValue ? value.Value : isNullable ? null : 0;
×
636
        }
637
        if (underlyingType == typeof(TimeSpan))
24!
638
        {
639
            var value = input.ToTimeSpan();
×
640
            return value.HasValue ? value.Value : isNullable ? null : TimeSpan.Zero;
×
641
        }
642
        if (underlyingType == typeof(Guid))
24✔
643
        {
644
            var value = input.ToGuid();
15✔
645
            return value.HasValue ? value.Value : isNullable ? null : Guid.Empty;
15!
646
        }
647
#if NET6_0_OR_GREATER
648
        if (underlyingType == typeof(DateOnly))
9!
649
        {
650
            var value = input.ToDateOnly();
9✔
651
            return value.HasValue ? value.Value : isNullable ? null : DateOnly.MinValue;
9!
652
        }
653
        if (underlyingType == typeof(TimeOnly))
×
654
        {
655
            var value = input.ToTimeOnly();
×
656
            return value.HasValue ? value.Value : isNullable ? null : TimeOnly.MinValue;
×
657
        }
658
#endif
659
        return default;
×
660
    }
661

662

663
    /// <summary>
664
    /// Converts the result to the TValue type.
665
    /// </summary>
666
    /// <typeparam name="TValue">The type of the value.</typeparam>
667
    /// <param name="result">The result to convert.</param>
668
    /// <param name="convert">The optional convert function.</param>
669
    /// <returns>The converted value.</returns>
670
    public static TValue? ConvertValue<TValue>(object? result, Func<object?, TValue>? convert = null)
671
    {
672
        if (result is null || result == DBNull.Value)
87!
673
            return default;
×
674

675
        if (result is TValue valueType)
87✔
676
            return valueType;
48✔
677

678
        if (convert != null)
39!
679
            return convert(result);
×
680

681
        if (result is string stringValue)
39✔
682
            return (TValue?)SafeConvert(typeof(TValue), stringValue);
15✔
683

684
        return (TValue)Convert.ChangeType(result, typeof(TValue));
24✔
685
    }
686
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc