Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Factor out `fromIntParam` helper

  • Loading branch information...
commit 2df6efd481f68cd512a9363c6f808eaf13aea97e 1 parent 14ba665
@dzhus authored
Showing with 19 additions and 11 deletions.
  1. +1 −11 src/Snap/Snaplet/Redson.hs
  2. +18 −0 src/Snap/Snaplet/Redson/Util.hs
View
12 src/Snap/Snaplet/Redson.hs
@@ -24,11 +24,8 @@ import Data.Functor
import Data.Aeson as A
-import Data.Char (isDigit)
-
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as LB (ByteString)
-import qualified Data.ByteString.UTF8 as BU (toString)
import Data.Configurator
@@ -411,7 +408,6 @@ search =
-- TODO: Mark these field names as reserved
mType <- getParam "_matchType"
sType <- getParam "_searchType"
- iLimit <- getParam "_limit"
outFields <- (\p -> maybe [] (B.split comma) p) <$>
getParam "_fields"
@@ -425,13 +421,7 @@ search =
Just "or" -> unionAll
_ -> intersectAll
- itemLimit <- return $ case iLimit of
- Just b -> let
- s = BU.toString b
- in
- if (all isDigit s) then (read s)
- else defaultSearchLimit
- _ -> defaultSearchLimit
+ itemLimit <- fromIntParam "_limit" defaultSearchLimit
-- Produce Just SearchTerm
indexValues <- mapM (\(i, c) -> do
View
18 src/Snap/Snaplet/Redson/Util.hs
@@ -10,7 +10,11 @@ module Snap.Snaplet.Redson.Util where
import Control.Applicative
+import Data.Char (isDigit)
+
import Data.ByteString (ByteString)
+import qualified Data.ByteString.UTF8 as BU (toString)
+
import Data.Maybe
import Snap.Core
@@ -21,6 +25,20 @@ fromParam :: MonadSnap m => ByteString -> m ByteString
fromParam p = fromMaybe "" <$> getParam p
+------------------------------------------------------------------------------
+-- | Get integer parameter value from Request or return default value.
+fromIntParam :: MonadSnap m => ByteString -> Int -> m Int
+fromIntParam p def = do
+ i <- getParam p
+ return $ case i of
+ Just b -> let
+ s = BU.toString b
+ in
+ if (all isDigit s) then (read s)
+ else def
+ _ -> def
+
+
data Error = Error { code :: Int
-- ^ Error response code
}
Please sign in to comment.
Something went wrong with that request. Please try again.