module Yu.Core.Model.TH
( makeFetch
, makeUpdate
) where
import Control.Monad.IO.Class
import Data.Char
import Database.MongoDB as Mongo
import Yu.Core.Model.Internal
import Yu.Import.TH
firstUpper :: String -> String
firstUpper (x:xs) = toUpper x:xs
firstUpper xs = xs
makeFetch :: Name
-> String
-> Name
-> String
-> String
-> Q [Dec]
makeFetch func n kind field collection =
let name = mkName $ "fetch" ++ firstUpper n
m = mkName "m"
dec = SigD name $ ForallT [PlainTV m] [AppT (ConT ''MonadIO) (VarT m)]
(AppT
(AppT ArrowT (ConT ''ResT))
(AppT (AppT (ConT ''Action) (VarT m)) (AppT (ConT ''Maybe) (ConT kind))))
resv = mkName "res"
body = FunD name
[Clause [VarP resv] (NormalB $
(AppE
(AppE (VarE $ mkName "<#>") (VarE func))
(AppE (AppE (AppE (VarE 'fetchContext)
(LitE $ StringL field))
(VarE resv))
(LitE $ StringL collection)))) []]
in return [dec,body]
makeUpdate :: String
-> Name
-> String
-> String
-> Q [Dec]
makeUpdate n kind f c =
let field = LitE $ StringL f
coll = LitE $ StringL c
name = mkName $ "update" ++ firstUpper n
m = mkName "m"
dec = SigD name $ ForallT [PlainTV m] [AppT (ConT ''MonadIO) (VarT m)]
(AppT
(AppT ArrowT (ConT kind))
(AppT (AppT ArrowT (ConT ''ResT))
(AppT (AppT (ConT ''Action) (VarT m)) (ConT ''()))))
body = FunD name
[Clause [] (NormalB $
(AppE (AppE (VarE 'updateItem) coll)
field)) []]
in return [dec,body]