acid-state / safecopy

An extension to Data.Serialize with built-in version control
60 stars 37 forks source link

Hello world is not complete #80

Closed yaitskov closed 4 years ago

yaitskov commented 4 years ago

Hi.

I am studying safecopy library and trying to execute hello world. I followed the example from haddock and cannot get deserialization working for a new type. Something is missing in example, which is not obvious.

GHCi, version 8.8.3

{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Main where

import GHC.Generics
import Data.SafeCopy
import Data.Serialize

data BaseT = BaseT Int deriving (Generic, Serialize, Show, Eq)

instance SafeCopy BaseT where
  version = 0
  kind = base
  putCopy (BaseT s) = contain $ safePut s
  getCopy = contain $ BaseT <$> safeGet

data NextT = NextT Int Int deriving (Generic, Serialize, Show, Eq)

instance SafeCopy NextT where
  version = 1
  kind = extension
  putCopy (NextT a b) = contain $ do safePut a ; safePut b
  getCopy = contain $ NextT <$> safeGet <*> safeGet

instance Migrate NextT where
  type MigrateFrom NextT = BaseT
  migrate (BaseT s) = NextT s s

*Main> (runGet safeGet (encode $ BaseT 888)) :: Either String NextT
Left "Failed reading: safecopy: Int: Cannot find getter associated with this version number: Version {unVersion = 888}\nEmpty call stack\n"
lemmih commented 4 years ago

The opposite of runGet safeGet is not encode, it is runPut safePut.

yaitskov commented 4 years ago

Thanks, @Lemmih