question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ChoCSVRecordReader and ChoCSVRecordWriter can't find property by FieldName

See original GitHub issue

🐞 bug report

Short description

Tried to map the fields of a .tsv using only the FieldName, but it dosen’t work when the FieldName is different from the property name.

Example

Given a .tsv File with headers:

| FirstHeader | Second header |
|-------------|---------------|
| ...         | ...           |
| ...         | ...           |
| ...         | ...           |

and a POCO:

[ChoCSVRecordObject(delimiter: "\t", ErrorMode = ChoErrorMode.ThrowAndStop)]
[ChoCSVFileHeader()]
public class Model
{
    [ChoCSVRecordField(FieldName = "FirstHeader")]
    public string FirstHeader { get; set; }

    [ChoCSVRecordField(FieldName = "Second header")]
    public string SecondHeader { get; set; }
}

The first header works because the property has the same name as the FieldName. But the socond dosen’t and I get an Exeption:

ChoETL.ChoMissingRecordFieldException: 'Missing 'SecondHeader ' property in ConsoleApp.Model type.'

Loking at the code, you can see that it only tries to match the data using the property name https://github.com/Cinchoo/ChoETL/blob/b0d3dfa7c7e9ef55ccb4469136ca5f3dbb942ffc/src/ChoETL/File/CSV/ChoCSVRecordReader.cs#L704-L705

When what it should do is to match usign the FieldName if it is specified. like this:

if (Configuration.PIDict != null)
{
   // if FieldName is set
    if (!string.IsNullOrEmpty(fieldConfig.FieldName))
    {
       // match using FieldName
        Configuration.PIDict.TryGetValue(fieldConfig.FieldName, out pi);
    }
    else
    {
       // otherwise match usign the property name
        Configuration.PIDict.TryGetValue(kvp.Key, out pi);
    }
}

or if you are worried with breaking changes, you could also do this:

if (Configuration.PIDict != null)
{
    // try matching usign the property name
    Configuration.PIDict.TryGetValue(kvp.Key, out pi);

    // if still not found
    if (pi == null)
    {
          // match usign the FieldName
          Configuration.PIDict.TryGetValue(fieldConfig.FieldName, out pi);
    }
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Cinchoocommented, Nov 23, 2020

new packages released to nuget. try it and let me know. thx.

1reaction
Cinchoocommented, Nov 20, 2020

Got it, it is in all reader and writers.

Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found