< Summary - pva.SuperV

Information
Class: pva.SuperV.Engine.FieldDefinition<T>
Assembly: pva.SuperV.Engine
File(s): /home/runner/work/pva.SuperV/pva.SuperV/pva.SuperV.Engine/FieldDefinition.cs
Tag: dotnet-ubuntu_18869653307
Line coverage
100%
Covered lines: 55
Uncovered lines: 0
Coverable lines: 55
Total lines: 155
Line coverage: 100%
Branch coverage
90%
Covered branches: 18
Total branches: 20
Branch coverage: 90%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Name()100%11100%
set_Name(...)100%11100%
get_Type()100%11100%
get_Formatter()100%11100%
get_DefaultValue()100%11100%
get_ValuePostChangeProcessings()100%11100%
.ctor(...)100%11100%
.ctor(...)100%22100%
GetCode()100%11100%
GetCodeValueForNew(...)87.5%1616100%
pva.SuperV.Engine.IFieldDefinition.Clone()100%11100%
Update(...)100%22100%

File(s)

/home/runner/work/pva.SuperV/pva.SuperV/pva.SuperV.Engine/FieldDefinition.cs

#LineLine coverage
 1using pva.SuperV.Engine.Exceptions;
 2using pva.SuperV.Engine.FieldFormatters;
 3using pva.SuperV.Engine.Processing;
 4using System.Globalization;
 5using System.Text;
 6
 7namespace pva.SuperV.Engine
 8{
 9    /// <summary>
 10    /// Definition of a field used in a <see cref="Class"/>.
 11    /// </summary>
 12    /// <typeparam name="T"></typeparam>
 13    /// <seealso cref="pva.SuperV.Engine.IFieldDefinition" />
 14    public class FieldDefinition<T> : IFieldDefinition
 15    {
 16        /// <summary>
 17        /// The name of the field. Uses <see cref="Name"/> to access value.
 18        /// </summary>
 19        private string _name;
 20
 21        /// <summary>
 22        /// Gets or sets the name of the field.
 23        /// </summary>
 24        /// <value>
 25        /// The name.
 26        /// </value>
 27        public string Name
 28        {
 1026129            get => _name;
 30            set
 383531            {
 383532                _name = value;
 383533                IdentifierValidation.ValidateIdentifier("field", value);
 383234            }
 35        }
 36
 37        /// <summary>
 38        /// Gets or sets the type of the field.
 39        /// </summary>
 40        /// <value>
 41        /// The type.
 42        /// </value>
 580043        public Type Type { get; init; }
 44
 45        /// <summary>
 46        /// Gets or sets the formatter associated with field.
 47        /// </summary>
 48        /// <value>
 49        /// The formatter.
 50        /// </value>
 547051        public FieldFormatter? Formatter { get; set; }
 52
 53        /// <summary>
 54        /// Gets or sets the default value for fields in instances.
 55        /// </summary>
 56        /// <value>
 57        /// The default value.
 58        /// </value>
 705059        public T? DefaultValue { get; set; }
 60
 61        /// <summary>
 62        /// Gets or sets the value post change processings.
 63        /// </summary>
 64        /// <value>
 65        /// The value post change processings.
 66        /// </value>
 846167        public List<IFieldValueProcessing> ValuePostChangeProcessings { get; set; } = [];
 68
 69        /// <summary>
 70        /// Initializes a new instance of the <see cref="FieldDefinition{T}"/> class.
 71        /// </summary>
 72        /// <param name="name">The name of the field.</param>
 10973        public FieldDefinition(string name) : this(name, default)
 10974        {
 10975        }
 76
 77        /// <summary>
 78        /// Initializes a new instance of the <see cref="FieldDefinition{T}"/> class.
 79        /// </summary>
 80        /// <param name="name">The name of the field.</param>
 81        /// <param name="defaultValue">The default value for fields.</param>
 82#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider ad
 383583        public FieldDefinition(string name, T? defaultValue)
 84#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider ad
 383585        {
 383586            Name = name;
 383287            DefaultValue = defaultValue ?? default;
 383288            Type = typeof(T);
 383289        }
 90
 91        /// <summary>
 92        /// Gets the C# code to create field of an <see cref="Class"/>.
 93        /// </summary>
 94        /// <returns>C# code for field.</returns>
 95        public string GetCode()
 219296        {
 219297            StringBuilder codeBuilder = new();
 219298            codeBuilder.AppendLine(
 219299                $"public Field<{typeof(T)}> {Name} {{ get; set; }} = new ({GetCodeValueForNew(DefaultValue)});");
 2192100            return codeBuilder.ToString();
 2192101        }
 102
 103        private static string? GetCodeValueForNew(T? defaultValue)
 2192104        {
 2192105            if (typeof(T).Equals(typeof(string)))
 218106            {
 218107                return $"\"{defaultValue}\"";
 108            }
 109            else
 1974110            {
 111
 1974112                return defaultValue switch
 1974113                {
 110114                    bool boolValue => boolValue ? "true" : "false",
 110115                    DateTime dateTimeValue => $"new {typeof(T)}({dateTimeValue.Ticks.ToString(CultureInfo.InvariantCultu
 110116                    TimeSpan timespanValue => $"new {typeof(T)}({timespanValue.Ticks.ToString(CultureInfo.InvariantCultu
 110117                    float floatValue => $"{floatValue.ToString(CultureInfo.InvariantCulture)}F",
 115118                    double doubleValue => $"{doubleValue.ToString(CultureInfo.InvariantCulture)}",
 1419119                    _ => defaultValue!.ToString(),
 1974120                };
 121            }
 2192122        }
 123
 124        /// <summary>
 125        /// Clones this instance.
 126        /// </summary>
 127        /// <returns></returns>
 128        IFieldDefinition IFieldDefinition.Clone()
 720129        {
 720130            FieldDefinition<T> fieldDefinition = new(Name, DefaultValue)
 720131            {
 720132                Formatter = Formatter,
 720133                ValuePostChangeProcessings = [.. ValuePostChangeProcessings]
 720134            };
 720135            return fieldDefinition;
 720136        }
 137
 138        /// <summary>
 139        /// Update field from another field.
 140        /// </summary>
 141        /// <param name="fieldDefinitionUpdate">Field from which to update. Only default value and formatter are copied.
 142        /// <param name="fieldFormatter">Formatter</param>
 143        /// <exception cref="WrongFieldTypeException"></exception>
 144        public void Update(IFieldDefinition fieldDefinitionUpdate, FieldFormatter? fieldFormatter)
 4145        {
 4146            if (fieldDefinitionUpdate is FieldDefinition<T> typedFieldDefinitionUpdate)
 2147            {
 2148                DefaultValue = typedFieldDefinitionUpdate.DefaultValue;
 2149                Formatter = fieldFormatter;
 2150                return;
 151            }
 2152            throw new WrongFieldTypeException(Name, Type, fieldDefinitionUpdate.Type);
 2153        }
 154    }
 155}