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:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
No results found
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
new packages released to nuget. try it and let me know. thx.
Got it, it is in all reader and writers.