< 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_22190969454
Line coverage
91%
Covered lines: 52
Uncovered lines: 5
Coverable lines: 57
Total lines: 123
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.Common;
 3using pva.SuperV.Engine.Exceptions;
 4
 5namespace pva.SuperV.Engine.FieldFormatters
 6{
 7    /// <summary>
 8    /// Enum formatter for integer fields. Allows to display the string representation according to a list of strings.
 9    /// </summary>
 10    /// <seealso cref="FieldFormatter" />
 11    public class EnumFormatter : FieldFormatter
 12    {
 13        /// <summary>
 14        /// The allowed types for Enum formatter.
 15        /// </summary>
 316        private static readonly HashSet<Type> EnumAllowedTypes =
 317            [
 318            typeof(short),
 319            typeof(ushort),
 320            typeof(int),
 321            typeof(uint),
 322            typeof(long),
 323            typeof(ulong)
 324            ];
 25
 26        /// <summary>
 27        /// Gets or sets the string values of enum.
 28        /// </summary>
 29        /// <value>
 30        /// The values.
 31        /// </value>
 98232        public Dictionary<int, string>? Values { get; set; } = [];
 33
 34        /// <summary>
 35        /// Gets or sets the string values of enum.
 36        /// </summary>
 37        /// <value>
 38        /// The values.
 39        /// </value>
 125340        private Dictionary<string, int>? StringsToValues { get; set; } = [];
 41
 42        /// <summary>
 43        /// Initializes a new instance of the <see cref="EnumFormatter"/> class. Used by JSON deserializer.
 44        /// </summary>
 345        public EnumFormatter() : base(EnumAllowedTypes)
 346        {
 347        }
 48
 49        /// <summary>
 50        /// Initializes a new instance of the <see cref="EnumFormatter"/> class with values starting at 0.
 51        /// </summary>
 52        /// <param name="enumName">Name of the enum.</param>
 53        /// <param name="values">The string values of enum.</param>
 5354        public EnumFormatter(string enumName, HashSet<string> values) : base(enumName, EnumAllowedTypes)
 5355        {
 5356            int index = 0;
 5357            values.ForEach(value
 15958                => Values.Add(index++, value));
 5359            CreateStringsToValuesDictionary();
 5360        }
 61
 62        /// <summary>
 63        /// Initializes a new instance of the <see cref="EnumFormatter"/> class with strng values and their associated i
 64        /// </summary>
 65        /// <param name="enumName">Name of the enum.</param>
 66        /// <param name="values">The value pairs (int and string).</param>
 18767        public EnumFormatter(string enumName, Dictionary<int, string> values) : base(enumName, EnumAllowedTypes)
 18568        {
 18569            Values = values;
 18570            CreateStringsToValuesDictionary();
 18571        }
 72
 73
 74        private void CreateStringsToValuesDictionary()
 23875        {
 23876            Values?.ForEach(entry
 121577                => StringsToValues?.Add(entry.Value, entry.Key));
 23878        }
 79        /// <summary>
 80        /// Converts a value to string.
 81        /// </summary>
 82        /// <param name="value">The (int) value.</param>
 83        /// <returns>String representation of value. If int value is not found in <see cref="Values"/>, the int value wi
 84        public override string? ConvertToString(dynamic? value)
 6885        {
 6886            if (value is null)
 087            {
 088                return null;
 89            }
 6890            int longValue = (int)value;
 6891            if (Values!.TryGetValue(longValue, out string? stringValue))
 6792            {
 6793                return stringValue;
 94            }
 195            return $"{longValue} ?";
 6896        }
 97
 98        public override void ConvertFromString(IField field, string? stringValue, DateTime? timestamp, QualityLevel? qua
 2799        {
 27100            if (string.IsNullOrEmpty(stringValue))
 0101            {
 0102                throw new StringConversionException(field.FieldDefinition!.Name, stringValue, field.Type);
 103            }
 27104            if (StringsToValues!.TryGetValue(stringValue, out int convertedValue))
 21105            {
 106                (field switch
 107                {
 18108                    Field<int> typedField => new Action(() => typedField.SetValue(convertedValue, timestamp, quality)),
 6109                    Field<long> typedField => new Action(() => typedField.SetValue(convertedValue, timestamp, quality)),
 6110                    Field<short> typedField => new Action(() => typedField.SetValue((short)convertedValue, timestamp, qu
 4111                    Field<uint> typedField => new Action(() => typedField.SetValue((uint)convertedValue, timestamp, qual
 4112                    Field<ulong> typedField => new Action(() => typedField.SetValue((ulong)convertedValue, timestamp, qu
 4113                    Field<ushort> typedField => new Action(() => typedField.SetValue((ushort)convertedValue, timestamp, 
 0114                    _ => new Action(() => throw new UnhandledFieldTypeException(field.FieldDefinition!.Name, field.Type)
 115                })();
 21116            }
 117            else
 6118            {
 6119                throw new StringConversionException(field.FieldDefinition!.Name, stringValue, [.. StringsToValues.Keys])
 120            }
 21121        }
 122    }
 123}