Exemplo n.º 1
0
        /// <summary>
        /// Override to provide custom logic of body processing
        /// </summary>
        /// <returns></returns>
        protected virtual async Task ProcessBodyAsync(ICsvRowConvertionStrategy <TModel> csvConverter)
        {
            Result = new List <TModel>();

            await foreach (var bodyRow in _dataProvider.GetBodyAsync().ConfigureAwait(false))
            {
                var model = csvConverter.Convert(bodyRow.Columns);
                Result.Add(model);
            }
        }
Exemplo n.º 2
0
        protected override async Task ProcessBodyAsync(ICsvRowConvertionStrategy <TModel> csvConverter)
        {
            var iterator = _dataProvider.GetIterator();

            if (iterator == null)
            {
                throw new CsvParserException("csv iterator is not set");
            }

            var rowSplitter = _dataProvider.GetRowSplitter() ?? new QuotesSensitiveRowSplitter();

            while (await iterator.ReadLineAsync().ConfigureAwait(false))
            {
                var columns = rowSplitter.SplitRow <CsvColumn>(iterator.GetValue(), _dataProvider.Separator, true);
                var model   = csvConverter.Convert(columns);

                _lineProcessedCallback.Invoke(iterator.CurrentRow, model);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Process csv data source using <see cref="ICsvRowConvertionStrategy<TModel>"/> strategy
        /// Parser extracts header of csv data source and initialize csvConverter
        /// </summary>
        /// <param name="csvConverter">csv converter</param>
        /// <param name="cleanUpResources">clean up resource after obtaining data</param>
        /// <returns></returns>
        public async Task <bool> ProcessAsync(
            ICsvRowConvertionStrategy <TModel> csvConverter,
            bool cleanUpResources = true)
        {
            if (_dataProvider == null)
            {
                throw new CsvParserException("data Provider is not set");
            }

            var iterator = _dataProvider.GetIterator();

            try
            {
                if (await iterator.StartAsync().ConfigureAwait(false))
                {
                    var header = await _dataProvider
                                 .GetHeaderAsync(true)
                                 .ConfigureAwait(false);

                    // initialize csv converter by header row if it has not been initialized before
                    var isInitialized = csvConverter.IsInitialized || csvConverter.Initialize(header);

                    if (!isInitialized)
                    {
                        throw new CsvParserException("can not initialize csv converter");
                    }

                    await ProcessBodyAsync(csvConverter).ConfigureAwait(false);
                }
            }
            finally
            {
                await iterator.StopAsync().ConfigureAwait(false);

                if (cleanUpResources)
                {
                    this.Dispose();
                }
            }

            return(true);
        }