Skip to content

Commit

Permalink
Merge branch 'master' of github.com:digitallyinduced/ihp
Browse files Browse the repository at this point in the history
  • Loading branch information
mpscholten committed Jan 4, 2021
2 parents 49730fd + 489df4a commit d9bd3b4
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 1 deletion.
10 changes: 10 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,13 @@ Perhaps a package was added to IHP recently. Start a `nix-shell` in the IHP dire
### Trouble adding packages to IHP

Either add the package to your project's `default.nix` as well, or change the section `ihp = builtins.fetchGit ...` to `ihp = ./IHP;`in your project's `default.nix`. Then the `IHP/ihp.nix` will be used to fetch packages.

### Reverting back to running with stock IHP library

If you want to go back and use the standard IHP library instead of the cloned source, for instance when your much needed, contributed and approved feature becomes part of the release, then in your project directory:

```
rm -rf IHP
rm build/ihp-lib
./start
```
18 changes: 18 additions & 0 deletions IHP/Controller/Param.hs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,24 @@ instance ParamReader UTCTime where
readParameterJSON (Aeson.String string) = readParameter (cs string)
readParameterJSON _ = Left "ParamReader UTCTime: Expected String"

-- | Accepts values such as @2020-11-08T12:03:35Z@ or @2020-11-08@
instance ParamReader LocalTime where
{-# INLINABLE readParameter #-}
readParameter "" = Left "ParamReader LocalTime: Parameter missing"
readParameter byteString =
let
input = (cs byteString)
dateTime = parseTimeM True defaultTimeLocale "%Y-%m-%dT%H:%M:%S%QZ" input
date = parseTimeM True defaultTimeLocale "%Y-%-m-%-d" input
in case dateTime of
Nothing -> case date of
Just value -> Right value
Nothing -> Left "ParamReader LocalTime: Failed parsing"
Just value -> Right value

readParameterJSON (Aeson.String string) = readParameter (cs string)
readParameterJSON _ = Left "ParamReader LocalTime: Expected String"

-- | Accepts values such as @2020-11-08@
instance ParamReader Day where
{-# INLINABLE readParameter #-}
Expand Down
3 changes: 3 additions & 0 deletions IHP/ModelSupport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ instance InputValue () where
instance InputValue UTCTime where
inputValue time = cs (iso8601Show time)

instance InputValue LocalTime where
inputValue time = cs (iso8601Show time)

instance InputValue Day where
inputValue date = cs (iso8601Show date)

Expand Down
2 changes: 2 additions & 0 deletions IHP/Prelude.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module IHP.Prelude
, module Data.String.Conversions
, module Data.Time.Clock
, module Data.Time.Calendar
, module Data.Time.LocalTime
, module Data.Text
, module GHC.OverloadedLabels
, plain
Expand Down Expand Up @@ -55,6 +56,7 @@ import qualified Data.List as List
import Data.String.Conversions (ConvertibleStrings (convertString), cs)
import Data.Time.Clock
import Data.Time.Calendar
import Data.Time.LocalTime
import Data.Text (words, unwords, lines, unlines, intersperse, intercalate, toLower, toUpper, isInfixOf, isSuffixOf, isPrefixOf, splitAt)
import qualified Data.String.Interpolate
import GHC.OverloadedLabels
Expand Down
15 changes: 14 additions & 1 deletion Test/Controller/ParamSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,20 @@ tests = do

it "should accept JSON strings" do
(tshow (readParameterJSON @UTCTime (json "\"2020-11-08T12:03:35Z\""))) `shouldBe` ("Right 2020-11-08 12:03:35 UTC")


describe "LocalTime" do
it "should accept timestamps" do
(tshow (readParameter @LocalTime "2020-11-08T12:03:35Z")) `shouldBe` ("Right 2020-11-08 12:03:35")

it "should accept dates" do
(tshow (readParameter @LocalTime "2020-11-08")) `shouldBe` ("Right 2020-11-08 00:00:00")

it "should fail on invalid inputs" do
(readParameter @LocalTime "not a timestamp") `shouldBe` (Left "ParamReader LocalTime: Failed parsing")

it "should accept JSON strings" do
(tshow (readParameterJSON @LocalTime (json "\"2020-11-08T12:03:35Z\""))) `shouldBe` ("Right 2020-11-08 12:03:35")

describe "Day" do
it "should accept dates" do
(tshow (readParameter @Day "2020-11-08")) `shouldBe` ("Right 2020-11-08")
Expand Down

0 comments on commit d9bd3b4

Please sign in to comment.