Skip to content

Commit

Permalink
rename to setMaybe
Browse files Browse the repository at this point in the history
  • Loading branch information
hendi committed Sep 14, 2021
1 parent dac3bfe commit 3c151e5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
36 changes: 18 additions & 18 deletions IHP/HaskellSupport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module IHP.HaskellSupport
, get
, set
, setJust
, setIfJust
, setMaybe
, ifOrEmpty
, modify
, modifyJust
Expand Down Expand Up @@ -115,9 +115,9 @@ instance forall name name'. (KnownSymbol name, name' ~ name) => IsLabel name (Pr
-- | Returns the field value for a field name
--
-- __Example:__
--
--
-- > data Project = Project { name :: Text, isPublic :: Bool }
-- >
-- >
-- > let project = Project { name = "Hello World", isPublic = False }
--
-- >>> get #name project
Expand All @@ -132,9 +132,9 @@ get _ record = Record.getField @name record
-- | Sets a field of a record and returns the new record.
--
-- __Example:__
--
--
-- > data Project = Project { name :: Text, isPublic :: Bool }
-- >
-- >
-- > let project = Project { name = "Hello World", isPublic = False }
--
-- >>> set #name "New Name" project
Expand All @@ -156,25 +156,25 @@ set name value record = setField @name value record
-- >
-- > let project = Project { name = Nothing }
--
-- >>> setIfJust #name (Just "New Name") project
-- >>> setMaybe #name (Just "New Name") project
-- Project { name = Just "New Name" }
--
-- >>> setIfJust #name Nothing project
-- >>> setMaybe #name Nothing project
-- Project { name = Just "New Name" } -- previous value is kept
--
setIfJust :: forall model name value. (KnownSymbol name, SetField name model (Maybe value)) => Proxy name -> Maybe value -> model -> model
setIfJust name value record = case value of
setMaybe :: forall model name value. (KnownSymbol name, SetField name model (Maybe value)) => Proxy name -> Maybe value -> model -> model
setMaybe name value record = case value of
Just value -> setField @name (Just value) record
Nothing -> record
{-# INLINE setIfJust #-}
{-# INLINE setMaybe #-}


-- | Like 'set' but wraps the value with a 'Just'. Useful when you want to set a 'Maybe' field
--
-- __Example:__
--
--
-- > data Project = Project { name :: Maybe Text }
-- >
-- >
-- > let project = Project { name = Nothing }
--
-- >>> setJust #name "New Name" project
Expand Down Expand Up @@ -208,9 +208,9 @@ modifyJust _ updateFunction model = case Record.getField @name model of
-- | Plus @1@ on record field.
--
-- __Example:__
--
--
-- > data Project = Project { name :: Text, followersCount :: Int }
-- >
-- >
-- > let project = Project { name = "Hello World", followersCount = 0 }
--
-- >>> project |> incrementField #followersCount
Expand All @@ -222,9 +222,9 @@ incrementField _ model = let value = Record.getField @name model in setField @na
-- | Minus @1@ on a record field.
--
-- __Example:__
--
--
-- > data Project = Project { name :: Text, followersCount :: Int }
-- >
-- >
-- > let project = Project { name = "Hello World", followersCount = 1337 }
--
-- >>> project |> decrementField #followersCount
Expand Down Expand Up @@ -282,7 +282,7 @@ forEach elements function = forM_ elements function
--
-- > printUser :: (Int, User) -> IO ()
-- > printUser (index, user) = putStrLn (tshow index <> ": " <> tshow user)
-- >
-- >
-- > forEachWithIndex users printUser
--
-- __Example:__ Within HSX
Expand Down Expand Up @@ -434,4 +434,4 @@ instance (CopyFields rest destinationRecord sourceRecord
--
allEnumValues :: forall enumType. Enum enumType => [enumType]
allEnumValues = enumFrom (toEnum 0)
{-# INLINABLE allEnumValues #-}
{-# INLINABLE allEnumValues #-}
6 changes: 3 additions & 3 deletions Test/HaskellSupportSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ tests = do
it "should return all enum values" do
(allEnumValues @Color) `shouldBe` [Yellow, Red, Blue]

describe "setIfJust" do
describe "setMaybe" do
it "should set a Just value" do
let c = RecordC { field = Nothing }
let c' = c |> setIfJust #field (Just 1)
let c' = c |> setMaybe #field (Just 1)

c' `shouldBe` RecordC { field = Just 1 }

it "should not set a Nothing value" do
let c = RecordC { field = Just 1 }
let c' = c |> setIfJust #field Nothing
let c' = c |> setMaybe #field Nothing

c' `shouldBe` RecordC { field = Just 1 }

Expand Down

0 comments on commit 3c151e5

Please sign in to comment.