private static int GetFieldIndexByName(RowProcessorDefinition rowProcessorDefinition, string name)
        {
            for (int i = 0; i < rowProcessorDefinition.FieldProcessorDefinitions.Length; i++)
            {
                var fieldProcessorDefinition = rowProcessorDefinition.FieldProcessorDefinitions[i];
                if (fieldProcessorDefinition.FieldName == name)
                {
                    return(i);
                }
            }

            return(-1);
        }
Пример #2
0
        public static void ValidateRowProcessorDefinition(string lineType, RowProcessorDefinition rowProcessorDefinition)
        {
            if (rowProcessorDefinition is null)
            {
                throw new ArgumentNullException($"{lineType} - {nameof(rowProcessorDefinition)}");
            }

            if (rowProcessorDefinition.FieldProcessorDefinitions is null)
            {
                throw new ArgumentNullException($"{lineType} - {nameof(rowProcessorDefinition.FieldProcessorDefinitions)}");
            }

            for (int i = 0; i < rowProcessorDefinition.FieldProcessorDefinitions.Length; i++)
            {
                var fieldProcessorDefinition = rowProcessorDefinition.FieldProcessorDefinitions[i];
                if (fieldProcessorDefinition.Decoder == null)
                {
                    throw new ArgumentNullException($"{lineType} - {nameof(fieldProcessorDefinition.Decoder)}");
                }
            }
        }
Пример #3
0
        public static bool ValidateNumerOfFields(string lineType, Row row, RowProcessorDefinition rowProcessorDefinition)
        {
            if (rowProcessorDefinition is null)
            {
                throw new ArgumentNullException(nameof(rowProcessorDefinition));
            }

            if (rowProcessorDefinition.FieldProcessorDefinitions is null)
            {
                throw new ArgumentNullException(nameof(rowProcessorDefinition.FieldProcessorDefinitions));
            }

            if (rowProcessorDefinition.FieldProcessorDefinitions.Length == row.RawFields.Length)
            {
                return(true);
            }

            row.ValidationResult = ValidationResultType.Error;
            var error = $"{lineType} - The expected number of fields {rowProcessorDefinition.FieldProcessorDefinitions.Length} is not equal to the actual number of fields {row.RawFields.Length}";

            row.Errors.Add(error);
            return(false);
        }
        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 }
                }
            };
        }