Skip to content

Commit

Permalink
Merge pull request #22 from LuigimonSoft/Fix/LC-Several-errors
Browse files Browse the repository at this point in the history
Fix errors
  • Loading branch information
LuigimonSoft authored Feb 23, 2024
2 parents 7cf8cc9 + c435e95 commit 23d89a8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
50 changes: 46 additions & 4 deletions CsvDataMapper.Core/Services/CsvService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,29 @@ public CsvService(ICsvRepository csvRepository, char delimiter, bool hasHeader)

while ((line = _csvRepository.ReadCsvFileLine()) != null)
{
if (line.Contains("[") && line.Contains("]") )
{
int ini = line.IndexOf("[");
int end = line.IndexOf("]");
string substring = line.Substring(ini, end - ini + 1);
substring = substring.Replace(",", "|");
line = line.Replace(line.Substring(ini, end - ini + 1), substring);
}
string[] Datacolumns = line.Split(_delimiter);
if(Datacolumns.Contains("[") && Datacolumns.Contains("]"))
{
for (int i = 0; i < Datacolumns.Length; i++)
{
if (Datacolumns[i].Contains("[") && Datacolumns[i].Contains("]"))
{
int ini = Datacolumns[i].IndexOf("[");
int end = Datacolumns[i].IndexOf("]");
string substring = Datacolumns[i].Substring(ini, end - ini + 1);
substring = substring.Replace("|", ",");
Datacolumns[i] = Datacolumns[i].Replace(Datacolumns[i].Substring(ini, end - ini + 1), substring);
}
}
}

if (_hasHeader)
{
Expand Down Expand Up @@ -126,7 +148,7 @@ public CsvService(ICsvRepository csvRepository, char delimiter, bool hasHeader)
{
string? line = string.Empty;
string[] columnsHeaders = Array.Empty<string>();
var model = new TModel();

var properties = typeof(TModel).GetProperties();
var columns = new List<CsvColumn>();

Expand Down Expand Up @@ -158,9 +180,21 @@ public CsvService(ICsvRepository csvRepository, char delimiter, bool hasHeader)

while ((line = await _csvRepository.ReadCsvFileLineAsync()) != null)
{
string[] Datacolumns = line.Split(_delimiter);
var model = new TModel();
string[] Datacolumns =new string[] { };
if (line.Contains("[") && line.Contains("]"))
{
int ini = line.IndexOf("[");
int end = line.IndexOf("]");
string substring = line.Substring(ini, end - ini + 1);
substring = substring.Replace(",", "|");
line = line.Replace(line.Substring(ini, end - ini + 1), substring);

if(_hasHeader)
}

Datacolumns = line.Split(_delimiter);

if (_hasHeader)
{
for (int i = 0; i < columns.Count; i++)
{
Expand All @@ -179,7 +213,15 @@ public CsvService(ICsvRepository csvRepository, char delimiter, bool hasHeader)
}
else
{
column.PropertyInfo.SetValue(model, Convert.ChangeType(Datacolumns[i], column.PropertyInfo.PropertyType, CultureInfo.InvariantCulture));
if (Datacolumns[i].Contains("[") && Datacolumns[i].Contains("]"))
{
Datacolumns[i] = Datacolumns[i].Replace("[", "");
Datacolumns[i] = Datacolumns[i].Replace("]", "").Replace("\"","");
var dataArray = Datacolumns[i].Trim().Count() > 0 ? Datacolumns[i].Split("|").ToList() : new List<string>();
column.PropertyInfo.SetValue(model, dataArray);
}
else
column.PropertyInfo.SetValue(model, Convert.ChangeType(Datacolumns[i], column.PropertyInfo.PropertyType, CultureInfo.InvariantCulture));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions CsvDataMapper.Core/Utils/CsvDataMapperException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace CsvDataMapper.Core.Utils
public class CsvDataMapperException: Exception
{
public ErrorCode ErrorCode { get; }
private static readonly ResourceManager _resourceManager = new ResourceManager("CsvDataMapper.Core.Resources.ErrorMessages", typeof(ErrorMessages).Assembly);
private static readonly ResourceManager _resourceManager = new ResourceManager("CsvDataMapper.Core.Utils.ErrorMessages", typeof(ErrorMessages).Assembly);

public CsvDataMapperException(ErrorCode errorCode) : base(GetErrorMessage(errorCode))
{
Expand All @@ -25,7 +25,7 @@ public CsvDataMapperException(ErrorCode errorCode, Exception innerException) : b

private static string GetErrorMessage(ErrorCode errorCode)
{
return _resourceManager.GetString(errorCode.ToString(), CultureInfo.CurrentCulture) ?? "Unknown error.";
return _resourceManager.GetString(((int)errorCode).ToString(), CultureInfo.CurrentCulture) ?? "Unknown error.";
}
}
}

0 comments on commit 23d89a8

Please sign in to comment.