cassava icon indicating copy to clipboard operation
cassava copied to clipboard

instance {From,To}Field Bool

Open fisx opened this issue 9 years ago • 2 comments

I noticed that Bool doesn't have field instances, and propose to add these.

This PR contains all I need for now, but I'll ramble on a little about alternatives just in case. Feel free to skip to the code and ignore the rest of this text. (-:

I have also thought about making the FromField instance more lenient (accepting 'yes', '1', ...), but didn't like the assymetry and the expected performance penalty. We could give the choice to the library user, though (not type-checked):

import Data.CaseInsensitive

newtype LenientBool = LenientBool { fromLenientBool :: Bool }

instance FromField LenientBool where
    parseField s = pure $ mk s `elem` ["true", "yes", "on", "1"]

instance ToField LenientBool where
    toField (LenientBool True)  = "True"
    toField (LenientBool False) = "False"

Choices:

  • do not depend on CaseInsensitive and just make the list longer? (what about trUE?)
  • make LenientBool less lenient and still reject Essex St. (also reject 300, or read it as True? what about 300.2?)
  • write true, false in order to make the two types distinguishable in the output?

Such a small, simple problem, and so many things to consider! :-)

fisx avatar Dec 31 '15 16:12 fisx

I think this would be nice to have. As #119 notes, there's no standard way to encode booleans in CSVs. Case in point, I'm parsing a file now that uses true and false. I have to use a newtype for that, but it would be nice to be able to encode booleans by default.

tfausak avatar Mar 27 '17 22:03 tfausak

I know that the Python csv-writer happens to encode as the strings True/False as well; I'd be curious to know what other mainstream CSV libraries and programs use as the convention to encode booleans before considering attaching a canonical instance to Bool.

hvr avatar Jun 15 '17 16:06 hvr