< 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_22190969454
Line coverage
100%
Covered lines: 56
Uncovered lines: 0
Coverable lines: 56
Total lines: 172
Line coverage: 100%
Branch coverage
86%
Covered branches: 19
Total branches: 22
Branch coverage: 86.3%
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_TopicName()100%11100%
get_FieldValueChangedEventChannel()100%11100%
get_ValuePostChangeProcessings()100%11100%
.ctor(...)100%11100%
.ctor(...)75%44100%
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;
 6using System.Text.Json.Serialization;
 7using System.Threading.Channels;
 8
 9namespace pva.SuperV.Engine
 10{
 11    /// <summary>
 12    /// Definition of a field used in a <see cref="Class"/>.
 13    /// </summary>
 14    /// <typeparam name="T">Type of field</typeparam>
 15    /// <seealso cref="pva.SuperV.Engine.IFieldDefinition" />
 16    public class FieldDefinition<T> : IFieldDefinition
 17    {
 18        /// <summary>
 19        /// The name of the field. Uses <see cref="Name"/> to access value.
 20        /// </summary>
 21        private string _name;
 22
 23        /// <summary>
 24        /// Gets or sets the name of the field.
 25        /// </summary>
 26        /// <value>
 27        /// The name.
 28        /// </value>
 29        public string Name
 30        {
 2497331            get => _name;
 32            set
 463433            {
 463434                _name = IdentifierValidation.ValidateIdentifier("field", value);
 463135            }
 36        }
 37
 38        /// <summary>
 39        /// Gets or sets the type of the field.
 40        /// </summary>
 41        /// <value>
 42        /// The type.
 43        /// </value>
 875644        public Type Type { get; init; }
 45
 46        /// <summary>
 47        /// Gets or sets the formatter associated with field.
 48        /// </summary>
 49        /// <value>
 50        /// The formatter.
 51        /// </value>
 661452        public FieldFormatter? Formatter { get; set; }
 53
 54        /// <summary>
 55        /// Gets or sets the default value for fields in instances.
 56        /// </summary>
 57        /// <value>
 58        /// The default value.
 59        /// </value>
 860460        public T? DefaultValue { get; set; }
 61
 62        /// <summary>
 63        /// Gets or sets the name of the topic to trigger script(s) when the value changes.
 64        /// </summary>
 65        /// <value>
 66        /// The name of the topic.
 67        /// </value>
 1092468        public string? TopicName { get; set; }
 69
 70        /// <summary>
 71        /// Gets or sets the field value changed event channel.
 72        /// </summary>
 73        /// <value>
 74        /// The field value changed event.
 75        /// </value>
 76        [JsonIgnore]
 232777        public Channel<FieldValueChangedEvent>? FieldValueChangedEventChannel { get; set; }
 78
 79        /// <summary>
 80        /// Gets or sets the value post change processings.
 81        /// </summary>
 82        /// <value>
 83        /// The value post change processings.
 84        /// </value>
 2837485        public List<IFieldValueProcessing> ValuePostChangeProcessings { get; set; } = [];
 86
 87        /// <summary>
 88        /// Initializes a new instance of the <see cref="FieldDefinition{T}"/> class with the initial field value set to
 89        /// </summary>
 90        /// <param name="name">The name of the field.</param>
 10991        public FieldDefinition(string name) : this(name, default, "")
 10992        {
 10993        }
 94
 95        /// <summary>
 96        /// Initializes a new instance of the <see cref="FieldDefinition{T}"/> class.
 97        /// </summary>
 98        /// <param name="name">The name of the field.</param>
 99        /// <param name="defaultValue">The default initial value for instances field.</param>
 100        /// <param name="topicName">Name of topic to publish to when field value changes.Can be null if no publishing is
 101#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider ad
 4634102        public FieldDefinition(string name, T? defaultValue, string? topicName = "")
 103#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider ad
 4634104        {
 4634105            Name = name;
 4631106            DefaultValue = defaultValue ?? default;
 4631107            TopicName = topicName?.Trim();
 4631108            Type = typeof(T);
 4631109        }
 110
 111        /// <summary>
 112        /// Gets the C# code to create field for an <see cref="Instance"/> of a <see cref="Class"/>.
 113        /// </summary>
 114        /// <returns>C# code for field.</returns>
 115        public string GetCode()
 2635116        {
 2635117            StringBuilder codeBuilder = new();
 2635118            codeBuilder.AppendLine(
 2635119                $"public Field<{typeof(T)}> {Name} {{ get; set; }} = new ({GetCodeValueForNew(DefaultValue)});");
 2635120            return codeBuilder.ToString();
 2635121        }
 122
 123        private static string? GetCodeValueForNew(T? defaultValue)
 2635124        {
 2635125            if (typeof(T).Equals(typeof(string)))
 234126            {
 234127                return $"\"{defaultValue}\"";
 128            }
 129
 2401130            return defaultValue switch
 2401131            {
 118132                bool boolValue => boolValue ? "true" : "false",
 118133                DateTime dateTimeValue => $"new {typeof(T)}({dateTimeValue.Ticks.ToString(CultureInfo.InvariantCulture)}
 118134                TimeSpan timespanValue => $"new {typeof(T)}({timespanValue.Ticks.ToString(CultureInfo.InvariantCulture)}
 123135                float floatValue => $"{floatValue.ToString(CultureInfo.InvariantCulture)}F",
 123136                double doubleValue => $"{doubleValue.ToString(CultureInfo.InvariantCulture)}",
 1801137                _ => defaultValue!.ToString(),
 2401138            };
 2635139        }
 140
 141        /// <summary>
 142        /// Clones this instance.
 143        /// </summary>
 144        /// <returns>Cloned field definition</returns>
 145        IFieldDefinition IFieldDefinition.Clone()
 836146            => (FieldDefinition<T>)new(Name, DefaultValue)
 836147            {
 836148                Formatter = Formatter,
 836149                TopicName = TopicName,
 836150                FieldValueChangedEventChannel = FieldValueChangedEventChannel,
 836151                ValuePostChangeProcessings = [.. ValuePostChangeProcessings]
 836152            };
 153
 154        /// <summary>
 155        /// Update field from another field.
 156        /// </summary>
 157        /// <param name="fieldDefinitionUpdate">Field from which to update. Only default value and formatter are copied.
 158        /// <param name="fieldFormatter">Formatter</param>
 159        /// <exception cref="WrongFieldTypeException"></exception>
 160        public void Update(IFieldDefinition fieldDefinitionUpdate, FieldFormatter? fieldFormatter)
 4161        {
 4162            if (fieldDefinitionUpdate is FieldDefinition<T> typedFieldDefinitionUpdate)
 2163            {
 2164                DefaultValue = typedFieldDefinitionUpdate.DefaultValue;
 2165                Formatter = fieldFormatter;
 2166                TopicName = fieldDefinitionUpdate.TopicName;
 2167                return;
 168            }
 2169            throw new WrongFieldTypeException(Name, Type, fieldDefinitionUpdate.Type);
 2170        }
 171    }
 172}