| | | 1 | | using pva.SuperV.Common; |
| | | 2 | | using pva.SuperV.Engine.Exceptions; |
| | | 3 | | using System.Globalization; |
| | | 4 | | |
| | | 5 | | namespace pva.SuperV.Engine |
| | | 6 | | { |
| | | 7 | | public static class FieldValueSetter |
| | | 8 | | { |
| | | 9 | | public static void SetValue<T1>(IField field, T1 value, DateTime? timestamp, QualityLevel? quality) |
| | 151 | 10 | | { |
| | 151 | 11 | | if (field.Type.IsAssignableFrom(value?.GetType())) |
| | 89 | 12 | | { |
| | 89 | 13 | | ((Field<T1>)field).SetValue(value, timestamp, quality); |
| | 89 | 14 | | return; |
| | | 15 | | } |
| | 62 | 16 | | else if (value is string stringValue) |
| | 62 | 17 | | { |
| | 62 | 18 | | if (field.FieldDefinition?.Formatter is not null) |
| | 27 | 19 | | { |
| | 27 | 20 | | field.FieldDefinition?.Formatter.ConvertFromString(field, stringValue, timestamp, quality); |
| | 21 | 21 | | } |
| | | 22 | | else |
| | 35 | 23 | | { |
| | | 24 | | (field switch |
| | | 25 | | { |
| | 8 | 26 | | Field<bool> typedField => new Action(() => typedField.SetValue(ConvertToBool(field.FieldDefiniti |
| | 6 | 27 | | Field<DateTime> typedField => new Action(() => typedField.SetValue(ConvertToDateTime(field.Field |
| | 6 | 28 | | Field<double> typedField => new Action(() => typedField.SetValue(ConvertToDouble(field.FieldDefi |
| | 6 | 29 | | Field<float> typedField => new Action(() => typedField.SetValue(ConvertToFloat(field.FieldDefini |
| | 6 | 30 | | Field<int> typedField => new Action(() => typedField.SetValue(ConvertToInt(field.FieldDefinition |
| | 6 | 31 | | Field<long> typedField => new Action(() => typedField.SetValue(ConvertToLong(field.FieldDefiniti |
| | 6 | 32 | | Field<short> typedField => new Action(() => typedField.SetValue(ConvertToShort(field.FieldDefini |
| | 6 | 33 | | Field<TimeSpan> typedField => new Action(() => typedField.SetValue(ConvertToTimeSpan(field.Field |
| | 6 | 34 | | Field<uint> typedField => new Action(() => typedField.SetValue(ConvertToUint(field.FieldDefiniti |
| | 6 | 35 | | Field<ulong> typedField => new Action(() => typedField.SetValue(ConvertToUlong(field.FieldDefini |
| | 6 | 36 | | Field<ushort> typedField => new Action(() => typedField.SetValue(ConvertToUshort(field.FieldDefi |
| | 2 | 37 | | _ => new Action(() => throw new UnhandledFieldTypeException(field.FieldDefinition!.Name, field.T |
| | | 38 | | })(); |
| | 23 | 39 | | } |
| | 44 | 40 | | return; |
| | | 41 | | } |
| | 0 | 42 | | throw new UnhandledFieldTypeException(field.FieldDefinition!.Name, field.Type); |
| | 133 | 43 | | } |
| | | 44 | | |
| | | 45 | | private static bool ConvertToBool(string fieldName, string stringValue) |
| | 4 | 46 | | => Boolean.TryParse(stringValue, out bool result) |
| | 4 | 47 | | ? result |
| | 4 | 48 | | : throw new StringConversionException(fieldName, stringValue, typeof(bool)); |
| | | 49 | | |
| | | 50 | | private static DateTime ConvertToDateTime(string fieldName, string stringValue) |
| | 3 | 51 | | => DateTime.TryParse(stringValue, CultureInfo.InvariantCulture, out DateTime result) |
| | 3 | 52 | | ? result.ToUniversalTime() |
| | 3 | 53 | | : throw new StringConversionException(fieldName, stringValue, typeof(DateTime)); |
| | | 54 | | |
| | | 55 | | private static double ConvertToDouble(string fieldName, string stringValue) |
| | 3 | 56 | | => double.TryParse(stringValue, NumberStyles.Float, CultureInfo.InvariantCulture, out double result) |
| | 3 | 57 | | ? result |
| | 3 | 58 | | : throw new StringConversionException(fieldName, stringValue, typeof(double)); |
| | | 59 | | |
| | | 60 | | private static float ConvertToFloat(string fieldName, string stringValue) |
| | 3 | 61 | | => float.TryParse(stringValue, NumberStyles.Float, CultureInfo.InvariantCulture, out float result) |
| | 3 | 62 | | ? result |
| | 3 | 63 | | : throw new StringConversionException(fieldName, stringValue, typeof(float)); |
| | | 64 | | |
| | | 65 | | private static int ConvertToInt(string fieldName, string stringValue) |
| | 3 | 66 | | => int.TryParse(stringValue, out int result) |
| | 3 | 67 | | ? result |
| | 3 | 68 | | : throw new StringConversionException(fieldName, stringValue, typeof(int)); |
| | | 69 | | |
| | | 70 | | private static long ConvertToLong(string fieldName, string stringValue) |
| | 3 | 71 | | => long.TryParse(stringValue, out long result) |
| | 3 | 72 | | ? result |
| | 3 | 73 | | : throw new StringConversionException(fieldName, stringValue, typeof(long)); |
| | | 74 | | |
| | | 75 | | private static short ConvertToShort(string fieldName, string stringValue) |
| | 3 | 76 | | => short.TryParse(stringValue, out short result) |
| | 3 | 77 | | ? result |
| | 3 | 78 | | : throw new StringConversionException(fieldName, stringValue, typeof(short)); |
| | | 79 | | |
| | | 80 | | private static TimeSpan ConvertToTimeSpan(string fieldName, string stringValue) |
| | 3 | 81 | | => TimeSpan.TryParse(stringValue, CultureInfo.InvariantCulture, out TimeSpan result) |
| | 3 | 82 | | ? result |
| | 3 | 83 | | : throw new StringConversionException(fieldName, stringValue, typeof(TimeSpan)); |
| | | 84 | | |
| | | 85 | | private static uint ConvertToUint(string fieldName, string stringValue) |
| | 3 | 86 | | => uint.TryParse(stringValue, out uint result) |
| | 3 | 87 | | ? result |
| | 3 | 88 | | : throw new StringConversionException(fieldName, stringValue, typeof(uint)); |
| | | 89 | | |
| | | 90 | | private static ulong ConvertToUlong(string fieldName, string stringValue) |
| | 3 | 91 | | => ulong.TryParse(stringValue, out ulong result) |
| | 3 | 92 | | ? result |
| | 3 | 93 | | : throw new StringConversionException(fieldName, stringValue, typeof(ulong)); |
| | | 94 | | |
| | | 95 | | private static ushort ConvertToUshort(string fieldName, string stringValue) |
| | 3 | 96 | | => ushort.TryParse(stringValue, out ushort result) |
| | 3 | 97 | | ? result |
| | 3 | 98 | | : throw new StringConversionException(fieldName, stringValue, typeof(ushort)); |
| | | 99 | | } |
| | | 100 | | } |