Created
March 11, 2012 21:23
-
-
Save tazjin/2018257 to your computer and use it in GitHub Desktop.
SafeCopy instance?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# LANGUAGE DeriveDataTypeable, GeneralizedNewtypeDeriving, RecordWildCards, | |
| TemplateHaskell, TypeFamilies, OverloadedStrings #-} | |
| module Main where | |
| import Control.Applicative ((<$>), optional) | |
| import Control.Exception (bracket) | |
| import Control.Monad (msum, mzero) | |
| import Control.Monad.Reader (ask) | |
| import Control.Monad.State (get, put) | |
| import Control.Monad.Trans (liftIO) | |
| import Data.Acid | |
| import Data.Acid.Advanced | |
| import Data.Acid.Local | |
| import Data.ByteString (ByteString) | |
| import qualified Data.ByteString.Char8 as BS | |
| import Data.Data (Data, Typeable) | |
| import Data.IxSet (Indexable(..), IxSet(..), (@=), Proxy(..), getOne, ixFun, ixSet) | |
| import qualified Data.IxSet as IxSet | |
| import Data.SafeCopy (SafeCopy, base, deriveSafeCopy) | |
| import Data.Text (Text) | |
| import Data.Text.Lazy (toStrict) | |
| import qualified Data.Text as Text | |
| import Data.Time | |
| import Happstack.Server | |
| newtype PostId = PostId { unPostId :: Integer } | |
| deriving (Eq, Ord, Data, Enum, Typeable, SafeCopy) | |
| data Comment = Comment { | |
| cauthor :: Text, | |
| ctext :: Text, | |
| cdate :: UTCTime | |
| } deriving (Eq, Ord, Data, Typeable) | |
| data Entry = Entry { | |
| postId :: PostId, | |
| author :: Text, | |
| title :: Text, | |
| btext :: Text, | |
| mtext :: Text, | |
| edate :: UTCTime, | |
| tags :: [Text], | |
| comments :: [Comment] | |
| } deriving (Eq, Ord, Data, Typeable) | |
| -- ixSet requires different datatypes for field indexes, so let's define some | |
| newtype Author = Author Text deriving (Eq, Ord, Data, Typeable, SafeCopy) | |
| newtype Title = Title Text deriving (Eq, Ord, Data, Typeable, SafeCopy) | |
| newtype BText = BText Text deriving (Eq, Ord, Data, Typeable, SafeCopy) -- standard text | |
| newtype MText = MText Text deriving (Eq, Ord, Data, Typeable, SafeCopy) -- "read more" text | |
| newtype Tag = Tag Text deriving (Eq, Ord, Data, Typeable, SafeCopy) | |
| newtype EDate = EDate UTCTime deriving (Eq, Ord, Data, Typeable, SafeCopy) | |
| newtype SDate = SDate UTCTime deriving (Eq, Ord, Data, Typeable, SafeCopy) | |
| newtype Username = Username Text deriving (Eq, Ord, Data, Typeable, SafeCopy) | |
| newtype SessionID = SessionID Text deriving (Eq, Ord, Data, Typeable, SafeCopy) | |
| instance Indexable Entry where | |
| empty = ixSet [ ixFun $ \e -> [ postId e] | |
| , ixFun $ \e -> [ Author $ author e ] | |
| , ixFun $ \e -> [ Title $ title e] | |
| , ixFun $ \e -> [ BText $ btext e] | |
| , ixFun $ \e -> [ MText $ mtext e] | |
| , ixFun $ \e -> [ EDate $ edate e] | |
| , ixFun $ \e -> map Tag (tags e) | |
| , ixFun $ comments | |
| ] | |
| data User = User { | |
| username :: Text, | |
| password :: ByteString | |
| } deriving (Eq, Ord, Data, Typeable) | |
| data Session = Session { | |
| sessionID :: Text, | |
| user :: User, | |
| sdate :: UTCTime | |
| } deriving (Eq, Ord, Data, Typeable) | |
| instance Indexable User where | |
| empty = ixSet [ ixFun $ \u -> [Username $ username u] | |
| , ixFun $ (:[]) . password | |
| ] | |
| instance Indexable Session where | |
| empty = ixSet [ ixFun $ \s -> [SessionID $ sessionID s] | |
| , ixFun $ (:[]) . user | |
| , ixFun $ \s -> [SDate $ sdate s] | |
| ] | |
| data Blog = Blog { | |
| blogSessions :: IxSet Session, | |
| blogUsers :: IxSet User, | |
| blogEntries :: IxSet Entry | |
| } | |
| $(deriveSafeCopy 0 'base ''Blog) | |
| initialBlogState :: Blog | |
| initialBlogState = | |
| Blog { blogSessions = empty | |
| , blogUsers = empty | |
| , blogEntries = empty } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| *Main> :l Acid | |
| [1 of 1] Compiling Main ( Acid.hs, interpreted ) | |
| Acid.hs:98:3: | |
| No instances for (SafeCopy Session, SafeCopy User, SafeCopy Entry) | |
| arising from a use of `safecopy-0.6.1:Data.SafeCopy.SafeCopy.getSafePut' | |
| Possible fix: | |
| add instance declarations for | |
| (SafeCopy Session, SafeCopy User, SafeCopy Entry) | |
| In a stmt of a 'do' block: | |
| safePut_IxSetSession_a8lA <- safecopy-0.6.1:Data.SafeCopy.SafeCopy.getSafePut | |
| In the first argument of `safecopy-0.6.1:Data.SafeCopy.SafeCopy.contain', namely | |
| `do { safePut_IxSetSession_a8lA <- safecopy-0.6.1:Data.SafeCopy.SafeCopy.getSafePut; | |
| safePut_IxSetUser_a8lB <- safecopy-0.6.1:Data.SafeCopy.SafeCopy.getSafePut; | |
| safePut_IxSetEntry_a8lC <- safecopy-0.6.1:Data.SafeCopy.SafeCopy.getSafePut; | |
| safePut_IxSetSession_a8lA arg_a8lx; | |
| .... }' | |
| In the expression: | |
| safecopy-0.6.1:Data.SafeCopy.SafeCopy.contain | |
| (do { safePut_IxSetSession_a8lA <- safecopy-0.6.1:Data.SafeCopy.SafeCopy.getSafePut; | |
| safePut_IxSetUser_a8lB <- safecopy-0.6.1:Data.SafeCopy.SafeCopy.getSafePut; | |
| safePut_IxSetEntry_a8lC <- safecopy-0.6.1:Data.SafeCopy.SafeCopy.getSafePut; | |
| safePut_IxSetSession_a8lA arg_a8lx; | |
| .... }) | |
| Failed, modules loaded: none. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment