private static void ValidateProcessorDefinition(FileProcessorDefinition20 processorDefinition) { if (processorDefinition is null) { throw new ArgumentNullException(nameof(processorDefinition)); } ParsedDataProcessorHelper.ValidateRowProcessorDefinition("Header", processorDefinition.HeaderRowProcessorDefinition); ParsedDataProcessorHelper.ValidateRowProcessorDefinition("Trailer", processorDefinition.TrailerRowProcessorDefinition); ValidateDataRowProcessorDefinitions(processorDefinition); }
public ParsedDataProcessor20(IDataSource <ParserContext20> source, FileProcessorDefinition20 fileProcessorDefinition) : base(source, fileProcessorDefinition.HeaderRowProcessorDefinition.FieldProcessorDefinitions.Length > 0, fileProcessorDefinition.TrailerRowProcessorDefinition.FieldProcessorDefinitions.Length > 0) { ValidateProcessorDefinition(fileProcessorDefinition); _fileProcessorDefinition = fileProcessorDefinition; _source.BeforeProcessRow += SourceBeforeProcessRow; _source.AfterProcessRow += SourceAfterProcessRow; _source.ProcessField += SourceProcessField; }
public static FileProcessorDefinition20 CreateFileProcessorDefinition(InputDefinitionFile20 inputDefinitionFile_20) { var aggregateManager = new AggregateManager(); var processorDefinition = new FileProcessorDefinition20(); InitializeFileProcessorDefinition(processorDefinition, inputDefinitionFile_20, aggregateManager); processorDefinition.DataRowProcessorDefinitions = LoadRowProcessorDefinitions(inputDefinitionFile_20.Datas, aggregateManager); processorDefinition.KeyField = inputDefinitionFile_20.Datas.KeyField; processorDefinition.DataTypeField = inputDefinitionFile_20.Datas.DataTypeField; ResolveDataTypeField(processorDefinition); var fieldProcessorDefinitionsInDataRows = processorDefinition.DataRowProcessorDefinitions.SelectMany(a => a.Value.RowProcessorDefinition.FieldProcessorDefinitions); InitializeRules(fieldProcessorDefinitionsInDataRows.SelectMany(a => a.Rules), aggregateManager.GetAggregates()); InitializeRules(processorDefinition.TrailerRowProcessorDefinition.FieldProcessorDefinitions.SelectMany(a => a.Rules), aggregateManager.GetAggregates()); return(processorDefinition); }
private static void ResolveDataTypeField(FileProcessorDefinition20 processorDefinition) { if (string.IsNullOrWhiteSpace(processorDefinition.DataTypeField)) { throw new InvalidOperationException("Invalid DataTypeField"); } foreach (var kvp in processorDefinition.DataRowProcessorDefinitions) { kvp.Value.DataTypeFieldIndex = GetFieldIndexByName(kvp.Value.RowProcessorDefinition, processorDefinition.DataTypeField); if (kvp.Value.DataTypeFieldIndex == -1) { throw new InvalidOperationException($"DataTypeField '{processorDefinition.DataTypeField}' must be present in every data definition"); } kvp.Value.DataKeyFieldIndex = GetFieldIndexByName(kvp.Value.RowProcessorDefinition, processorDefinition.KeyField); if (kvp.Value.DataKeyFieldIndex == -1) { throw new InvalidOperationException($"KeyField '{processorDefinition.KeyField}' must be present in every data definition"); } } }
public void Initialize() { _fileDataSource = TestHelpers.CreateFileDataSource <ParserContext20>("test-file-data-trailer.20.csv", false); _textDecoder = new TextDecoder { Pattern = @"*.", FailValidationResult = ValidationResultType.Critical }; _dataType1 = new DataRowProcessorDefinition { DataTypeFieldIndex = 0, RowProcessorDefinition = new RowProcessorDefinition { FieldProcessorDefinitions = new FieldProcessorDefinition[] { new FieldProcessorDefinition { Decoder = _textDecoder, FieldName = "DataType", Description = "DT1 Field A" }, new FieldProcessorDefinition { Decoder = _textDecoder, FieldName = "key-field", Description = "DT1 Field B" }, new FieldProcessorDefinition { Decoder = _textDecoder, FieldName = "DT1-Field-c", Description = "DT1 Field C" } } } }; _dataType2 = new DataRowProcessorDefinition { DataTypeFieldIndex = 0, RowProcessorDefinition = new RowProcessorDefinition { FieldProcessorDefinitions = new FieldProcessorDefinition[] { new FieldProcessorDefinition { Decoder = _textDecoder, FieldName = "DataType", Description = "DT2 Field A" }, new FieldProcessorDefinition { Decoder = _textDecoder, FieldName = "key-field", Description = "DT2 Field B" }, new FieldProcessorDefinition { Decoder = _textDecoder, FieldName = "DT2-Field-c", Description = "DT2 Field C" }, new FieldProcessorDefinition { Decoder = _textDecoder, FieldName = "DT2-Field-d", Description = "DT2 Field D" } } } }; _trailer = new RowProcessorDefinition { FieldProcessorDefinitions = new FieldProcessorDefinition[] { new FieldProcessorDefinition { Decoder = _textDecoder, FieldName = "Field-TA" }, new FieldProcessorDefinition { Decoder = _textDecoder, FieldName = "Field-TB" } } }; _fileProcessorDefinition = new FileProcessorDefinition20 { DataTypeField = "FieldA", KeyField = "key-field", HeaderRowProcessorDefinition = new RowProcessorDefinition { FieldProcessorDefinitions = new FieldProcessorDefinition[] { }, }, TrailerRowProcessorDefinition = _trailer, DataRowProcessorDefinitions = new Dictionary <string, DataRowProcessorDefinition> { { "dt1", _dataType1 }, { "dt2", _dataType2 } } }; }
private static KeyValuePair <string, DataRowProcessorDefinition> FindDataRowProcessorDefinition(FileProcessorDefinition20 fileProcessorDefinition, ProcessRowEventArgs <ParserContext20> e) { foreach (var kvp in fileProcessorDefinition.DataRowProcessorDefinitions) { if (kvp.Value.DataTypeFieldIndex >= e.Row.RawFields.Length) { continue; } if (e.Row.RawFields[kvp.Value.DataTypeFieldIndex] == kvp.Key) { return(kvp); } } return(default);