| | | 1 | | using pva.SuperV.Engine.Exceptions; |
| | | 2 | | using System.Text.Json.Serialization; |
| | | 3 | | |
| | | 4 | | namespace pva.SuperV.Engine.FieldFormatters |
| | | 5 | | { |
| | | 6 | | /// <summary> |
| | | 7 | | /// Base class for <see cref="Field{T}"/> formatting. If adding new formatters, they need to be added as JsonDerived |
| | | 8 | | /// </summary> |
| | | 9 | | [JsonDerivedType(typeof(EnumFormatter), typeDiscriminator: "Enum")] |
| | | 10 | | public abstract class FieldFormatter |
| | | 11 | | { |
| | 230 | 12 | | private readonly string _name = String.Empty; |
| | | 13 | | |
| | | 14 | | public string Name |
| | | 15 | | { |
| | 555 | 16 | | get => _name; |
| | | 17 | | init |
| | 230 | 18 | | { |
| | 230 | 19 | | IdentifierValidation.ValidateIdentifier("field value formatter", value); |
| | 228 | 20 | | _name = value; |
| | 228 | 21 | | } |
| | | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Gets or sets the allowed types. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <value> |
| | | 28 | | /// The allowed types. |
| | | 29 | | /// </value> |
| | | 30 | | [JsonIgnore] |
| | 532 | 31 | | public HashSet<Type> AllowedTypes { get; init; } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Initializes a new instance of the <see cref="FieldFormatter"/> class. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="name">The name.</param> |
| | | 37 | | /// <param name="allowedTypes">The allowed types.</param> |
| | 227 | 38 | | protected FieldFormatter(string name, HashSet<Type> allowedTypes) |
| | 227 | 39 | | { |
| | 227 | 40 | | Name = name; |
| | 225 | 41 | | AllowedTypes = allowedTypes; |
| | 225 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Initializes a new instance of the <see cref="FieldFormatter"/> class. |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="allowedTypes">The allowed types.</param> |
| | 3 | 48 | | protected FieldFormatter(HashSet<Type> allowedTypes) |
| | 3 | 49 | | { |
| | 3 | 50 | | AllowedTypes = allowedTypes; |
| | 3 | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Validates that type is part of the allowed types of formatter. |
| | | 55 | | /// </summary> |
| | | 56 | | /// <param name="fieldType">Type of the field.</param> |
| | | 57 | | /// <exception cref="InvalidTypeForFormatterException"></exception> |
| | | 58 | | internal void ValidateAllowedType(Type fieldType) |
| | 291 | 59 | | { |
| | 291 | 60 | | if (!AllowedTypes.Contains(fieldType)) |
| | 1 | 61 | | { |
| | 1 | 62 | | throw new InvalidTypeForFormatterException(fieldType, Name!); |
| | | 63 | | } |
| | 290 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Converts a value to string. |
| | | 68 | | /// </summary> |
| | | 69 | | /// <param name="value">The value.</param> |
| | | 70 | | /// <returns>String representing the value.</returns> |
| | | 71 | | public abstract string? ConvertToString(dynamic? value); |
| | | 72 | | |
| | | 73 | | public abstract void ConvertFromString(IField field, string? stringValue, DateTime? timestamp, QualityLevel? qua |
| | | 74 | | } |
| | | 75 | | } |