public void Date_Read() { // Arrange DataItemProperty property = new DataItemProperty() { DataType = typeof(DateTime), Name = "DateTimeTest", OrdinalPosition = -1, Size = 0, Pattern = "dd MMM yyyy" }; DataItemDefinition definition = new DataItemDefinition() { }; // Act Object rawValue = DataFormatHelper.ReadData("23 Oct 1984", property, definition); DateTime value = (rawValue != DBNull.Value) ? (DateTime)rawValue : DateTime.MinValue; // Assert Assert.True(value != DateTime.MinValue && value.Day == 23 && value.Month == 10 && value.Year == 1984); }
private static Boolean GetField <T>(CsvReader csvReader, DataItemProperty property, Type overridingDataType, out T value) { Object tempValue = DBNull.Value; // The temporary value before it is cast Boolean response = false; // Successful? // Try and get the value from either the oridinal position or by the // column name Int32 calculatedPosition = (property.OrdinalPosition != -1) ? property.OrdinalPosition : Array.IndexOf(csvReader.Context.HeaderRecord, property.Name); //response = csvReader.TryGetField(overridingDataType, calculatedPosition, out tempValue); try { tempValue = csvReader.GetField(calculatedPosition); response = true; } catch { } // Return the value casted to the required type value = (T)tempValue; // Return if it was successful return(response); }
/// <summary> /// Get the data from a field /// </summary> /// <param name="csvReader">The reader to handle the property get</param> /// <param name="property">The property data</param> /// <returns>If it was successful</returns> private static Boolean GetPropertyValue(CsvReader csvReader, DataItemProperty property, DataItemDefinition definition, ref Object value) { // Get the proeprty type as some types of data need handling differently straight away String propertyType = property.DataType.ToShortName(); // Get the raw data Boolean fieldFound = GetField <String>(csvReader, property, typeof(String), out String rawValue); if (fieldFound) { value = DataFormatHelper.ReadData(DataFormatHelper.CleanString(rawValue, csvReader.Configuration.Quote), property, definition); } // Return the data return(fieldFound); }
private DataItem CreateDataItem(DataItemProperty property, string id) { DataItemCategory category; if (property.PropertyAttribute is SampleDataItemAttribute) { category = DataItemCategory.Sample; } else if (property.PropertyAttribute is EventDataItemAttribute) { category = DataItemCategory.Event; } else { if (Debugger.IsAttached) { Debugger.Break(); } throw new NotSupportedException(); } var valueType = property.PropertyInfo.PropertyType; var type = property.PropertyAttribute.ItemType; var name = property.PropertyInfo.Name; var dataItem = new DataItem(category, type, name, id); dataItem.Writable = property.PropertyInfo.CanWrite; dataItem.ValueType = valueType; if (property.PropertyAttribute.ItemSubType != DataItemSubtype.NONE) { dataItem.SubType = property.PropertyAttribute.ItemSubType.ToString(); } dataItem.UUID = property.PropertyAttribute.UUID; dataItem.Source = property.PropertyAttribute.Source; dataItem.SignificantDigits = property.PropertyAttribute.SignificantDigits; dataItem.NativeUnits = property.PropertyAttribute.NativeUnits; dataItem.NativeScale = property.PropertyAttribute.NativeScale; dataItem.CoordinateSystem = property.PropertyAttribute.CoordianteSystem; dataItem.Units = property.PropertyAttribute.Units; return(dataItem); }
public void Boolean_Read() { // Arrange DataItemProperty property = new DataItemProperty() { DataType = typeof(Boolean), Name = "BooleanTest", OrdinalPosition = -1, Size = 0 }; DataItemDefinition definition = new DataItemDefinition() { }; // Act Object rawValue = DataFormatHelper.ReadData("true", property, definition); Boolean value = (rawValue != DBNull.Value) ? (Boolean)rawValue : false; // Assert Assert.True(value); }
public void String_Read() { // Arrange DataItemProperty property = new DataItemProperty() { DataType = typeof(String), Name = "StringTest", OrdinalPosition = -1, Size = 0 }; DataItemDefinition definition = new DataItemDefinition() { }; // Act Object value = DataFormatHelper.ReadData("This Is A String", property, definition); // Assert Assert.True(value != DBNull.Value && (String)value == "This Is A String"); }
/// <summary> /// Get a field of type T from the current csv reader row /// </summary> /// <typeparam name="T">The response type of the data</typeparam> /// <param name="reader">The CSV reader to read the dat from</param> /// <param name="dataType">The </param> /// <param name="value"></param> /// <returns></returns> private static Boolean GetField <T>(CsvReader csvReader, DataItemProperty property, out T value) => GetField <T>(csvReader, property, property.DataType, out value);
set => SetValue(DataItemProperty, value);
/// <summary> /// Write some data to a specific patterm /// </summary> /// <param name="value">The value to be transformed</param> /// <param name="property">The property definition for the data (aka the column definition)</param> /// <param name="definition">The definitiion of the entire data set that this belongs to</param> /// <returns>The data formatted in a specific pattern and cast to a string</returns> public static String WriteData(Object value, DataItemProperty property, DataItemDefinition definition) { String result = ""; // The formatted result that will be returned // Get the property type as some types of data need handling differently straight away if (value != null) { String propertyType = property.DataType.ToShortName(); switch (propertyType) { case "boolean": case "bool": try { result = Boolean.Parse(value.ToString()).ToString(); } catch { result = "0"; } break; case "double": case "int": case "unint": case "int16": case "int32": case "int64": case "float": case "decimal": case "single": case "byte": case "sbyte": case "short": case "ushort": case "long": case "ulong": try { result = value.ToString(); } catch { result = "0"; } break; case "datetime": // Not a null value try { if (value != DBNull.Value) { DateTime dateTime = (DateTime)value; // Cast the date so we don't have to do it each time // Do we have a manual property pattern or just leave it to the culture? if ((property.Pattern ?? "") != "") { result = dateTime.ToString(property.Pattern); } else { result = dateTime.ToString(definition.Culture); } } else { result = ""; } } catch { result = ""; } break; default: try { result = (String)value ?? ""; } catch { result = ""; } break; } } return(result); // Send the formatted result back }
/// <summary> /// Read some data from a specific pattern /// </summary> /// <param name="value">The value to read the data from</param> /// <param name="property">The property definition for the data (aka the column definition)</param> /// <param name="definition">The definitiion of the entire data set that this belongs to</param> /// <returns>The data formatted as the appropriate type</returns> public static Object ReadData(String value, DataItemProperty property, DataItemDefinition definition) { Object result = DBNull.Value; // The formatted result that will be returned // Get the property type as some types of data need handling differently straight away if (value != null) { String propertyType = property.DataType.ToShortName(); switch (propertyType) { case "boolean": case "bool": // Get the first character of the raw data if there is some Char firstChar = (value.Length > 0) ? value.ToLower().ToCharArray()[0] : ' '; // Check the first character to see if it matches a true state result = booleanChars.Contains(firstChar); break; case "double": case "int": case "unint": case "int16": case "int32": case "int64": case "float": case "decimal": case "single": case "byte": case "sbyte": case "short": case "ushort": case "long": case "ulong": // Clean the string up for parsing try { if (IsNumeric(value)) { result = value; } } catch { result = (Int32)0; } break; case "datetime": // If a specific pattern has been provided then use that, otherwise use the culuture information provided DateTime formattedDate = DateTime.MinValue; // Do we have a manual property pattern or just leave it to the culture? try { if ((property.Pattern ?? "") != "") { formattedDate = DateTime.ParseExact(value, (property.Pattern ?? ""), CultureInfo.InvariantCulture); } else { formattedDate = DateTime.Parse(value, definition.Culture); } // Anything found? If so set it if (formattedDate != DateTime.MinValue) { result = formattedDate; } else { result = DBNull.Value; } } catch { } break; default: result = value; break; } } return(result); }