|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | + |
| 5 | +namespace FikaAmazonAPI.ReportGeneration.ReportDataTable |
| 6 | +{ |
| 7 | + public static class RowExtensionMethods |
| 8 | + { |
| 9 | + public static string GetString(this TableRow row, string id) |
| 10 | + { |
| 11 | + return AValueWithThisIdExists(row, id) |
| 12 | + ? row[id] |
| 13 | + : null; |
| 14 | + } |
| 15 | + public static int GetInt32(this TableRow row, string id) |
| 16 | + { |
| 17 | + return AValueWithThisIdExists(row, id) && TheValueIsNotEmpty(row, id) |
| 18 | + ? Convert.ToInt32(row[id]) |
| 19 | + : int.MinValue; |
| 20 | + } |
| 21 | + public static long GetInt64(this TableRow row, string id) |
| 22 | + { |
| 23 | + return AValueWithThisIdExists(row, id) && TheValueIsNotEmpty(row, id) |
| 24 | + ? Convert.ToInt64(row[id]) |
| 25 | + : long.MinValue; |
| 26 | + } |
| 27 | + public static decimal GetDecimal(this TableRow row, string id) |
| 28 | + { |
| 29 | + return AValueWithThisIdExists(row, id) && TheValueIsNotEmpty(row, id) |
| 30 | + ? Convert.ToDecimal(row[id]) |
| 31 | + : decimal.MinValue; |
| 32 | + } |
| 33 | + public static DateTime GetDateTime(this TableRow row, string id) |
| 34 | + { |
| 35 | + return AValueWithThisIdExists(row, id) && TheValueIsNotEmpty(row, id) |
| 36 | + ? Convert.ToDateTime(row[id]) |
| 37 | + : DateTime.MinValue; |
| 38 | + } |
| 39 | + public static bool GetBoolean(this TableRow row, string id) |
| 40 | + { |
| 41 | + if (TheBooleanValueIsEmpty(row, id)) |
| 42 | + return false; |
| 43 | + |
| 44 | + AssertThatTheRequestIsValid(row, id); |
| 45 | + |
| 46 | + return string.Equals(row[id], "true", StringComparison.OrdinalIgnoreCase); |
| 47 | + } |
| 48 | + private static void AssertThatTheRequestIsValid(TableRow row, string id) |
| 49 | + { |
| 50 | + AssertThatAValueWithThisIdExistsInThisRow(row, id); |
| 51 | + AssertThatThisIsAnAcceptableBoolValue(row, id); |
| 52 | + } |
| 53 | + private static void AssertThatThisIsAnAcceptableBoolValue(TableRow row, string id) |
| 54 | + { |
| 55 | + var acceptedValues = new[] { "true", "false" }; |
| 56 | + if (acceptedValues.Contains(row[id], StringComparer.OrdinalIgnoreCase) == false) |
| 57 | + throw new InvalidCastException($"You must use 'true' or 'false' when setting bools for {id}"); |
| 58 | + } |
| 59 | + |
| 60 | + private static void AssertThatAValueWithThisIdExistsInThisRow(TableRow row, string id) |
| 61 | + { |
| 62 | + if (AValueWithThisIdExists(row, id) == false) |
| 63 | + throw new InvalidOperationException($"{id} could not be found in the row."); |
| 64 | + } |
| 65 | + private static bool TheBooleanValueIsEmpty(TableRow row, string id) |
| 66 | + { |
| 67 | + return AValueWithThisIdExists(row, id) && string.IsNullOrEmpty(row[id]); |
| 68 | + } |
| 69 | + |
| 70 | + public static float GetSingle(this TableRow row, string id) |
| 71 | + { |
| 72 | + return AValueWithThisIdExists(row, id) && TheValueIsNotEmpty(row, id) |
| 73 | + ? Convert.ToSingle(row[id]) |
| 74 | + : float.MinValue; |
| 75 | + } |
| 76 | + public static char GetChar(this TableRow row, string id) |
| 77 | + { |
| 78 | + return Convert.ToChar(row[id]); |
| 79 | + } |
| 80 | + public static T GetDiscreteEnum<T>(this TableRow row, string id) where T : struct, IConvertible |
| 81 | + { |
| 82 | + var value = row[id].Replace(" ", string.Empty); |
| 83 | + T @enum; |
| 84 | + if (Enum.TryParse(value, true, out @enum)) |
| 85 | + return @enum; |
| 86 | + |
| 87 | + throw new InvalidOperationException($"No enum with value {value} found in enum {typeof(T).Name}"); |
| 88 | + } |
| 89 | + public static T GetDiscreteEnum<T>(this TableRow row, string id, T defaultValue) where T : struct, IConvertible |
| 90 | + { |
| 91 | + var value = row[id].Replace(" ", string.Empty); |
| 92 | + T @enum; |
| 93 | + return Enum.TryParse(value, true, out @enum) ? @enum : defaultValue; |
| 94 | + } |
| 95 | + public static TEnum GetEnumValue<TEnum>(this TableRow row, string id) |
| 96 | + { |
| 97 | + return (TEnum)Enum.Parse(typeof(TEnum), row[id]); |
| 98 | + } |
| 99 | + public static Enum GetEnum<T>(this TableRow row, string id) where T : class |
| 100 | + { |
| 101 | + return GetTheEnumValue<T>(row[id], id); |
| 102 | + } |
| 103 | + private static Enum GetTheEnumValue<T>(string rowValue, string propertyName) where T : class |
| 104 | + { |
| 105 | + var value = rowValue.Replace(" ", string.Empty); |
| 106 | + |
| 107 | + var enumType = GetTheEnumType<T>(propertyName, value); |
| 108 | + |
| 109 | + return Enum.Parse(enumType, value, true) as Enum; |
| 110 | + } |
| 111 | + |
| 112 | + private static Type GetTheEnumType<T>(string propertyName, string value) |
| 113 | + { |
| 114 | + var propertyType = (from property in typeof(T).GetProperties() |
| 115 | + where property.Name == propertyName |
| 116 | + && property.PropertyType.IsEnum |
| 117 | + && EnumValueIsDefinedCaseInsensitve(property.PropertyType, value) |
| 118 | + select property.PropertyType).FirstOrDefault(); |
| 119 | + |
| 120 | + if (propertyType == null) |
| 121 | + throw new InvalidOperationException($"No enum with value {value} found in type {typeof(T).Name}"); |
| 122 | + |
| 123 | + return propertyType; |
| 124 | + } |
| 125 | + private static bool EnumValueIsDefinedCaseInsensitve(Type @enum, string value) |
| 126 | + { |
| 127 | + Enum parsedEnum = null; |
| 128 | + try |
| 129 | + { |
| 130 | + parsedEnum = Enum.Parse(@enum, value, true) as Enum; |
| 131 | + } |
| 132 | + catch |
| 133 | + { |
| 134 | + // just catch it |
| 135 | + } |
| 136 | + |
| 137 | + return parsedEnum != null; |
| 138 | + } |
| 139 | + public static Guid GetGuid(this TableRow row, string id) |
| 140 | + { |
| 141 | + return AValueWithThisIdExists(row, id) && TheValueIsNotEmpty(row, id) |
| 142 | + ? new Guid(row[id]) |
| 143 | + : new Guid(); |
| 144 | + } |
| 145 | + public static double GetDouble(this TableRow row, string id) |
| 146 | + { |
| 147 | + return AValueWithThisIdExists(row, id) && TheValueIsNotEmpty(row, id) |
| 148 | + ? Convert.ToDouble(row[id]) |
| 149 | + : double.MinValue; |
| 150 | + } |
| 151 | + private static bool AValueWithThisIdExists(IEnumerable<KeyValuePair<string, string>> row, string id) |
| 152 | + { |
| 153 | + return row.Any(x => x.Key == id); |
| 154 | + } |
| 155 | + private static bool TheValueIsNotEmpty(TableRow row, string id) |
| 156 | + { |
| 157 | + return string.IsNullOrEmpty(row[id]) == false; |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments