Skip to content

Commit fb0d140

Browse files
committed
Get rid of MonadFail constraints in MongoDB.Query
PR #141
2 parents 6f1d842 + 46cfe5b commit fb0d140

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
All notable changes to this project will be documented in this file.
33
This project adheres to [Package Versioning Policy](https://wiki.haskell.org/Package_versioning_policy).
44

5+
* Get rid of `MonadFail` constraints in `Database.MongoDB.Query`
6+
57
## [2.7.1.2] - 2022-10-26
68

79
### Added

Database/MongoDB/Query.hs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ import Control.Monad
6060
when,
6161
)
6262
import Control.Monad.Reader (MonadReader, ReaderT, ask, asks, local, runReaderT)
63-
import Control.Monad.Trans (MonadIO, liftIO)
63+
import Control.Monad.Trans (MonadIO, liftIO, lift)
6464
import qualified Crypto.Hash.MD5 as MD5
6565
import qualified Crypto.Hash.SHA1 as SHA1
6666
import qualified Crypto.MAC.HMAC as HMAC
@@ -131,6 +131,7 @@ import Database.MongoDB.Internal.Protocol
131131
pwKey,
132132
FlagBit (..)
133133
)
134+
import Control.Monad.Trans.Except
134135
import qualified Database.MongoDB.Internal.Protocol as P
135136
import Database.MongoDB.Internal.Util (liftIOE, loop, true1, (<.>))
136137
import System.Mem.Weak (Weak)
@@ -1279,7 +1280,7 @@ find q@Query{selection, batchSize} = do
12791280
dBatch <- liftIO $ requestOpMsg pipe newQr []
12801281
newCursor db (coll selection) batchSize dBatch
12811282

1282-
findCommand :: (MonadIO m, MonadFail m) => Query -> Action m Cursor
1283+
findCommand :: (MonadIO m) => Query -> Action m Cursor
12831284
-- ^ Fetch documents satisfying query using the command "find"
12841285
findCommand q@Query{..} = do
12851286
pipe <- asks mongoPipe
@@ -1371,7 +1372,7 @@ defFamUpdateOpts ups = FamUpdate
13711372
-- Return a single updated document (@new@ option is set to @True@).
13721373
--
13731374
-- See 'findAndModifyOpts' for more options.
1374-
findAndModify :: (MonadIO m, MonadFail m)
1375+
findAndModify :: (MonadIO m)
13751376
=> Query
13761377
-> Document -- ^ updates
13771378
-> Action m (Either String Document)
@@ -1386,7 +1387,7 @@ findAndModify q ups = do
13861387

13871388
-- | Run the @findAndModify@ command
13881389
-- (allows more options than 'findAndModify')
1389-
findAndModifyOpts :: (MonadIO m, MonadFail m)
1390+
findAndModifyOpts :: (MonadIO m)
13901391
=> Query
13911392
-> FindAndModifyOpts
13921393
-> Action m (Either String (Maybe Document))
@@ -1666,7 +1667,7 @@ isCursorClosed (Cursor _ _ var) = do
16661667
type Pipeline = [Document]
16671668
-- ^ The Aggregate Pipeline
16681669

1669-
aggregate :: (MonadIO m, MonadFail m) => Collection -> Pipeline -> Action m [Document]
1670+
aggregate :: (MonadIO m) => Collection -> Pipeline -> Action m [Document]
16701671
-- ^ Runs an aggregate and unpacks the result. See <http://docs.mongodb.org/manual/core/aggregation/> for details.
16711672
aggregate aColl agg = do
16721673
aggregateCursor aColl agg def >>= rest
@@ -1689,7 +1690,7 @@ aggregateCommand aColl agg AggregateConfig {..} =
16891690
, "allowDiskUse" =: allowDiskUse
16901691
]
16911692

1692-
aggregateCursor :: (MonadIO m, MonadFail m) => Collection -> Pipeline -> AggregateConfig -> Action m Cursor
1693+
aggregateCursor :: (MonadIO m) => Collection -> Pipeline -> AggregateConfig -> Action m Cursor
16931694
-- ^ Runs an aggregate and unpacks the result. See <http://docs.mongodb.org/manual/core/aggregation/> for details.
16941695
aggregateCursor aColl agg cfg = do
16951696
pipe <- asks mongoPipe
@@ -1708,18 +1709,21 @@ aggregateCursor aColl agg cfg = do
17081709
>>= either (liftIO . throwIO . AggregateFailure) return
17091710

17101711
getCursorFromResponse
1711-
:: (MonadIO m, MonadFail m)
1712+
:: (MonadIO m)
17121713
=> Collection
17131714
-> Document
17141715
-> Action m (Either String Cursor)
17151716
getCursorFromResponse aColl response
1716-
| true1 "ok" response = do
1717-
cursor <- lookup "cursor" response
1718-
firstBatch <- lookup "firstBatch" cursor
1719-
cursorId <- lookup "id" cursor
1720-
db <- thisDatabase
1721-
Right <$> newCursor db aColl 0 (return $ Batch Nothing cursorId firstBatch)
1717+
| true1 "ok" response = runExceptT $ do
1718+
cursor <- lookup "cursor" response ?? "cursor is missing"
1719+
firstBatch <- lookup "firstBatch" cursor ?? "firstBatch is missing"
1720+
cursorId <- lookup "id" cursor ?? "id is missing"
1721+
db <- lift thisDatabase
1722+
lift $ newCursor db aColl 0 (return $ Batch Nothing cursorId firstBatch)
17221723
| otherwise = return $ Left $ at "errmsg" response
1724+
where
1725+
Nothing ?? e = throwE e
1726+
Just a ?? _ = pure a
17231727

17241728
-- ** Group
17251729

0 commit comments

Comments
 (0)