Safecopy transition from string to bytestring

I am trying to use safecopy library haskell, but when I try to cast a string to a byte string, the last 4 characters are lost and 4 '\ nULL' characters are appended to the string:

{-# 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

      

This outputs:, string set with SetT

but after that after the next modification version:

{-# 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

      

it displays: "\NUL\NUL\NUL\NULstring set with "

. I have no idea why this is happening. Did I do something wrong during the migration stage? I tried to get as close as possible to the example code . Does anyone know why this is happening?

PS: Sorry for the large amount of code, but I couldn't think of a better way to convey the problem.

+3


source to share


1 answer


This is because of acid-state

. You have changed types getT

and setT

between implementations that interfere with the transaction log. You practically cannot change any tagged functions makeAcidic

without clearing the transaction log with createCheckpoint

(using an older version of the codebase).



0


source







All Articles