cassava
cassava copied to clipboard
instance {From,To}Field Bool
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 rejectEssex St
. (also reject300
, or read it asTrue
? what about300.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! :-)
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.
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
.