mirror of
https://github.com/tomjaguarpaw/bluefin.git
synced 2024-08-14 16:20:30 +03:00
Implement DB example/template
Thanks to Florian Beeres @cideM
This commit is contained in:
parent
98f9076817
commit
8bbda7c76d
@ -15,6 +15,12 @@ including
|
|||||||
For an introduction to Bluefin, see the docs in the
|
For an introduction to Bluefin, see the docs in the
|
||||||
[`Bluefin`](bluefin/src/Bluefin.hs) module.
|
[`Bluefin`](bluefin/src/Bluefin.hs) module.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
There is an `bluefin-examples` package which you can see in this
|
||||||
|
repository at
|
||||||
|
[`bluefin-examples/src/Bluefin/Examples`](bluefin-examples/src/Bluefin/Examples).
|
||||||
|
|
||||||
## Acknowledgements
|
## Acknowledgements
|
||||||
|
|
||||||
Tom Ellis would like to thank many individuals for their work related
|
Tom Ellis would like to thank many individuals for their work related
|
||||||
|
3
bluefin-examples/CHANGELOG.md
Normal file
3
bluefin-examples/CHANGELOG.md
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
## 0.0.0.0
|
||||||
|
|
||||||
|
* Implement DB example/template. Thanks to Florian Beeres @cideM.
|
25
bluefin-examples/bluefin-examples.cabal
Normal file
25
bluefin-examples/bluefin-examples.cabal
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
cabal-version: 3.0
|
||||||
|
name: bluefin-examples
|
||||||
|
version: 0.0.0.0
|
||||||
|
license: MIT
|
||||||
|
license-file: LICENSE
|
||||||
|
author: Tom Ellis
|
||||||
|
maintainer: Tom Ellis
|
||||||
|
build-type: Simple
|
||||||
|
extra-doc-files: CHANGELOG.md
|
||||||
|
description: The Bluefin effect system, examples
|
||||||
|
synopsis: The Bluefin effect system, examples
|
||||||
|
homepage: https://github.com/tomjaguarpaw/bluefin
|
||||||
|
bug-reports: https://github.com/tomjaguarpaw/bluefin/issues
|
||||||
|
|
||||||
|
common warnings
|
||||||
|
ghc-options: -Wall
|
||||||
|
|
||||||
|
library
|
||||||
|
import: warnings
|
||||||
|
exposed-modules:
|
||||||
|
Bluefin.Examples.DB
|
||||||
|
build-depends:
|
||||||
|
base, bluefin >= 0.0.6.0 && < 0.1
|
||||||
|
hs-source-dirs: src
|
||||||
|
default-language: Haskell2010
|
58
bluefin-examples/src/Bluefin/Examples/DB.hs
Normal file
58
bluefin-examples/src/Bluefin/Examples/DB.hs
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
{-# LANGUAGE RankNTypes #-}
|
||||||
|
{-# LANGUAGE TypeOperators #-}
|
||||||
|
|
||||||
|
module Bluefin.Examples.DB where
|
||||||
|
|
||||||
|
import Bluefin.Compound (useImpl, useImplIn)
|
||||||
|
import Bluefin.Eff (Eff, (:&), (:>))
|
||||||
|
import qualified Bluefin.Eff as BF
|
||||||
|
import Bluefin.Exception (Exception)
|
||||||
|
import qualified Bluefin.Exception as BF
|
||||||
|
import Bluefin.IO (IOE)
|
||||||
|
import qualified Bluefin.IO as BF
|
||||||
|
|
||||||
|
newtype DbHandle = DbHandle String deriving (Show)
|
||||||
|
|
||||||
|
newtype UserId = UserId String deriving (Show, Eq)
|
||||||
|
|
||||||
|
newtype User = User String deriving (Show)
|
||||||
|
|
||||||
|
data DbEff es = MkDbEff
|
||||||
|
{ queryImpl :: DbHandle -> UserId -> Eff es User
|
||||||
|
}
|
||||||
|
|
||||||
|
query :: (e :> es) => DbEff e -> DbHandle -> UserId -> Eff es User
|
||||||
|
query db dbHandle userId = useImpl $ queryImpl db dbHandle userId
|
||||||
|
|
||||||
|
runDbEffIo ::
|
||||||
|
forall exEff dbEff es r.
|
||||||
|
(exEff :> es, dbEff :> es) =>
|
||||||
|
Exception String exEff ->
|
||||||
|
IOE dbEff ->
|
||||||
|
(forall e. DbEff e -> Eff (e :& es) r) ->
|
||||||
|
Eff es r
|
||||||
|
runDbEffIo ex _ fn =
|
||||||
|
useImplIn
|
||||||
|
fn
|
||||||
|
( MkDbEff
|
||||||
|
{ queryImpl = \_ userId -> do
|
||||||
|
if userId == UserId "1"
|
||||||
|
then pure $ User "Alice"
|
||||||
|
else BF.throw ex "not found"
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
main :: IO ()
|
||||||
|
main = do
|
||||||
|
let dbHandle = DbHandle "db"
|
||||||
|
|
||||||
|
result <- BF.runEff $ \io -> BF.try $ \ex ->
|
||||||
|
runDbEffIo ex io $ \db -> do
|
||||||
|
u1 <- query db dbHandle (UserId "1")
|
||||||
|
BF.effIO io $ print u1
|
||||||
|
u2 <- query db dbHandle (UserId "2")
|
||||||
|
BF.effIO io $ print u2
|
||||||
|
|
||||||
|
case result of
|
||||||
|
Left err -> print err
|
||||||
|
Right _ -> print "success"
|
@ -1,3 +1,4 @@
|
|||||||
packages:
|
packages:
|
||||||
bluefin/bluefin.cabal
|
bluefin/bluefin.cabal
|
||||||
bluefin-internal/bluefin-internal.cabal
|
bluefin-internal/bluefin-internal.cabal
|
||||||
|
bluefin-examples/bluefin-examples.cabal
|
||||||
|
Loading…
Reference in New Issue
Block a user