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