module Database.DynamoDB.TH (
mkTableDefs
, TableConfig(..)
, tableConfig
, defaultTranslate
, deriveCollection
, deriveEncodable
, RangeType(..)
) where
import Control.Lens (ix, over, (.~), (^.), _1, view)
import Control.Monad (forM_, when)
import Control.Monad.Trans.Class (lift)
import Control.Monad.Trans.Writer.Lazy (WriterT, execWriterT, tell)
import Data.Char (toUpper)
import Data.Function ((&))
import Data.Monoid ((<>))
import qualified Data.Text as T
import Data.HashMap.Strict (HashMap)
import Generics.SOP
import Generics.SOP.TH (deriveGenericOnly)
import Language.Haskell.TH
import Language.Haskell.TH.Syntax (Name (..), OccName (..))
import Network.AWS.DynamoDB.Types (attributeValue, avM, ProvisionedThroughput, StreamViewType)
import Network.AWS (MonadAWS)
import Database.DynamoDB.Class
import Database.DynamoDB.Migration (runMigration)
import Database.DynamoDB.Types
import Database.DynamoDB.Internal
data TableConfig = TableConfig {
tableSetup :: (Name, RangeType, String)
, globalIndexes :: [(Name, RangeType, String)]
, localIndexes :: [(Name, String)]
, translateField :: String -> String
}
tableConfig ::
(Name, RangeType)
-> [(Name, RangeType)]
-> [Name]
-> TableConfig
tableConfig (table, tbltype) globidx locidx =
TableConfig {
tableSetup = (table, tbltype, nameToStr table)
, globalIndexes = map (\(n,r) -> (n, r, nameToStr n)) globidx
, localIndexes = map (\n -> (n, nameToStr n)) locidx
, translateField = defaultTranslate
}
where
nameToStr (Name (OccName name) _) = name
defaultTranslate :: String -> String
defaultTranslate = translate
where
translate ('_':rest) = rest
translate name
| '_' `elem` name = drop 1 $ dropWhile (/= '_') name
| otherwise = name
mkTableDefs ::
String
-> TableConfig
-> Q [Dec]
mkTableDefs migname TableConfig{..} =
execWriterT $ do
let (table, tblrange, tblname) = tableSetup
tblFieldNames <- getFieldNames table translateField
let tblHashName = fst (head tblFieldNames)
buildColData tblFieldNames
genBaseCollection table tblrange tblname Nothing translateField
forM_ localIndexes $ \(idx, _) -> do
idxHashName <- (fst . head) <$> getFieldNames idx translateField
when (idxHashName /= tblHashName) $
fail ("Hash key " <> show idxHashName <> " in local index " <> show idx
<> " is not equal to table hash key " <> show tblHashName)
let allindexes = globalIndexes ++ map (\(idx,name) -> (idx, WithRange, name)) localIndexes
forM_ allindexes $ \(idx, idxrange, idxname) -> do
genBaseCollection idx idxrange idxname (Just table) translateField
instfields <- getFieldNames idx translateField
let pkeytable = [True | _ <- [1..(pkeySize idxrange)] ] ++ repeat False
forM_ (zip instfields pkeytable) $ \((fieldname, ltype), isKey) ->
case lookup fieldname tblFieldNames of
Just (AppT (ConT mbtype) inptype)
| mbtype == ''Maybe && inptype == ltype && isKey -> return ()
Just ptype
| ltype /= ptype -> fail $ "Record '" <> fieldname <> "' form index " <> show idx <> " has different type from table " <> show table
<> ": " <> show ltype <> " /= " <> show ptype
| otherwise -> return ()
Nothing ->
fail ("Record '" <> fieldname <> "' from index " <> show idx <> " is not in present in table " <> show table)
migfunc <- lift $ mkMigrationFunc migname table (map (view _1) globalIndexes) (map (view _1) localIndexes)
tell migfunc
pkeySize :: RangeType -> Int
pkeySize WithRange = 2
pkeySize NoRange = 1
genBaseCollection :: Name -> RangeType -> String -> Maybe Name -> (String -> String) -> WriterT [Dec] Q ()
genBaseCollection coll collrange tblname mparent translate = do
tblFieldNames <- getFieldNames coll translate
let fieldNames = map fst tblFieldNames
let fieldList = pure (ListE (map (AppE (VarE 'T.pack) . LitE . StringL) fieldNames))
primaryList' <- case (collrange, fieldNames) of
(NoRange, hashname:_) -> return [hashname]
(WithRange, h:r:_)-> return [h,r]
_ -> fail "Table must have at least 1/2 fields based on range key"
let primaryList = pure (ListE (map (AppE (VarE 'T.pack) . LitE . StringL) primaryList'))
let tbltype = maybe (PromotedT 'IsTable) (const $ PromotedT 'IsIndex) mparent
lift (deriveGenericOnly coll) >>= tell
lift [d|
instance DynamoCollection $(pure (ConT coll)) $(pure (ConT $ mrange collrange)) $(pure tbltype) where
allFieldNames _ = $(fieldList)
primaryFields _ = $(primaryList)
|] >>= tell
case mparent of
Nothing ->
lift [d|
instance DynamoTable $(pure (ConT coll)) $(pure (ConT $ mrange collrange)) where
tableName _ = $(pure $ AppE (VarE 'T.pack) (LitE (StringL tblname)))
|] >>= tell
Just parent ->
lift [d|
instance DynamoIndex $(pure (ConT coll)) $(pure (ConT parent)) $(pure (ConT $ mrange collrange)) where
indexName _ = $(pure $ AppE (VarE 'T.pack) (LitE (StringL tblname)))
|] >>= tell
let constrNames = mkConstrNames tblFieldNames
forM_ (drop (pkeySize collrange) constrNames) $ \constr ->
lift [d|
instance InCollection $(pure (ConT constr)) $(pure (ConT coll)) 'FullPath
|] >>= tell
where
mrange WithRange = 'WithRange
mrange NoRange = 'NoRange
getFieldNames :: Name -> (String -> String) -> WriterT [Dec] Q [(String, Type)]
getFieldNames tbl translate = do
info <- lift $ reify tbl
case getRecords info of
Left err -> fail $ "Table " <> show tbl <> ": " <> err
Right lst -> return $ map (over _1 translate) lst
where
getRecords :: Info -> Either String [(String, Type)]
#if __GLASGOW_HASKELL__ >= 800
getRecords (TyConI (DataD _ _ _ _ [RecC _ vars] _)) = Right $ map (\(Name (OccName rname) _,_,typ) -> (rname, typ)) vars
#else
getRecords (TyConI (DataD _ _ _ [RecC _ vars] _)) = Right $ map (\(Name (OccName rname) _,_,typ) -> (rname, typ)) vars
#endif
getRecords _ = Left "not a record declaration with 1 constructor"
toConstrName :: String -> String
toConstrName = ("P_" <>) . over (ix 0) toUpper
mkConstrNames :: [(String,a)] -> [Name]
mkConstrNames = map (mkName . toConstrName . fst)
buildColData :: [(String, Type)] -> WriterT [Dec] Q ()
buildColData fieldlist = do
let constrNames = mkConstrNames fieldlist
forM_ (zip fieldlist constrNames) $ \((fieldname, ltype), constr) -> do
let pat = mkName (toPatName fieldname)
#if __GLASGOW_HASKELL__ >= 800
say $ DataD [] constr [] Nothing [] []
#else
say $ DataD [] constr [] [] []
#endif
lift [d|
instance ColumnInfo $(pure (ConT constr)) where
columnName _ = T.pack fieldname
|] >>= tell
say $ SigD pat (AppT (AppT (AppT (ConT ''Column) ltype) (ConT 'TypColumn)) (ConT constr))
say $ ValD (VarP pat) (NormalB (VarE 'mkColumn)) []
where
toPatName = ("col" <> ) . over (ix 0) toUpper
say :: Monad m => t -> WriterT [t] m ()
say a = tell [a]
deriveCollection :: Name -> (String -> String) -> Q [Dec]
deriveCollection table translate =
execWriterT $ do
lift (deriveGenericOnly table) >>= tell
tblFieldNames <- getFieldNames table translate
buildColData tblFieldNames
deriveEncodable table translate
deriveEncodable :: Name -> (String -> String) -> WriterT [Dec] Q ()
deriveEncodable table translate = do
tblFieldNames <- getFieldNames table translate
let fieldList = pure (ListE (map (AppE (VarE 'T.pack) . LitE . StringL . fst) tblFieldNames))
lift [d|
instance DynamoEncodable $(pure (ConT table)) where
dEncode val = Just (attributeValue & avM .~ gsEncodeG $(fieldList) val)
dDecode (Just attr) = gsDecodeG $(fieldList) (attr ^. avM)
dDecode Nothing = Nothing
|] >>= tell
let constrs = mkConstrNames tblFieldNames
forM_ constrs $ \constr ->
lift [d|
instance InCollection $(pure (ConT constr)) $(pure (ConT table)) 'NestedPath
|] >>= tell
mkMigrationFunc :: String -> Name -> [Name] -> [Name] -> Q [Dec]
mkMigrationFunc name table globindexes locindexes = do
let glMap = ListE (map glIdxTemplate globindexes)
locMap = ListE (map locIdxTemplate locindexes)
let funcname = mkName name
m <- newName "m"
let signature = SigD funcname (ForallT [PlainTV m] [AppT (ConT ''MonadAWS) (VarT m)]
(AppT (AppT ArrowT (AppT (AppT (ConT ''HashMap) (ConT ''T.Text))
(ConT ''ProvisionedThroughput)))
(AppT (AppT ArrowT (AppT (ConT ''Maybe) (ConT ''StreamViewType)))
(AppT (VarT m) (TupleT 0)))))
return [signature, ValD (VarP funcname) (NormalB (AppE (AppE (AppE (VarE 'runMigration)
(SigE (ConE 'Proxy)
(AppT (ConT ''Proxy)
(ConT table)))) glMap) locMap)) []]
where
glIdxTemplate idx = AppE (VarE 'createGlobalIndex) (SigE (ConE 'Proxy) (AppT (ConT ''Proxy) (ConT idx)))
locIdxTemplate idx = AppE (VarE 'createLocalIndex) (SigE (ConE 'Proxy) (AppT (ConT ''Proxy) (ConT idx)))