< Summary - pva.SuperV

Information
Class: pva.SuperV.Engine.FieldFormatters.EnumFormatter
Assembly: pva.SuperV.Engine
File(s): /home/runner/work/pva.SuperV/pva.SuperV/pva.SuperV.Engine/FieldFormatters/EnumFormatter.cs
Tag: dotnet-ubuntu_18869653307
Line coverage
91%
Covered lines: 52
Uncovered lines: 5
Coverable lines: 57
Total lines: 122
Line coverage: 91.2%
Branch coverage
75%
Covered branches: 9
Total branches: 12
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
get_Values()100%11100%
get_StringsToValues()100%11100%
.ctor()100%11100%
.ctor(...)100%11100%
.ctor(...)100%11100%
CreateStringsToValuesDictionary()50%22100%
ConvertToString(...)83.33%6680%
ConvertFromString(...)87.5%171682.35%

File(s)

/home/runner/work/pva.SuperV/pva.SuperV/pva.SuperV.Engine/FieldFormatters/EnumFormatter.cs

#LineLine coverage
 1using pva.Helpers.Extensions;
 2using pva.SuperV.Engine.Exceptions;
 3
 4namespace 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>
 315        private static readonly HashSet<Type> EnumAllowedTypes =
 316            [
 317            typeof(short),
 318            typeof(ushort),
 319            typeof(int),
 320            typeof(uint),
 321            typeof(long),
 322            typeof(ulong)
 323            ];
 24
 25        /// <summary>
 26        /// Gets or sets the string values of enum.
 27        /// </summary>
 28        /// <value>
 29        /// The values.
 30        /// </value>
 91631        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>
 117139        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>
 344        public EnumFormatter() : base(EnumAllowedTypes)
 345        {
 346        }
 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>
 5353        public EnumFormatter(string enumName, HashSet<string> values) : base(enumName, EnumAllowedTypes)
 5354        {
 5355            int index = 0;
 5356            values.ForEach(value
 15957                => Values.Add(index++, value));
 5358            CreateStringsToValuesDictionary();
 5359        }
 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>
 17466        public EnumFormatter(string enumName, Dictionary<int, string> values) : base(enumName, EnumAllowedTypes)
 17267        {
 17268            Values = values;
 17269            CreateStringsToValuesDictionary();
 17270        }
 71
 72
 73        private void CreateStringsToValuesDictionary()
 22574        {
 22575            Values?.ForEach(entry
 113776                => StringsToValues?.Add(entry.Value, entry.Key));
 22577        }
 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)
 4184        {
 4185            if (value is null)
 086            {
 087                return null;
 88            }
 4189            int longValue = (int)value;
 4190            if (Values!.TryGetValue(longValue, out string? stringValue))
 4091            {
 4092                return stringValue;
 93            }
 194            return $"{longValue} ?";
 4195        }
 96
 97        public override void ConvertFromString(IField field, string? stringValue, DateTime? timestamp, QualityLevel? qua
 2398        {
 2399            if (string.IsNullOrEmpty(stringValue))
 0100            {
 0101                throw new StringConversionException(field.FieldDefinition!.Name, stringValue, field.Type);
 102            }
 23103            if (StringsToValues!.TryGetValue(stringValue, out int convertedValue))
 17104            {
 105                (field switch
 106                {
 10107                    Field<int> typedField => new Action(() => typedField.SetValue(convertedValue, timestamp, quality)),
 6108                    Field<long> typedField => new Action(() => typedField.SetValue(convertedValue, timestamp, quality)),
 6109                    Field<short> typedField => new Action(() => typedField.SetValue((short)convertedValue, timestamp, qu
 4110                    Field<uint> typedField => new Action(() => typedField.SetValue((uint)convertedValue, timestamp, qual
 4111                    Field<ulong> typedField => new Action(() => typedField.SetValue((ulong)convertedValue, timestamp, qu
 4112                    Field<ushort> typedField => new Action(() => typedField.SetValue((ushort)convertedValue, timestamp, 
 0113                    _ => new Action(() => throw new UnhandledFieldTypeException(field.FieldDefinition!.Name, field.Type)
 114                })();
 17115            }
 116            else
 6117            {
 6118                throw new StringConversionException(field.FieldDefinition!.Name, stringValue, [.. StringsToValues.Keys])
 119            }
 17120        }
 121    }
 122}