Skip to content

Commit

Permalink
wip add paramWithAlt
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Zocca committed Oct 3, 2023
1 parent 1928910 commit a2aaa96
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Web/Scotty/Action.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ module Web.Scotty.Action

import Blaze.ByteString.Builder (fromLazyByteString)

import Control.Applicative (Alternative(..))
import qualified Control.Exception as E
import Control.Monad (when)
import Control.Monad.IO.Class (MonadIO(..))
Expand Down Expand Up @@ -328,6 +329,21 @@ paramWith ty f err k = do
_ -> raiseStatus err (T.unwords ["Cannot parse", v, "as a", T.pack (show ty), "parameter"])
in either (const $ handleParseError ty) return $ parseParam v

-- | Look up a parameter with the possibility of lookup or parsing failure.
--
-- We return the result in an 'Alternative' functor to allow for various use cases : 'Maybe', 'Validation', etc.
--
-- /Since: FIXME/
paramWithAlt :: (Monad m, Alternative f, Parsable b) =>
(ActionEnv -> [Param])
-> T.Text -- ^ parameter name
-> ActionT m (f b)
paramWithAlt f k = do

Check warning on line 341 in Web/Scotty/Action.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 8.10.7

Defined but not used: ‘paramWithAlt’

Check warning on line 341 in Web/Scotty/Action.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.0.2

Defined but not used: ‘paramWithAlt’

Check warning on line 341 in Web/Scotty/Action.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.2.8

Defined but not used: ‘paramWithAlt’

Check warning on line 341 in Web/Scotty/Action.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.4.6

Defined but not used: ‘paramWithAlt’

Check warning on line 341 in Web/Scotty/Action.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.6.2

Defined but not used: ‘paramWithAlt’
val <- ActionT $ (lookup k . f) <$> ask
case val of
Nothing -> pure empty
Just v -> either (const $ pure empty) (pure . pure) $ parseParam v

-- | Get all parameters from capture, form and query (in that order).
params :: Monad m => ActionT m [Param]
params = paramsWith getParams
Expand Down

0 comments on commit a2aaa96

Please sign in to comment.