Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

parseMayParam #1

Open
wants to merge 3 commits into from

2 participants

@dzhus

This allows parsing a request parameter using an attoparsec Parser, which I found quite useful. The original name of the routine was getParamWith but I renamed it to match other functions in CoreUtils module.

@mightybyte
Collaborator

Hmmmm, my initial thought is to not pollute the package with dependencies on things like attoparsec. Maybe a more general function for dealing with Maybes and params would be good though.

@dzhus

I thought of this as well. However, aeson already pulls attoparsec into the dep tree.

@dzhus

Le bump.

@dzhus

Any new thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 31, 2013
  1. @dzhus

    Added parseMayParam helper

    dzhus authored
Commits on Jun 4, 2013
  1. @dzhus

    Merge from upstream

    dzhus authored
Commits on Nov 6, 2013
  1. @dzhus
This page is out of date. Refresh to see the latest.
Showing with 18 additions and 0 deletions.
  1. +1 −0  snap-extras.cabal
  2. +17 −0 src/Snap/Extras/CoreUtils.hs
View
1  snap-extras.cabal
@@ -39,6 +39,7 @@ Library
hs-source-dirs: src
Build-depends:
aeson >= 0.6 && < 0.7
+ , attoparsec >= 0.10 && < 0.11
, base >= 4 && < 5
, blaze-builder >= 0.3 && < 0.4
, blaze-html >= 0.6 && < 0.7
View
17 src/Snap/Extras/CoreUtils.hs
@@ -14,6 +14,7 @@ module Snap.Extras.CoreUtils
, reqParam
, readParam
, readMayParam
+ , parseMayParam
, redirectReferer
, redirectRefererFunc
, dirify
@@ -25,6 +26,7 @@ module Snap.Extras.CoreUtils
-------------------------------------------------------------------------------
import Control.Monad
+import Data.Attoparsec
import Data.ByteString.Char8 (ByteString)
import qualified Data.ByteString.Char8 as B
import Data.Maybe
@@ -122,6 +124,21 @@ readMayParam k = do
------------------------------------------------------------------------------
+-- | Use the supplied parser to read a parameter from request. Fail
+-- when the parameter is not present or can't be parsed.
+parseMayParam :: MonadSnap m =>
+ Parser a
+ -> ByteString
+ -- ^ Parameter name.
+ -> m (Maybe a)
+parseMayParam parser k = do
+ input <- liftM (parseOnly parser) `liftM` getParam k
+ return $ case input of
+ Just (Right p) -> Just p
+ _ -> Nothing
+
+
+------------------------------------------------------------------------------
-- | Redirects back to the refering page. If there is no Referer header, then
-- redirect to /.
redirectReferer :: MonadSnap m => m b
Something went wrong with that request. Please try again.