Skip to content

Commit

Permalink
WIP matchMaybe
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom authored and Tom committed Sep 5, 2021
1 parent 4faa2ec commit dc97993
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Opaleye/Internal/MaybeFields.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import Opaleye.Internal.Operators ((.&&), (.||), (.==), restrict, not,
ifExplict, IfPP, EqPP(EqPP))
import qualified Opaleye.Internal.Lateral
import qualified Opaleye.SqlTypes
import Opaleye.SqlTypes (SqlBool, IsSqlType)
import Opaleye.SqlTypes (SqlBool, IsSqlType, SqlInt4)

import Control.Monad (replicateM_)

Expand Down Expand Up @@ -92,6 +92,15 @@ justFields = pure
maybeFields :: PP.Default IfPP b b => b -> (a -> b) -> MaybeFields a -> b
maybeFields = maybeFieldsExplicit PP.def

matchMaybe :: PP.Default IfPP b b => MaybeFields a -> (Maybe a -> b) -> b
matchMaybe mf f = maybeFields (f Nothing) (f . Just) mf

example :: MaybeFields (Field SqlInt4)
-> Field SqlInt4
example mf = matchMaybe mf $ \case
Nothing -> 0
Just x -> x * 100

-- | The Opaleye analogue of 'Data.Maybe.fromMaybe'
fromMaybeFields :: PP.Default IfPP b b => b -> MaybeFields b -> b
fromMaybeFields = fromMaybeFieldsExplicit PP.def
Expand Down

0 comments on commit dc97993

Please sign in to comment.