Handle blank lines in LeafInput

This commit is contained in:
2023-02-27 10:54:43 -05:00
parent 48ab9a19e8
commit 2082eba527
9 changed files with 116 additions and 43 deletions
+8 -9
View File
@@ -10,25 +10,25 @@ namespace LeafWeb.Core.Parsers
private readonly MemoryStream _memoryStream;
protected readonly CsvReader CsvReader;
protected CsvParserBase(FileSystemInfo csvFile)
protected CsvParserBase(FileSystemInfo csvFile, bool ignoreBlankLines)
{
_reader = OpenCsv(csvFile);
CsvReader = InitCsvReader(_reader);
CsvReader = InitCsvReader(_reader, ignoreBlankLines);
}
protected CsvParserBase(byte[] fileContents)
protected CsvParserBase(byte[] fileContents, bool ignoreBlankLines)
{
_memoryStream = new MemoryStream(fileContents);
_reader = new StreamReader(_memoryStream);
CsvReader = InitCsvReader(_reader);
CsvReader = InitCsvReader(_reader, ignoreBlankLines);
}
private static CsvReader InitCsvReader(StreamReader reader)
private static CsvReader InitCsvReader(TextReader reader, bool ignoreBlankLines)
{
var csvReader = new CsvReader(reader);
csvReader.Configuration.HasHeaderRecord = false;
csvReader.Configuration.IgnoreBlankLines = false;
csvReader.Configuration.IgnoreBlankLines = ignoreBlankLines;
csvReader.Configuration.ReadingExceptionOccurred = exception => false;
return csvReader;
}
@@ -53,8 +53,7 @@ namespace LeafWeb.Core.Parsers
public void Dispose()
{
_reader.Dispose();
if (_memoryStream != null)
_memoryStream.Dispose();
}
_memoryStream?.Dispose();
}
}
}
+1
View File
@@ -4,6 +4,7 @@ using System.Linq;
using CsvHelper;
using CsvHelper.Configuration;
using LeafWeb.Core.Entities;
// ReSharper disable IdentifierTypo
namespace LeafWeb.Core.Parsers
{
+2 -2
View File
@@ -8,11 +8,11 @@ namespace LeafWeb.Core.Parsers
{
public class LeafGasComparisonParser : CsvParserBase
{
public LeafGasComparisonParser(FileSystemInfo csvFile) : base(csvFile)
public LeafGasComparisonParser(FileSystemInfo csvFile) : base(csvFile, false)
{
}
public LeafGasComparisonParser(byte[] fileContents) : base(fileContents)
public LeafGasComparisonParser(byte[] fileContents) : base(fileContents, false)
{
}
+11 -4
View File
@@ -9,11 +9,11 @@ namespace LeafWeb.Core.Parsers
{
public class LeafInputCsvParser : CsvParserBase
{
public LeafInputCsvParser(FileSystemInfo csvFile) : base(csvFile)
public LeafInputCsvParser(FileSystemInfo csvFile) : base(csvFile, true)
{
}
public LeafInputCsvParser(byte[] fileContents) : base(fileContents)
public LeafInputCsvParser(byte[] fileContents) : base(fileContents, true)
{
}
@@ -45,9 +45,16 @@ namespace LeafWeb.Core.Parsers
if (!CsvReader.Read())
throw new ParseException("Could not read line number " + CsvReader.Context.Row);
string field;
if (!CsvReader.TryGetField(0, out field))
if (!CsvReader.TryGetField(0, out string field))
throw new ParseException("Could not read first field on line number " + CsvReader.Context.Row);
// skip empty lines
if (string.IsNullOrEmpty(field))
{
i--;
continue;
}
items.Add(field);
}
return ParsedObjectFactory<LeafInputData>.Create(items.ToArray());
+24 -21
View File
@@ -9,7 +9,7 @@ namespace LeafWeb.Core.Utility
{
public static void Register()
{
TypeDescriptor.AddAttributes(typeof(Boolean),
TypeDescriptor.AddAttributes(typeof(bool),
new TypeConverterAttribute(typeof(BoolTypeConverter)));
}
@@ -24,26 +24,29 @@ namespace LeafWeb.Core.Utility
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (value is bool)
return value;
if (value is string)
{
var s = value as string;
if (string.IsNullOrEmpty(s))
return false;
switch (s.Trim().ToUpper())
{
case "TRUE":
case "YES":
case "1":
case "-1":
return true;
switch (value)
{
case bool _:
return value;
case string s:
{
if (string.IsNullOrEmpty(s))
return false;
switch (s.Trim().ToUpper())
{
case "TRUE":
case "YES":
case "1":
case "-1":
return true;
default:
return false;
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
default:
return false;
}
}
default:
return base.ConvertTo(context, culture, value, destinationType);
}
}
}
}
+3 -6
View File
@@ -52,8 +52,7 @@ namespace LeafWeb.Core.Utility
if (property != null)
{
object convertedVal;
if (!TryConvertValue(property, value, out convertedVal))
if (!TryConvertValue(property, value, out var convertedVal))
throw new ParseException($"Cannot convert value '{value}' for {property.Name} at line number {lineNumber}");
property.Set(obj, convertedVal);
}
@@ -83,8 +82,7 @@ namespace LeafWeb.Core.Utility
if (property != null)
{
object convertedVal;
if (!TryConvertValue(property, value, out convertedVal))
if (!TryConvertValue(property, value, out var convertedVal))
throw new ParseException($"Cannot convert value '{value}' for {property.Name} in position {position}");
property.Set(obj, convertedVal);
}
@@ -113,8 +111,7 @@ namespace LeafWeb.Core.Utility
if (property != null)
{
object convertedVal;
if (!TryConvertValue(property, value, out convertedVal))
if (!TryConvertValue(property, value, out var convertedVal))
throw new ParseException($"Cannot convert value '{value}' for {property.Name} in position {position}");
property.Set(obj, convertedVal);
}