diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2017-03-15 20:11:41 +0100 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2017-03-15 20:11:41 +0100 |
commit | 127d763a6a68d8fd242e093ba9f55bf769635842 (patch) | |
tree | a864c82c0476766cb61ae6c2d009581f4ef89d1e /Model.hs | |
parent | 7888d76bc45caed34cb3aa10824807d01057d746 (diff) | |
download | bar-127d763a6a68d8fd242e093ba9f55bf769635842.tar bar-127d763a6a68d8fd242e093ba9f55bf769635842.tar.gz bar-127d763a6a68d8fd242e093ba9f55bf769635842.tar.bz2 bar-127d763a6a68d8fd242e093ba9f55bf769635842.tar.xz bar-127d763a6a68d8fd242e093ba9f55bf769635842.zip |
Make typing many items more efficient
Diffstat (limited to 'Model.hs')
-rw-r--r-- | Model.hs | 35 |
1 files changed, 28 insertions, 7 deletions
@@ -10,6 +10,9 @@ import Control.Monad.Writer | |||
10 | import Data.Text (Text) | 10 | import Data.Text (Text) |
11 | import qualified Data.Text as Text | 11 | import qualified Data.Text as Text |
12 | 12 | ||
13 | import Data.Map (Map, (!)) | ||
14 | import qualified Data.Map as Map | ||
15 | |||
13 | import qualified Data.HashMap.Lazy as HashMap | 16 | import qualified Data.HashMap.Lazy as HashMap |
14 | 17 | ||
15 | import Data.Aeson | 18 | import Data.Aeson |
@@ -23,19 +26,24 @@ share [mkPersist sqlSettings, mkMigrate "migrateAll"] | |||
23 | $(persistFileWith lowerCaseSettings "config/models") | 26 | $(persistFileWith lowerCaseSettings "config/models") |
24 | 27 | ||
25 | class HasType a where | 28 | class HasType a where |
26 | getType :: ( BaseBackend backend ~ SqlBackend | 29 | fkType :: a -> Key Kind |
27 | , MonadIO m | 30 | |
28 | , PersistStoreRead backend | 31 | getType :: ( BaseBackend backend ~ SqlBackend |
29 | ) => a -> ReaderT backend m Kind | 32 | , MonadIO m |
33 | , PersistStoreRead backend | ||
34 | , HasType a | ||
35 | ) => a -> ReaderT backend m Kind | ||
36 | getType = getJust . fkType | ||
30 | 37 | ||
31 | instance HasType Item where | 38 | instance HasType Item where |
32 | getType = belongsToJust itemFkType | 39 | fkType = itemFkType |
33 | 40 | ||
34 | instance HasType Reference where | 41 | instance HasType Reference where |
35 | getType = belongsToJust referenceFkType | 42 | fkType = referenceFkType |
36 | 43 | ||
37 | instance HasType a => HasType (Entity a) where | 44 | instance HasType a => HasType (Entity a) where |
38 | getType Entity{..} = getType entityVal | 45 | fkType = fkType . entityVal |
46 | |||
39 | 47 | ||
40 | withType :: ( BaseBackend backend ~ SqlBackend | 48 | withType :: ( BaseBackend backend ~ SqlBackend |
41 | , MonadIO m | 49 | , MonadIO m |
@@ -44,6 +52,19 @@ withType :: ( BaseBackend backend ~ SqlBackend | |||
44 | ) => a -> ReaderT backend m (WithType a) | 52 | ) => a -> ReaderT backend m (WithType a) |
45 | withType val = (val `WithType`) . kindType <$> getType val | 53 | withType val = (val `WithType`) . kindType <$> getType val |
46 | 54 | ||
55 | withTypes :: ( BaseBackend backend ~ SqlBackend | ||
56 | , MonadIO m | ||
57 | , PersistStoreRead backend | ||
58 | , PersistQueryRead backend | ||
59 | , HasType a | ||
60 | , Functor f | ||
61 | ) => f a -> ReaderT backend m (f (WithType a)) | ||
62 | withTypes vals = do | ||
63 | typeMap <- Map.fromList . map (\(Entity kindKey kind) -> (kindKey, kind)) <$> selectList [] [] | ||
64 | let | ||
65 | lookupType x = (x `WithType`) . kindType $ typeMap ! fkType x | ||
66 | return $ lookupType <$> vals | ||
67 | |||
47 | instance Ord Item where | 68 | instance Ord Item where |
48 | x `compare` y = mconcat cmprs | 69 | x `compare` y = mconcat cmprs |
49 | where | 70 | where |