| | | 1 | | using pva.SuperV.Engine.Exceptions; |
| | | 2 | | using pva.SuperV.Engine.FieldFormatters; |
| | | 3 | | using pva.SuperV.Engine.Processing; |
| | | 4 | | using System.Globalization; |
| | | 5 | | using System.Text; |
| | | 6 | | |
| | | 7 | | namespace 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 | | { |
| | 10261 | 29 | | get => _name; |
| | | 30 | | set |
| | 3835 | 31 | | { |
| | 3835 | 32 | | _name = value; |
| | 3835 | 33 | | IdentifierValidation.ValidateIdentifier("field", value); |
| | 3832 | 34 | | } |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Gets or sets the type of the field. |
| | | 39 | | /// </summary> |
| | | 40 | | /// <value> |
| | | 41 | | /// The type. |
| | | 42 | | /// </value> |
| | 5800 | 43 | | 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> |
| | 5470 | 51 | | 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> |
| | 7050 | 59 | | 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> |
| | 8461 | 67 | | 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> |
| | 109 | 73 | | public FieldDefinition(string name) : this(name, default) |
| | 109 | 74 | | { |
| | 109 | 75 | | } |
| | | 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 |
| | 3835 | 83 | | 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 |
| | 3835 | 85 | | { |
| | 3835 | 86 | | Name = name; |
| | 3832 | 87 | | DefaultValue = defaultValue ?? default; |
| | 3832 | 88 | | Type = typeof(T); |
| | 3832 | 89 | | } |
| | | 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() |
| | 2192 | 96 | | { |
| | 2192 | 97 | | StringBuilder codeBuilder = new(); |
| | 2192 | 98 | | codeBuilder.AppendLine( |
| | 2192 | 99 | | $"public Field<{typeof(T)}> {Name} {{ get; set; }} = new ({GetCodeValueForNew(DefaultValue)});"); |
| | 2192 | 100 | | return codeBuilder.ToString(); |
| | 2192 | 101 | | } |
| | | 102 | | |
| | | 103 | | private static string? GetCodeValueForNew(T? defaultValue) |
| | 2192 | 104 | | { |
| | 2192 | 105 | | if (typeof(T).Equals(typeof(string))) |
| | 218 | 106 | | { |
| | 218 | 107 | | return $"\"{defaultValue}\""; |
| | | 108 | | } |
| | | 109 | | else |
| | 1974 | 110 | | { |
| | | 111 | | |
| | 1974 | 112 | | return defaultValue switch |
| | 1974 | 113 | | { |
| | 110 | 114 | | bool boolValue => boolValue ? "true" : "false", |
| | 110 | 115 | | DateTime dateTimeValue => $"new {typeof(T)}({dateTimeValue.Ticks.ToString(CultureInfo.InvariantCultu |
| | 110 | 116 | | TimeSpan timespanValue => $"new {typeof(T)}({timespanValue.Ticks.ToString(CultureInfo.InvariantCultu |
| | 110 | 117 | | float floatValue => $"{floatValue.ToString(CultureInfo.InvariantCulture)}F", |
| | 115 | 118 | | double doubleValue => $"{doubleValue.ToString(CultureInfo.InvariantCulture)}", |
| | 1419 | 119 | | _ => defaultValue!.ToString(), |
| | 1974 | 120 | | }; |
| | | 121 | | } |
| | 2192 | 122 | | } |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// Clones this instance. |
| | | 126 | | /// </summary> |
| | | 127 | | /// <returns></returns> |
| | | 128 | | IFieldDefinition IFieldDefinition.Clone() |
| | 720 | 129 | | { |
| | 720 | 130 | | FieldDefinition<T> fieldDefinition = new(Name, DefaultValue) |
| | 720 | 131 | | { |
| | 720 | 132 | | Formatter = Formatter, |
| | 720 | 133 | | ValuePostChangeProcessings = [.. ValuePostChangeProcessings] |
| | 720 | 134 | | }; |
| | 720 | 135 | | return fieldDefinition; |
| | 720 | 136 | | } |
| | | 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) |
| | 4 | 145 | | { |
| | 4 | 146 | | if (fieldDefinitionUpdate is FieldDefinition<T> typedFieldDefinitionUpdate) |
| | 2 | 147 | | { |
| | 2 | 148 | | DefaultValue = typedFieldDefinitionUpdate.DefaultValue; |
| | 2 | 149 | | Formatter = fieldFormatter; |
| | 2 | 150 | | return; |
| | | 151 | | } |
| | 2 | 152 | | throw new WrongFieldTypeException(Name, Type, fieldDefinitionUpdate.Type); |
| | 2 | 153 | | } |
| | | 154 | | } |
| | | 155 | | } |