Update
This commit is contained in:
47
ConsoleApp2/Services/JsvSource.cs
Normal file
47
ConsoleApp2/Services/JsvSource.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using ConsoleApp2.Entities;
|
||||
using ConsoleApp2.Helpers;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using ServiceStack.Text;
|
||||
|
||||
namespace ConsoleApp2.Services;
|
||||
|
||||
public class JsvSource : IDisposable
|
||||
{
|
||||
private readonly string _filePath;
|
||||
private readonly JsvStringSerializer _jsv;
|
||||
private readonly StreamReader _reader;
|
||||
// ReSharper disable once PrivateFieldCanBeConvertedToLocalVariable
|
||||
private readonly ILogger? _logger;
|
||||
private readonly string _tableName;
|
||||
|
||||
public DataRecord Current { get; protected set; } = null!;
|
||||
public string[]? Headers { get; }
|
||||
public bool EndOfSource => _reader.EndOfStream;
|
||||
|
||||
public JsvSource(string filePath, string[]? headers = null, ILogger? logger = null)
|
||||
{
|
||||
_filePath = filePath;
|
||||
_jsv = new JsvStringSerializer();
|
||||
_reader = new StreamReader(filePath);
|
||||
Headers = headers;
|
||||
_logger = logger;
|
||||
// _logger?.LogInformation("Reading file: {FilePath}", filePath);
|
||||
_tableName = DumpDataHelper.GetTableName(filePath);
|
||||
}
|
||||
|
||||
public async ValueTask<bool> ReadAsync()
|
||||
{
|
||||
var str = await _reader.ReadLineAsync();
|
||||
if (string.IsNullOrEmpty(str))
|
||||
return false;
|
||||
var fields = _jsv.DeserializeFromString<string[]>(str);
|
||||
|
||||
Current = new DataRecord(fields, _tableName, Headers);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_reader.Dispose();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user