Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix errors #22

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.";
}
}
}