public CsvMappingResult <string[]> Map(TokenizedRow values) { return(new CsvMappingResult <string[]> { RowIndex = values.Index, Result = values.Tokens }); }
public bool TryMapValue(TEntity entity, TokenizedRow value) { // TODO Better Error Handling is a must! try { action(entity, value); return(true); } catch { return(false); } }
public CsvMappingResult <TEntity> Map(TokenizedRow values) { TEntity entity = new TEntity(); for (int pos = 0; pos < csvPropertyMappings.Count; pos++) { var indexToPropertyMapping = csvPropertyMappings[pos]; var columnIndex = indexToPropertyMapping.ColumnIndex; if (columnIndex >= values.Tokens.Length) { return(new CsvMappingResult <TEntity> { RowIndex = values.Index, Error = new CsvMappingError { ColumnIndex = columnIndex, Value = $"Column {columnIndex} is Out Of Range", UnmappedRow = string.Join("|", values.Tokens) } }); } var value = values.Tokens[columnIndex]; if (!indexToPropertyMapping.PropertyMapping.TryMapValue(entity, value)) { return(new CsvMappingResult <TEntity> { RowIndex = values.Index, Error = new CsvMappingError { ColumnIndex = columnIndex, Value = $"Column {columnIndex} with Value '{value}' cannot be converted", UnmappedRow = string.Join("|", values.Tokens) } }); } } return(new CsvMappingResult <TEntity> { RowIndex = values.Index, Result = entity }); }
private bool ShapeMapper(MyCsvRowEntity inProgressEntity, TokenizedRow rowData) { // NOTE: Be defensive and validate every piece of data you consume, // don't do things that will raise exceptions. (This same rule would // apply with any TypeConverter you write)... This gets called for // every row in your csv; if exceptions get raised within the loop // EVEN if you catch them), your performance will suffer dramatically! // We could also check this with tokens[0], but since we already mapped // it via MapProperty, we'll use the value on the entity that we're in // the process of building. if (inProgressEntity.ShapeName == "square") { var subMap = squareMap.Map(rowData); if (subMap.IsValid) { inProgressEntity.Shape = subMap.Result; } return(subMap.IsValid); } if (inProgressEntity.ShapeName == "triangle") { var subMap = triangleMap.Map(rowData); if (subMap.IsValid) { inProgressEntity.Shape = subMap.Result; } return(subMap.IsValid); } // NOTE: There are two possible strategies here. One, you can return true // which will allow any *OTHER* Row Mappings to run and see if they can make // sense of the data from this row. Maybe you have a whole separate mapper // for Circles & Ellipses, etc. Alternatively, you can return false, which // will cause this row to be marked as Invalid, SKIP any other Row Mappings, // and allow parsing to continue to subsequent rows. return(false); }
private bool MapCustomHeaders(DnsServer server, TokenizedRow headers) { for (int headerIndex = 0; headerIndex < _customHeaders.Length; headerIndex++) { string headerName = _customHeaders[headerIndex]; string headerValue = headers.Tokens.ElementAtOrDefault(headerIndex); if (headerValue == null) { throw new Exception(string.Format(i18n.dug.ER_Unable_To_Get_Value_From_Header_X_Expected_At_Index_X, headerName, headerIndex)); } var setterFunction = TemplateHelper.ServerSetterMap[headerName]; // This should always be present, its validation in UpdateOptions.cs try{ setterFunction(server, headerValue); } catch { throw new Exception(string.Format(i18n.dug.ER_Unable_Set_Field_X_To_Value_X, headerName, headerValue)); } } return(true); }
public CsvMappingResult <TEntity> Map(TokenizedRow values) { TEntity entity = new TEntity(); // Iterate over Index Mappings: for (int pos = 0; pos < csvIndexPropertyMappings.Count; pos++) { var indexToPropertyMapping = csvIndexPropertyMappings[pos]; var columnIndex = indexToPropertyMapping.ColumnIndex; if (columnIndex >= values.Tokens.Length) { return(new CsvMappingResult <TEntity> { RowIndex = values.Index, Error = new CsvMappingError { ColumnIndex = columnIndex, Value = $"Column {columnIndex} is Out Of Range", UnmappedRow = string.Join("|", values.Tokens) } }); } var value = values.Tokens[columnIndex]; if (!indexToPropertyMapping.PropertyMapping.TryMapValue(entity, value)) { return(new CsvMappingResult <TEntity> { RowIndex = values.Index, Error = new CsvMappingError { ColumnIndex = columnIndex, Value = $"Column {columnIndex} with Value '{value}' cannot be converted", UnmappedRow = string.Join("|", values.Tokens) } }); } } // Iterate over Range Mappings: for (int pos = 0; pos < csvRangePropertyMappings.Count; pos++) { var rangeToPropertyMapping = csvRangePropertyMappings[pos]; var range = rangeToPropertyMapping.Range; // Copy the Sub Array. This needs optimization, like ReadOnlyMemory! var slice = values.Tokens.Skip(range.Start).Take(range.Length).ToArray(); if (!rangeToPropertyMapping.PropertyMapping.TryMapValue(entity, slice)) { var columnIndex = range.Start; return(new CsvMappingResult <TEntity> { RowIndex = values.Index, Error = new CsvMappingError { ColumnIndex = columnIndex, Value = $"Range with Start Index {range.Start} and End Index {range.End} cannot be converted!", UnmappedRow = string.Join("|", values.Tokens) } }); } } // Iterate over Row Mappings. At this point previous values for the entity // should be set: for (int pos = 0; pos < csvRowMappings.Count; pos++) { var csvRowMapping = csvRowMappings[pos]; if (!csvRowMapping.TryMapValue(entity, values)) { return(new CsvMappingResult <TEntity> { RowIndex = values.Index, Error = new CsvMappingError { Value = $"Row could not be mapped!", UnmappedRow = string.Join("|", values.Tokens) } }); } } return(new CsvMappingResult <TEntity> { RowIndex = values.Index, Result = entity }); }
public bool TryMapValue(TEntity entity, TokenizedRow value) { return(action(entity, value)); }