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

From/ToFormUrlEncoded #31

Closed
wants to merge 8 commits into from

Conversation

cdepillabout
Copy link
Contributor

@cdepillabout cdepillabout commented Aug 24, 2016

This is PR #25 rebased onto the current master, as requested in #25 (comment).

I've added a commit (0e7f261) that fixes a failing test case. I've also added a commit (ac72f9e) that adds support for ghc-7.8 to the code from @jkarni.

    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.
import Web.HttpApiData.Internal.HttpApiData

-- | The contents of a form, not yet url-encoded.
newtype Form = Form { unForm :: M.Map T.Text T.Text }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to use lazy Map and not strict Map or HashMap? /cc @jkarni

@fizruk
Copy link
Owner

fizruk commented Aug 24, 2016

Hmm, we should not use String.

@cdepillabout will you change every Either String with Either Text, please?

=> GFromForm (M1 S sel (K1 i f)) where
gFromForm f = case M.lookup sel (unForm f) of
Nothing -> Left . T.unpack $ "Could not find key " <> sel
Just v -> left T.unpack $ M1 . K1 <$> parseUrlPiece v
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be more appropriate to use parseQueryParam here.

@cdepillabout
Copy link
Contributor Author

@fizruk Thanks for the review. I'll take a crack at fixing these comments.

I might need help with some of the documentation-fixes. I'm not very well-versed with Generic.

@fizruk
Copy link
Owner

fizruk commented Aug 25, 2016

@cdepillabout it's okay, just fix what you are confident in and we'll take care of the rest!

@cdepillabout
Copy link
Contributor Author

@fizruk I fixed up most of your comments. Here's a short description of what changes each commit makes:

  • acc2cd4: Remove the dependency on the string-conversions package.
  • 97f200f: Replace uses of toUrlPiece/parseUrlPiece with toQueryParam/parseQueryParam.
  • dd1baa5: Change all uses of String to Text.
  • 8ab1a1f: Add comments and doctests. Please make sure you check these comments, especially the 'Generic' stuff.
  • 6487f48: Add two convenience functions:
decodeWithFromForm :: (FromForm a) => BSL.ByteString -> Either T.Text a

encodeWithToForm :: (ToForm a) => a -> BSL.ByteString

Please feel free to bikeshed on the names. I don't particularly like decodeWithFromForm and encodeWithToForm.

@fizruk
Copy link
Owner

fizruk commented Aug 29, 2016

Thanks! I'll amend documentation, fix some other issues and merge later today.

@fizruk fizruk mentioned this pull request Aug 30, 2016
@cdepillabout
Copy link
Contributor Author

This has been superseded by #32.

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