Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why does showTextData convert everything to lower case #82

Open
tysonzero opened this issue Sep 24, 2018 · 3 comments
Open

Why does showTextData convert everything to lower case #82

tysonzero opened this issue Sep 24, 2018 · 3 comments
Labels

Comments

@tysonzero
Copy link

I was hoping readTextData and showTextData would form a nice bijection, so that I could easily derive the ability to send my custom types over the wire without too much thought, but for some reason showTextData converts everything to lower case, causing readTextData to fail.

What is the reasoning behind this? IMO the naming is a little misleading and error prone.

@fizruk
Copy link
Owner

fizruk commented Sep 24, 2018

I was hoping readTextData and showTextData would form a nice bijection

There cannot be a bijection (because not every Text can be parsed in general), but you probably meant this property

readTextData . showTextData == Right  -- does not hold: showTextData does lowercasing

What is the reasoning behind this?

I can't recall it now, but think the reasoning was that showt (or Text.pack . show) does the regular show-like serialisation already, and I needed a lowercase one for case-insensitive helpers, so I made showTextData to be that lowercase helper. Could've named it differently, I guess 😅

IMO the naming is a little misleading and error prone.

You're right, naming is a bit misleading (although I have tried to make documentation clear 😄 ).

Functions that do have the property you are looking for are

  • showt (from text-show) and
  • Text.pack . show.
readTextData . showt == Right

readTextData . Text.pack . show == Right

Examples:

>>> quickCheck (\x -> readTextData (showt (x :: String)) == Right x)
+++ OK, passed 100 tests.
data Person = Person { fullName :: String, age :: Int } deriving (Eq, Show, Read)
instance Arbitrary Person where arbitrary = Person <$> arbitrary <*> arbitrary
>>> quickCheck (\x -> readTextData (Text.pack (show (x :: Person))) == Right x)
+++ OK, passed 100 tests.

@tysonzero
Copy link
Author

tysonzero commented Sep 24, 2018

You are right that it's not a bijection, faithful serialization I guess is what i'm after. I ended up reading the docs after the fact and discovered my mistake, but initially I just noticed the existence of showTextData and made some assumptions.

pack . show ended up doing the job. Although it's worth noting that the existence of a showTextData function that did lowercase and no function that didn't, gave me the impression that there was something wrong with uppercase letters in URIs, so one purely doc change that might be worthwhile is just highlight that if you don't want lowercasing use pack . show.

@fizruk
Copy link
Owner

fizruk commented Sep 24, 2018

so one purely doc change that might be worthwhile is just highlight that if you don't want lowercasing use pack . show.

Would you do the honours? 😊

tysonzero added a commit to tysonzero/http-api-data that referenced this issue Sep 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants