我正在尝试使用safecopy haskell库,但是当我尝试将字符串迁移到bytestring时,最后4个字符丢失,4'\ NULL'字符前置于字符串:
{-# LANGUAGE TemplateHaskell, DeriveDataTypeable, TypeFamilies #-}
import Data.SafeCopy
import Data.Acid
import Data.Typeable
import Control.Monad.State.Class
import Control.Monad.Reader.Class
data T = T { str :: String }
deriving (Show, Typeable)
getT :: Query T String
getT = fmap str ask
setT :: String -> Update T ()
setT str = put $ T str
deriveSafeCopy 0 'base ''T
makeAcidic ''T ['setT, 'getT]
main :: IO ()
main = do
state <- openLocalState (T "string set with default")
update state (SetT "string set with SetT")
str <- query state GetT
putStrLn str
此输出:string set with SetT
,但在此之后运行以下修改后的版本:
{-# LANGUAGE TemplateHaskell, DeriveDataTypeable, TypeFamilies #-}
import Data.SafeCopy
import Data.Acid
import Data.Typeable
import Control.Monad.State.Class
import Control.Monad.Reader.Class
import Data.ByteString.Char8 as B
data T_v0 = T_v0 String
deriving (Show, Typeable)
deriveSafeCopy 0 'base ''T_v0
data T = T { str :: B.ByteString }
deriving (Show, Typeable)
deriveSafeCopy 1 'extension ''T
instance Migrate T where
type MigrateFrom T = T_v0
migrate (T_v0 str) = T $ B.pack str
getT :: Query T B.ByteString
getT = fmap str ask
setT :: B.ByteString -> Update T ()
setT str = put $ T str
makeAcidic ''T ['setT, 'getT]
main :: IO ()
main = do
state <- openLocalState (T $ B.pack "bytestring set with default")
str <- query state GetT
print str
输出:"\NUL\NUL\NUL\NULstring set with "
。我不知道为什么会这样。我在迁移步骤中做错了吗?我试图尽可能地靠近example code。有谁知道为什么会这样?
P.S:对于大量的代码感到抱歉,但我想不出更好的方法来传达这个问题。
答案 0 :(得分:0)
这是因为acid-state
。您已经在混淆事务日志的实现之间更改了getT
和setT
的类型。在没有先使用createCheckpoint
清除事务日志(使用较旧版本的代码库)的情况下,您基本上无法更改使用makeAcidic
标记的任何函数。