jehugaleahsa / FlatFiles

Reads and writes CSV, fixed-length and other flat file formats with a focus on schema definition, configuration and speed.
The Unlicense
357 stars 64 forks source link

Handling Null Values for Booleans #20

Closed ghost closed 7 years ago

ghost commented 7 years ago

I don't think the Boolean type mapping property can handle nulls :(

The BooleanColumn parse method will return a null, and when it tries to cast that there is a cast exception.

Shouldn't the default for Null be False?

jehugaleahsa commented 7 years ago

I just created 6 new unit tests, 3 for CSV and 3 for fixed-length files. They test round-tripping (reading/writing) boolean? with true, false and null. Could you provide more details or some sample code illustrating the issue?

ghost commented 7 years ago

Thanks! Sorry I'm sure there are other bugs in my code that is causing strange null reference exceptions.

My question is that, if I'm using a TypeMapper that maps a column to a bool or int type in my model, and the reader encounters an "N/A", which I've mapped to a NullValue, then does the parser use a default value of false or 0 for non-nullable types?

jehugaleahsa commented 7 years ago

Ah, I see. The parser will return null, not zero. So when the type mapper goes to convert it to a non-nullable type it fails to cast.

It might make more sense to make the property nullable and then when you refer to that property, use the null coalesce operator, e.g., a.IsActive ?? false.

Thanks, Travis

On Jun 2, 2017 7:09 PM, "KJ Moon" notifications@github.com wrote:

Thanks! Sorry I'm sure there are other bugs in my code that is causing strange null reference exceptions.

My question is that, if I'm using a TypeMapper that maps a column to a bool or int type, and the reader encounters an "N/A", which I've mapped to a NullValue, then does the parser use a default value of false or 0 for non-nullable types?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/jehugaleahsa/FlatFiles/issues/20#issuecomment-305928866, or mute the thread https://github.com/notifications/unsubscribe-auth/ABTgPgaaw0Nu_19k_ioeupsQcVeXLO-eks5sAJYfgaJpZM4Nu0Lc .