| | | 1 | | using pva.Helpers.Extensions; |
| | | 2 | | using pva.SuperV.Engine.Exceptions; |
| | | 3 | | |
| | | 4 | | namespace pva.SuperV.Engine.FieldFormatters |
| | | 5 | | { |
| | | 6 | | /// <summary> |
| | | 7 | | /// Enum formatter for integer fields. Allows to display the string representation according to a list of strings. |
| | | 8 | | /// </summary> |
| | | 9 | | /// <seealso cref="FieldFormatter" /> |
| | | 10 | | public class EnumFormatter : FieldFormatter |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// The allowed types for Enum formatter. |
| | | 14 | | /// </summary> |
| | 3 | 15 | | private static readonly HashSet<Type> EnumAllowedTypes = |
| | 3 | 16 | | [ |
| | 3 | 17 | | typeof(short), |
| | 3 | 18 | | typeof(ushort), |
| | 3 | 19 | | typeof(int), |
| | 3 | 20 | | typeof(uint), |
| | 3 | 21 | | typeof(long), |
| | 3 | 22 | | typeof(ulong) |
| | 3 | 23 | | ]; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Gets or sets the string values of enum. |
| | | 27 | | /// </summary> |
| | | 28 | | /// <value> |
| | | 29 | | /// The values. |
| | | 30 | | /// </value> |
| | 916 | 31 | | public Dictionary<int, string>? Values { get; set; } = []; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Gets or sets the string values of enum. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <value> |
| | | 37 | | /// The values. |
| | | 38 | | /// </value> |
| | 1171 | 39 | | private Dictionary<string, int>? StringsToValues { get; set; } = []; |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Initializes a new instance of the <see cref="EnumFormatter"/> class. Used by JSON deserializer. |
| | | 43 | | /// </summary> |
| | 3 | 44 | | public EnumFormatter() : base(EnumAllowedTypes) |
| | 3 | 45 | | { |
| | 3 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Initializes a new instance of the <see cref="EnumFormatter"/> class with values starting at 0. |
| | | 50 | | /// </summary> |
| | | 51 | | /// <param name="enumName">Name of the enum.</param> |
| | | 52 | | /// <param name="values">The string values of enum.</param> |
| | 53 | 53 | | public EnumFormatter(string enumName, HashSet<string> values) : base(enumName, EnumAllowedTypes) |
| | 53 | 54 | | { |
| | 53 | 55 | | int index = 0; |
| | 53 | 56 | | values.ForEach(value |
| | 159 | 57 | | => Values.Add(index++, value)); |
| | 53 | 58 | | CreateStringsToValuesDictionary(); |
| | 53 | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Initializes a new instance of the <see cref="EnumFormatter"/> class with strng values and their associated i |
| | | 63 | | /// </summary> |
| | | 64 | | /// <param name="enumName">Name of the enum.</param> |
| | | 65 | | /// <param name="values">The value pairs (int and string).</param> |
| | 174 | 66 | | public EnumFormatter(string enumName, Dictionary<int, string> values) : base(enumName, EnumAllowedTypes) |
| | 172 | 67 | | { |
| | 172 | 68 | | Values = values; |
| | 172 | 69 | | CreateStringsToValuesDictionary(); |
| | 172 | 70 | | } |
| | | 71 | | |
| | | 72 | | |
| | | 73 | | private void CreateStringsToValuesDictionary() |
| | 225 | 74 | | { |
| | 225 | 75 | | Values?.ForEach(entry |
| | 1137 | 76 | | => StringsToValues?.Add(entry.Value, entry.Key)); |
| | 225 | 77 | | } |
| | | 78 | | /// <summary> |
| | | 79 | | /// Converts a value to string. |
| | | 80 | | /// </summary> |
| | | 81 | | /// <param name="value">The (int) value.</param> |
| | | 82 | | /// <returns>String representation of value. If int value is not found in <see cref="Values"/>, the int value wi |
| | | 83 | | public override string? ConvertToString(dynamic? value) |
| | 41 | 84 | | { |
| | 41 | 85 | | if (value is null) |
| | 0 | 86 | | { |
| | 0 | 87 | | return null; |
| | | 88 | | } |
| | 41 | 89 | | int longValue = (int)value; |
| | 41 | 90 | | if (Values!.TryGetValue(longValue, out string? stringValue)) |
| | 40 | 91 | | { |
| | 40 | 92 | | return stringValue; |
| | | 93 | | } |
| | 1 | 94 | | return $"{longValue} ?"; |
| | 41 | 95 | | } |
| | | 96 | | |
| | | 97 | | public override void ConvertFromString(IField field, string? stringValue, DateTime? timestamp, QualityLevel? qua |
| | 23 | 98 | | { |
| | 23 | 99 | | if (string.IsNullOrEmpty(stringValue)) |
| | 0 | 100 | | { |
| | 0 | 101 | | throw new StringConversionException(field.FieldDefinition!.Name, stringValue, field.Type); |
| | | 102 | | } |
| | 23 | 103 | | if (StringsToValues!.TryGetValue(stringValue, out int convertedValue)) |
| | 17 | 104 | | { |
| | | 105 | | (field switch |
| | | 106 | | { |
| | 10 | 107 | | Field<int> typedField => new Action(() => typedField.SetValue(convertedValue, timestamp, quality)), |
| | 6 | 108 | | Field<long> typedField => new Action(() => typedField.SetValue(convertedValue, timestamp, quality)), |
| | 6 | 109 | | Field<short> typedField => new Action(() => typedField.SetValue((short)convertedValue, timestamp, qu |
| | 4 | 110 | | Field<uint> typedField => new Action(() => typedField.SetValue((uint)convertedValue, timestamp, qual |
| | 4 | 111 | | Field<ulong> typedField => new Action(() => typedField.SetValue((ulong)convertedValue, timestamp, qu |
| | 4 | 112 | | Field<ushort> typedField => new Action(() => typedField.SetValue((ushort)convertedValue, timestamp, |
| | 0 | 113 | | _ => new Action(() => throw new UnhandledFieldTypeException(field.FieldDefinition!.Name, field.Type) |
| | | 114 | | })(); |
| | 17 | 115 | | } |
| | | 116 | | else |
| | 6 | 117 | | { |
| | 6 | 118 | | throw new StringConversionException(field.FieldDefinition!.Name, stringValue, [.. StringsToValues.Keys]) |
| | | 119 | | } |
| | 17 | 120 | | } |
| | | 121 | | } |
| | | 122 | | } |