3
(SRC http://hackage.haskell.org/packages/archive/safecopy/0.6.1/doc/html/Data-SafeCopy.html)哈斯克爾safecopy例如
如果重命名聯繫人數據類型轉換數據Contacts_v0
type Name = String
type Address = String
data Contacts = Contacts [(Name, Address)]
instance SafeCopy Contacts where
putCopy (Contacts list) = contain $ safePut list
getCopy = contain $ Contacts <$> safeGet
如何Contacts_v0假設被分配到舊的現有數據?
type Name = String
type Address = String
type Phone = String
data Contacts_v0 = Contacts_v0 [(Name, Address)]
instance SafeCopy Contacts_v0 where
putCopy (Contacts_v0 list) = contain $ safePut list
getCopy = contain $ Contacts_v0 <$> safeGet
data Contact = Contact { name :: Name
, address :: Address
, phone :: Phone }
instance SafeCopy Contact where
putCopy Contact{..} = contain $ do safePut name; safePut address; safePut phone
getCopy = contain $ Contact <$> safeGet <*> safeGet <*> safeGet
data Contacts = Contacts [Contact]
instance SafeCopy Contacts where
version = 2
kind = extension
putCopy (Contacts contacts) = contain $ safePut contacts
getCopy = contain $ Contacts <$> safeGet
instance Migrate Contacts where
type MigrateFrom Contacts = Contacts_v0
migrate (Contacts_v0 contacts) = Contacts [ Contact{ name = name
, address = address
, phone = "" }
| (name, address) <- contacts ]
從上面的庫文檔我試圖做到這一點。
{-# LANGUAGE RecordWildCards, TypeFamilies #-}
import Control.Applicative
import Data.SafeCopy
type Name = String
type Address = String
type Phone = String
data Contacts = Contacts [(Name, Address)] deriving (Show)
instance SafeCopy Contacts where
putCopy (Contacts list) = contain $ safePut list
getCopy = contain $ Contacts <$> safeGet
data Contacts_v0 = Contacts_v0 [(Name, Address)] deriving (Show)
instance SafeCopy Contacts_v0 where
putCopy (Contacts_v0 list) = contain $ safePut list
getCopy = contain $ Contacts_v0 <$> safeGet
data Contact = Contact { name :: Name, address :: Address, phone :: Phone } deriving (Show)
instance SafeCopy Contact where
putCopy Contact{..} = contain $ do safePut name; safePut address; safePut phone
getCopy = contain $ Contact <$> safeGet <*> safeGet <*> safeGet
{-
data Contacts = Contacts [Contact]
instance SafeCopy Contacts where
version = 2
kind = extension
putCopy (Contacts contacts) = contain $ safePut contacts
getCopy = contain $ Contacts <$> safeGet
instance Migrate Contacts where
type MigrateFrom Contacts = Contacts_v0
migrate (Contacts_v0 contacts) = Contacts [ Contact{ name = name, address = address, phone = "" }
| (name, address) <- contacts ]
-}
main :: IO()
main = do
let test = Contacts [("gert","home")]
print test
--let testNew = how do you migrate test using migrate?
--print testNew
注意,它將使我更有意義,如果他們改名爲新的一個Contacts_v2代替重新命名舊的。
也許我應該改寫這個問題,何時safecopy有用?
什麼! -1爲什麼......? – 2012-08-09 18:27:05
你能否提出一個明確的問題,並在文中給出一些關於你的問題的介紹? – Tarrasch 2012-08-09 18:33:01
'Contacts_v0如何被分配到舊的已有數據?'不知道如何更清楚,在介紹中工作請稍候。 – 2012-08-09 18:58:08