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

Add paramListOrNothing function #1130

Merged
merged 7 commits into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions IHP/Controller/Param.hs
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,15 @@ paramList name =
-- []
--
--
paramListOrNothing :: forall valueType. (?context :: ControllerContext, DeepSeq.NFData valueType, ParamReader valueType) => ByteString -> [Maybe valueType]
paramListOrNothing :: forall valueType. (?context :: ControllerContext, DeepSeq.NFData valueType, ParamReader valueType, IsEmpty valueType) => ByteString -> [Maybe valueType]
paramListOrNothing name =
allParams
|> filter (\(paramName, paramValue) -> paramName == name)
|> mapMaybe (\(paramName, paramValue) -> paramValue)
amitaibu marked this conversation as resolved.
Show resolved Hide resolved
|> map (readParameter @valueType)
|> map (\value -> case value of
Left _ -> Nothing
Right val -> Just val
Right val -> if isEmpty val then Nothing else Just val
)
|> DeepSeq.force
{-# INLINABLE paramListOrNothing #-}
Expand Down
4 changes: 4 additions & 0 deletions IHP/HaskellSupport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ instance IsEmpty UUID.UUID where
isEmpty uuid = UUID.nil == uuid
{-# INLINE isEmpty #-}

instance IsEmpty Int where
isEmpty _ = False
{-# INLINE isEmpty #-}

ifOrEmpty :: (Monoid a) => Bool -> a -> a
ifOrEmpty bool a = if bool then a else mempty
{-# INLINE ifOrEmpty #-}
Expand Down
2 changes: 1 addition & 1 deletion Test/Controller/ParamSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ tests = do
describe "paramListOrNothing" do
it "should parse valid input" do
let ?context = createControllerContextWithParams [("ingredients", "milk"), ("ingredients", ""), ("ingredients", "egg")]
(paramListOrNothing @Text "ingredients") `shouldBe` [Just "milk", Just "", Just "egg"]
(paramListOrNothing @Text "ingredients") `shouldBe` [Just "milk", Nothing, Just "egg"]

it "should not fail on invalid input" do
let ?context = createControllerContextWithParams [("numbers", "1"), ("numbers", "NaN")]
Expand Down