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

Parsable UTCTime #250 #368

Merged
merged 3 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion Web/Scotty/Action.hs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@
import Data.Text.Encoding as STE
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Encoding as TLE
import Data.Time (UTCTime)
import Data.Time.Format (parseTimeM, defaultTimeLocale)
import Data.Word

import Network.HTTP.Types
Expand All @@ -92,7 +94,7 @@
import Numeric.Natural

import Web.Scotty.Internal.Types
import Web.Scotty.Util (mkResponse, addIfNotPresent, add, replace, lazyTextToStrictByteString, decodeUtf8Lenient)

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

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.6.3

The import of ‘decodeUtf8Lenient’

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

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.4.6

The import of ‘decodeUtf8Lenient’

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

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.6.2

The import of ‘decodeUtf8Lenient’
import UnliftIO.Exception (Handler(..), catch, catches, throwIO)

import Network.Wai.Internal (ResponseReceived(..))
Expand All @@ -119,7 +121,7 @@
-- | Catches 'StatusError' and produces an appropriate HTTP response.
statusErrorHandler :: MonadIO m => ErrorHandler m
statusErrorHandler = Handler $ \case
StatusError s e -> do

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

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.6.3

In the use of data constructor ‘StatusError’

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

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.2.8

In the use of data constructor ‘StatusError’

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

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 8.10.7

In the use of data constructor ‘StatusError’

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

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.0.2

In the use of data constructor ‘StatusError’

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

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.4.6

In the use of data constructor ‘StatusError’

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

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.6.2

In the use of data constructor ‘StatusError’
status s
let code = T.pack $ show $ statusCode s
let msg = decodeUtf8Lenient $ statusMessage s
Expand Down Expand Up @@ -186,7 +188,7 @@
--
-- Uncaught exceptions turn into HTTP responses corresponding to the given status.
raiseStatus :: Monad m => Status -> T.Text -> ActionT m a
raiseStatus s = E.throw . StatusError s

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

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.6.3

In the use of data constructor ‘StatusError’

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

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.2.8

In the use of data constructor ‘StatusError’

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

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 8.10.7

In the use of data constructor ‘StatusError’

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

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.0.2

In the use of data constructor ‘StatusError’

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

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.4.6

In the use of data constructor ‘StatusError’

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

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.6.2

In the use of data constructor ‘StatusError’
{-# DEPRECATED raiseStatus "Use status, text, and finish instead" #-}

-- | Throw an exception which can be caught within the scope of the current Action with 'catch'.
Expand Down Expand Up @@ -413,7 +415,7 @@
--
-- NB : Doesn't throw exceptions.
--
-- /Since: FIXME/
-- /Since: 0.21/
paramWithMaybe :: (Monad m, Parsable b) =>
(ActionEnv -> [Param])
-> T.Text -- ^ parameter name
Expand Down Expand Up @@ -525,6 +527,18 @@
instance Parsable Word64 where parseParam = readEither
instance Parsable Natural where parseParam = readEither

-- | parse a UTCTime timestamp formatted as a ISO 8601 timestamp:
--
-- @yyyy-mm-ddThh:mm:ssZ@ , where the seconds can have a decimal part with up to 12 digits and no trailing zeros.
instance Parsable UTCTime where
parseParam t =
let
fmt = "%FT%T%QZ"
in
case parseTimeM True defaultTimeLocale fmt (TL.unpack t) of
Just d -> Right d
_ -> Left $ "parseParam UTCTime: no parse of \"" <> t <> "\""

-- | Useful for creating 'Parsable' instances for things that already implement 'Read'. Ex:
--
-- > instance Parsable Int where parseParam = readEither
Expand Down
3 changes: 2 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## next [????.??.??]

## 0.21.1
### New
* add `instance Parsable UTCTime` (#250)

### Fixes
* Path parameters with value matching the parameter name prefixed by colon will properly populate `pathParams` with their literal value : `/:param` will match `/:param` and add a `Param` with value `("param", ":param")` (#301)
Expand Down
1 change: 1 addition & 0 deletions scotty.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ test-suite spec
network,
scotty,
text,
time,
wai
build-tool-depends: hspec-discover:hspec-discover == 2.*
GHC-options: -Wall -threaded -fno-warn-orphans
Expand Down
21 changes: 16 additions & 5 deletions test/Web/ScottySpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import Data.Char
import Data.String
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Encoding as TLE
import Data.Time (UTCTime(..))
import Data.Time.Calendar (fromGregorian)
import Data.Time.Clock (secondsToDiffTime)

import Network.HTTP.Types
import Network.Wai (Application, Request(queryString), responseLBS)
import qualified Control.Exception.Lifted as EL
Expand Down Expand Up @@ -152,7 +156,6 @@ spec = do
it "returns query parameter with given name" $ do
get "/search" `shouldRespondWith` "haskell"


describe "ActionM" $ do
context "MonadBaseControl instance" $ do
withApp (Scotty.get "/" $ (undefined `EL.catch` ((\_ -> html "") :: E.SomeException -> ActionM ()))) $ do
Expand Down Expand Up @@ -190,22 +193,30 @@ spec = do
it "Responds with a 302 Redirect" $ do
get "/a" `shouldRespondWith` 302 { matchHeaders = ["Location" <:> "/b"] }

describe "Parsable" $ do
it "parses a UTCTime string" $ do
parseParam "2023-12-18T00:38:00Z" `shouldBe` Right (UTCTime (fromGregorian 2023 12 18) (secondsToDiffTime (60 * 38)) )

describe "captureParam" $ do
withApp (
do
Scotty.matchAny "/search/:q" $ do
Scotty.get "/search/:q" $ do
_ :: Int <- captureParam "q"
text "int"
Scotty.matchAny "/search/:q" $ do
Scotty.get "/search/:q" $ do
_ :: String <- captureParam "q"
text "string"
Scotty.get "/search-time/:q" $ do
t :: UTCTime <- captureParam "q"
text $ TL.pack (show t)
) $ do
it "responds with 200 OK iff at least one route matches at the right type" $ do
get "/search/42" `shouldRespondWith` 200 { matchBody = "int" }
get "/search/potato" `shouldRespondWith` 200 { matchBody = "string" }
get "/search-time/2023-12-18T00:38:00Z" `shouldRespondWith` 200 {matchBody = "2023-12-18 00:38:00 UTC"}
withApp (
do
Scotty.matchAny "/search/:q" $ do
Scotty.get "/search/:q" $ do
v <- captureParam "q"
json (v :: Int)
) $ do
Expand All @@ -227,7 +238,7 @@ spec = do
get "/search/xxx" `shouldRespondWith` 200 { matchBody = "z"}

describe "queryParam" $ do
withApp (Scotty.matchAny "/search" $ queryParam "query" >>= text) $ do
withApp (Scotty.get "/search" $ queryParam "query" >>= text) $ do
it "returns query parameter with given name" $ do
get "/search?query=haskell" `shouldRespondWith` "haskell"
withApp (Scotty.matchAny "/search" (do
Expand Down
Loading