-
Notifications
You must be signed in to change notification settings - Fork 43
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
Forms #32
Forms #32
Conversation
This commit moves ToFormUrlEncoded and FromFormUrlEncoded from servant to http-api-data. The classes are renamed ToForm and FromForm, and gain generic instances.
`decodeForm (encodeForm x)` does not equal `x` when we have a form like `Form [("", "")]`. The result `Form` after encoding and decoding is just `Form []`. This commit adds in a newtype wrapper `NoEmptyKeyForm` with an `Arbitrary` instance that removes all empty-string keys.
This commit replaces all uses of the `cs` function with functions like `pack`, `unpack`, `decodeUtf8`, etc.
…rectly to / from a datatype.
|
||
-- | Typeclass for types that can be parsed from keys of a 'Form'. This is the reverse of 'ToFormKey'. | ||
class FromFormKey k where | ||
-- | Parse a key of a 'Form'. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at the W3 docs, it seems keys (control names
, in the lingo) should be case-insensitive.
https://www.w3.org/TR/html401/interact/forms.html#control-name
https://www.w3.org/TR/html401/interact/forms.html#h-17.3
So maybe this needs to be CI Text
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting, we might want to use CI Text
for helpers in Web.HttpApiData
too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jkarni can't find anywhere that "control names" should be case insensitive.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was looking at name
in 17.3, but that's not right. id
says it's case sensitive. So I think it's right as it stands.
7e55dd8
to
5ae2f86
Compare
encodeForm :: Form -> BSL.ByteString | ||
encodeForm xs = BSL.intercalate "&" $ map (BSL.fromStrict . encodePair) $ toList xs | ||
where | ||
escape = Text.encodeUtf8 . Text.pack . escapeURIString isUnreserved . Text.unpack |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is obviously inefficient. I'm in favor of making changes incrementally, and so leaving the fixing of it for another PR, but for reference [uri-bytestring
s](%28https://hackage.haskell.org/package/uri-bytestring-0.2.2.0/docs/URI-ByteString.html%29 could) urlEncode
could be a good option.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aha, I was looking for something like it!
urlEncodeQuery
looks ideal for encoding.
@jkarni ah, I still want to remove support for them for now. If there's demand we'll get back to this problem later. |
2fa39c1
to
ec3b440
Compare
ec3b440
to
e9c728b
Compare
@fizruk: This looks good to me. One thing I'd like to see is an example using For example, take a datatype like this:
I imagine it's common to want to urlencode this to be An example of how to use Of course, I can always send a PR for this additional documentation after this PR (#32) has been merged. |
570353a
to
9cb90d5
Compare
acae0c8
to
de9fe0f
Compare
@cdepillabout added an example in e613d05. Any final wishes before I release? :) |
I wish you a good flight? |
@jkarni yeah, thanks :) I am merging this now, I think we've covered most things here. |
Closes #23. Supersedes #31 and #25.
Some further questions:
FormOptions
to mimicaeson
'sOptions
?ToFormKey
/FromFormKey
instances forMaybe a
,Either a b
,First a
,Last a
like we have forToHttpApiData
andFromHttpApiData
?Map Text Text
,HashMap Text Text
and[(Text, Text)]
?/cc @jkarni @cdepillabout