|
| 1 | +// The Sisk Framework source code |
| 2 | +// Copyright (c) 2023 PROJECT PRINCIPIUM |
| 3 | +// |
| 4 | +// The code below is licensed under the MIT license as |
| 5 | +// of the date of its publication, available at |
| 6 | +// |
| 7 | +// File name: ValueItem.cs |
| 8 | +// Repository: https://github.com/sisk-http/core |
| 9 | + |
| 10 | +using Sisk.Core.Internal; |
| 11 | +using System; |
| 12 | +using System.Collections.Generic; |
| 13 | +using System.Linq; |
| 14 | +using System.Text; |
| 15 | +using System.Threading.Tasks; |
| 16 | + |
| 17 | +namespace Sisk.Core.Entity; |
| 18 | + |
| 19 | +public class ValueItem |
| 20 | +{ |
| 21 | + private string? _ref; |
| 22 | + private string argName; |
| 23 | + private string argType; |
| 24 | + |
| 25 | + internal ValueItem(string name, string type, string? data) |
| 26 | + { |
| 27 | + _ref = data; |
| 28 | + argName = name; |
| 29 | + argType = type; |
| 30 | + } |
| 31 | + |
| 32 | + public string Name { get => argName; } |
| 33 | + public string? Value { get => _ref; } |
| 34 | + public bool IsNullOrEmpty { get => string.IsNullOrEmpty(_ref); } |
| 35 | + public bool IsNull { get => _ref == null; } |
| 36 | + |
| 37 | + public ValueItem? MaybeNull() |
| 38 | + { |
| 39 | + if (IsNull) |
| 40 | + { |
| 41 | + return null; |
| 42 | + } |
| 43 | + return this; |
| 44 | + } |
| 45 | + |
| 46 | + public string GetString() |
| 47 | + { |
| 48 | + ThrowIfNull(); |
| 49 | + return _ref!; |
| 50 | + } |
| 51 | + |
| 52 | + public int GetInteger() |
| 53 | + { |
| 54 | + ThrowIfNull(); |
| 55 | + try |
| 56 | + { |
| 57 | + return int.Parse(_ref!); |
| 58 | + } |
| 59 | + catch (Exception ex) when (ex is FormatException || ex is InvalidCastException) |
| 60 | + { |
| 61 | + throw new FormatException(string.Format(SR.ValueItem_CastException, _ref, argName, "integer")); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public int GetByte() |
| 66 | + { |
| 67 | + ThrowIfNull(); |
| 68 | + try |
| 69 | + { |
| 70 | + return byte.Parse(_ref!); |
| 71 | + } |
| 72 | + catch (Exception ex) when (ex is FormatException || ex is InvalidCastException) |
| 73 | + { |
| 74 | + throw new FormatException(string.Format(SR.ValueItem_CastException, _ref, argName, "byte")); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public long GetLong() |
| 79 | + { |
| 80 | + ThrowIfNull(); |
| 81 | + try |
| 82 | + { |
| 83 | + return long.Parse(_ref!); |
| 84 | + } |
| 85 | + catch (Exception ex) when (ex is FormatException || ex is InvalidCastException) |
| 86 | + { |
| 87 | + throw new FormatException(string.Format(SR.ValueItem_CastException, _ref, argName, "long")); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + public short GetShort() |
| 92 | + { |
| 93 | + ThrowIfNull(); |
| 94 | + try |
| 95 | + { |
| 96 | + return short.Parse(_ref!); |
| 97 | + } |
| 98 | + catch (Exception ex) when (ex is FormatException || ex is InvalidCastException) |
| 99 | + { |
| 100 | + throw new FormatException(string.Format(SR.ValueItem_CastException, _ref, argName, "short")); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + public double GetDouble() |
| 105 | + { |
| 106 | + ThrowIfNull(); |
| 107 | + try |
| 108 | + { |
| 109 | + return double.Parse(_ref!); |
| 110 | + } |
| 111 | + catch (Exception ex) when (ex is FormatException || ex is InvalidCastException) |
| 112 | + { |
| 113 | + throw new FormatException(string.Format(SR.ValueItem_CastException, _ref, argName, "double")); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + public bool GetBoolean() |
| 118 | + { |
| 119 | + ThrowIfNull(); |
| 120 | + try |
| 121 | + { |
| 122 | + return bool.Parse(_ref!); |
| 123 | + } |
| 124 | + catch (Exception ex) when (ex is FormatException || ex is InvalidCastException) |
| 125 | + { |
| 126 | + throw new FormatException(string.Format(SR.ValueItem_CastException, _ref, argName, "boolean")); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + public DateTime GetDateTime() |
| 131 | + { |
| 132 | + ThrowIfNull(); |
| 133 | + try |
| 134 | + { |
| 135 | + return DateTime.Parse(_ref!); |
| 136 | + } |
| 137 | + catch (Exception ex) when (ex is FormatException || ex is InvalidCastException) |
| 138 | + { |
| 139 | + throw new FormatException(string.Format(SR.ValueItem_CastException, _ref, argName, "DateTime")); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + public Guid GetGuid() |
| 144 | + { |
| 145 | + ThrowIfNull(); |
| 146 | + try |
| 147 | + { |
| 148 | + return Guid.Parse(_ref!); |
| 149 | + } |
| 150 | + catch (Exception ex) when (ex is FormatException || ex is InvalidCastException) |
| 151 | + { |
| 152 | + throw new FormatException(string.Format(SR.ValueItem_CastException, _ref, argName, "GUID")); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + void ThrowIfNull() |
| 157 | + { |
| 158 | + if (IsNull) |
| 159 | + { |
| 160 | + throw new NullReferenceException(string.Format(SR.ValueItem_ValueNull, argName, argType)); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + public static implicit operator string?(ValueItem i) |
| 165 | + { |
| 166 | + return i.Value; |
| 167 | + } |
| 168 | +} |
0 commit comments