packages feed

binary-conduit 1.2.2 → 1.2.3

raw patch · 3 files changed

+58/−4 lines, 3 filesdep ~binarydep ~bytestringdep ~conduitPVP ok

version bump matches the API change (PVP)

Dependency ranges changed: binary, bytestring, conduit

API changes (from Hackage documentation)

+ Data.Conduit.Serialization.Binary: conduitMsgEncode :: (Binary b, MonadThrow m) => Conduit b m ByteString

Files

Data/Conduit/Serialization/Binary.hs view
@@ -4,6 +4,7 @@ module Data.Conduit.Serialization.Binary   ( conduitDecode   , conduitEncode+  , conduitMsgEncode   , conduitGet   , conduitPut   , conduitPutList@@ -48,9 +49,30 @@ conduitDecode = conduitGet get  -- | Runs default encoder on a input stream.+--+-- This function produces a stream of bytes where for each input+-- value you will have a number of 'ByteString's, and no boundary+-- between different values.  conduitEncode :: (Binary b, MonadThrow m) => Conduit b m ByteString conduitEncode = CL.map put =$= conduitPut ++-- | Runs default encoder on input stream.+--+-- This function produces a ByteString per each incomming packet,+-- it may be useful in datagram based protocols.+-- Function maintains following property+--+-- >   'conduitMsgEncode' xs == 'CL.map' 'Data.ByteString.encode' =$= 'CL.map' 'LBS.toStrict'+--+-- This invariant is maintaind by the cost of additional data copy,+-- so if you packets can be serialized to the large data chunks or+-- you interested in iterative packet serialization+-- concider using 'conduitPutList' or 'conduitPutMany'+--+conduitMsgEncode :: (Binary b, MonadThrow m) => Conduit b m ByteString+conduitMsgEncode = CL.map put =$= conduitMsg+ -- | Runs getter repeatedly on a input stream. conduitGet :: MonadThrow m => Get b -> Conduit ByteString m b conduitGet g = start@@ -75,9 +97,13 @@                     Nothing -> return ();\                     Just x  -> do { yi ; conduit}} --- | Runs putter repeatedly on a input stream.+-- | Runs putter repeatedly on a input stream, returns an output stream. conduitPut :: MonadThrow m => Conduit Put m ByteString conduitPutGeneric(conduitPut, (sourcePut x $$ CL.mapM_ yield))++-- | Runs a putter repeatedly on a input stream, returns a packets.+conduitMsg :: MonadThrow m => Conduit Put m ByteString+conduitPutGeneric(conduitMsg, (yield (LBS.toStrict $ runPut x)))  -- | Runs putter repeatedly on a input stream. -- Returns a lazy butestring so it's possible to use vectorized
binary-conduit.cabal view
@@ -1,5 +1,5 @@ name:                binary-conduit-version:             1.2.2+version:             1.2.3 synopsis:            data serialization/deserialization conduit library description:         The binary-conduit package.       Allow binary serialization using iterative conduit interface.@@ -10,7 +10,7 @@ copyright:           2013 Alexander Vershilov category:            Conduit stability:           Experimental-homepage:            http://github.com/qnikst/binary-conduit+homepage:            http://github.com/qnikst/binary-conduit/ bug-reports:         http://github.com/qnikst/binary-conduit/issues build-type:          Simple cabal-version:       >=1.8@@ -19,7 +19,7 @@   exposed-modules:          Data.Conduit.Serialization.Binary   build-depends:       base >=4 && <5,-                       conduit >= 1.1 && < 1.2,+                       conduit >= 1.1 && < 1.3,                        bytestring >= 0.9 && < 10.3,                        binary >= 0.6 && < 0.8,                        vector >= 0.10,
test/Main.hs view
@@ -1,3 +1,5 @@+{-# LANGUAGE DeriveGeneric #-}+import Control.Applicative import Control.Monad (forM_, when) import Data.Binary import Data.Binary.Put@@ -13,6 +15,7 @@ import Test.QuickCheck.Monadic import Test.QuickCheck import Control.Monad.Trans.Resource +import GHC.Generics  -- | check conduitEncode =$= conduitDecode == id prop_eq :: (Binary a,Eq a) => [a] -> Property@@ -73,6 +76,23 @@                     Left _ -> fail "exception in conduit"                     Right a -> stop $ xs ?== a ++data A = A ByteString ByteString deriving (Eq, Show, Generic)++instance Binary A+instance Arbitrary A where+  arbitrary = A <$> fmap BS.pack arbitrary+                <*> fmap BS.pack arbitrary+++prop_eq_plus :: (Binary a, Eq a) => [a] -> Property+prop_eq_plus xs = monadicIO $ do+   x <- runExceptionT $ CL.sourceList xs $= CL.map encode =$= CL.map LBS.toStrict $$ CL.consume+   y <- runExceptionT $ CL.sourceList xs $= conduitMsgEncode $$ CL.consume+   case liftA2 (?==) x y of+     Left _  -> fail "exception in conduit"+     Right a -> stop a+ main = hspec $ do     describe "QC properties: conduitEncode =$= conduitDecode == id" $ do         prop "int"               $ (prop_eq :: [Int] -> Property)@@ -81,9 +101,16 @@         prop "either int string" $ (prop_eq :: [Either Int String] -> Property)         prop "(Int,Int)"         $ (prop_sink :: (Int,Int) -> Property)         prop "(String,String)"   $ (prop_sink :: (String,String) -> Property)+	prop "A"                 $ (prop_eq   :: [A] -> Property)     describe "QC properties partial lists" $ do         prop "break data in 2 parts" $ (prop_part2)         prop "break data in 3 parts" $ (prop_part3)+    describe "QC properites: CL.conduitMsgEncode returns a correct chunks" $ do+        prop "int"               $ (prop_eq_plus :: [Int] -> Property)+        prop "string"            $ (prop_eq_plus :: [String] -> Property)+        prop "maybe int"         $ (prop_eq_plus :: [Maybe Int] -> Property)+        prop "either int string" $ (prop_eq_plus :: [Either Int String] -> Property)+	prop "A"                 $ (prop_eq_plus :: [A] -> Property)     describe "HUnit properties:" $ do       it "decodes message splitted to chunks" $ do           let i = -32@@ -100,3 +127,4 @@           x' <- CL.sourceList [ls1,ls2] $= conduitDecode $$ CL.consume           x `shouldBe` is++is           x' `shouldBe` is+