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

Forms #32

Merged
merged 43 commits into from
Aug 31, 2016
Merged

Forms #32

merged 43 commits into from
Aug 31, 2016

Conversation

fizruk
Copy link
Owner

@fizruk fizruk commented Aug 30, 2016

Closes #23. Supersedes #31 and #25.

Some further questions:

  • do we want FormOptions to mimic aeson's Options?
  • do we want ToFormKey/FromFormKey instances for Maybe a, Either a b, First a, Last a like we have for ToHttpApiData and FromHttpApiData?
  • do we want specialised instances for Map Text Text, HashMap Text Text and [(Text, Text)]?
  • do we want to bikeshed functions names (helpers in particular)?

/cc @jkarni @cdepillabout

jkarni and others added 26 commits August 24, 2016 22:23
    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.

-- | 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'.
Copy link
Contributor

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?

Copy link
Owner Author

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.

Copy link
Owner Author

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.

Copy link
Contributor

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.

@fizruk fizruk force-pushed the cdepillabout-add-form-url-encoded branch from 7e55dd8 to 5ae2f86 Compare August 30, 2016 13:27
encodeForm :: Form -> BSL.ByteString
encodeForm xs = BSL.intercalate "&" $ map (BSL.fromStrict . encodePair) $ toList xs
where
escape = Text.encodeUtf8 . Text.pack . escapeURIString isUnreserved . Text.unpack
Copy link
Contributor

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-bytestrings](%28https://hackage.haskell.org/package/uri-bytestring-0.2.2.0/docs/URI-ByteString.html%29 could) urlEncode could be a good option.

Copy link
Owner Author

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.

@fizruk
Copy link
Owner Author

fizruk commented Aug 30, 2016

@jkarni ah, I still want to remove support for them for now. If there's demand we'll get back to this problem later.

@fizruk fizruk force-pushed the cdepillabout-add-form-url-encoded branch 2 times, most recently from 2fa39c1 to ec3b440 Compare August 30, 2016 21:05
@fizruk fizruk force-pushed the cdepillabout-add-form-url-encoded branch from ec3b440 to e9c728b Compare August 30, 2016 21:12
@cdepillabout
Copy link
Contributor

@fizruk: This looks good to me.

One thing I'd like to see is an example using FormOptions. One thing that always bugs me is when I look at lens or aeson documentation is that there is not enough explanation for using their generic deriving options.

For example, take a datatype like this:

data Person = Person
  { personName :: Text
  , personAge :: Int
  }

I imagine it's common to want to urlencode this to be name=foo&age=30, not personName=foo&personAge=30.

An example of how to use defaultOptions to accomplish this would be really nice.

Of course, I can always send a PR for this additional documentation after this PR (#32) has been merged.

@fizruk fizruk force-pushed the cdepillabout-add-form-url-encoded branch from 570353a to 9cb90d5 Compare August 31, 2016 00:27
@fizruk fizruk force-pushed the cdepillabout-add-form-url-encoded branch from acae0c8 to de9fe0f Compare August 31, 2016 00:54
@fizruk
Copy link
Owner Author

fizruk commented Aug 31, 2016

@cdepillabout added an example in e613d05.

Any final wishes before I release? :)

@jkarni
Copy link
Contributor

jkarni commented Aug 31, 2016

I wish you a good flight?

@fizruk
Copy link
Owner Author

fizruk commented Aug 31, 2016

@jkarni yeah, thanks :)

I am merging this now, I think we've covered most things here.

@fizruk fizruk merged commit 56c53a4 into master Aug 31, 2016
@fizruk fizruk deleted the cdepillabout-add-form-url-encoded branch August 31, 2016 01:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants