Skip to content

Commit

Permalink
Add MonadFake class for lifting Fakes
Browse files Browse the repository at this point in the history
Following on from the 'FakeT' transformer, it makes sense to be able to
"lift" the default 'Fake' values into the monad stack, in order to allow
them to actually share the transformer cache and generator. This is done
by the 'MonadFake' class, which comes with an instance for 'FakeT', as
well as some of the other transformers defined in the `transformers`
package.
  • Loading branch information
ivanbakel committed Jun 9, 2021
1 parent 42fac6d commit 8c4d2cb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions fakedata.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ library
Faker.Cannabis
Faker.Chiquito
Faker.ChuckNorris
Faker.Class
Faker.Code
Faker.Coffee
Faker.Coin
Expand Down Expand Up @@ -609,6 +610,7 @@ library
, template-haskell
, text
, time
, transformers
, unordered-containers
, vector
, yaml
Expand Down
33 changes: 33 additions & 0 deletions src/Faker/Class.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module Faker.Class
( MonadFake(..)
) where

import Faker (FakeT(..), Fake)

import Control.Monad.IO.Class
import Control.Monad.Trans.Class (MonadTrans(lift))
import Control.Monad.Trans.Except (ExceptT)
import Control.Monad.Trans.Identity (IdentityT)
import Control.Monad.Trans.Maybe (MaybeT)
import Control.Monad.Trans.Reader (ReaderT)
import Control.Monad.Trans.State (StateT)
import Control.Monad.Trans.Writer (WriterT)

class Monad m => MonadFake m where
liftFake :: Fake a -> m a

instance MonadIO m => MonadFake (FakeT m) where
liftFake (Fake f) = FakeT (liftIO . f)

instance MonadFake m => MonadFake (ReaderT r m) where
liftFake = lift . liftFake
instance (Monoid w, MonadFake m) => MonadFake (WriterT w m) where
liftFake = lift . liftFake
instance MonadFake m => MonadFake (StateT s m) where
liftFake = lift . liftFake
instance MonadFake m => MonadFake (IdentityT m) where
liftFake = lift . liftFake
instance MonadFake m => MonadFake (ExceptT e m) where
liftFake = lift . liftFake
instance MonadFake m => MonadFake (MaybeT m) where
liftFake = lift . liftFake

0 comments on commit 8c4d2cb

Please sign in to comment.