implemented encodeRows
This commit is contained in:
parent
5d6a7db6c5
commit
3df133147f
|
@ -33,6 +33,7 @@ module Data.CSV.Sip (
|
||||||
slurpRawLabelledCSV,
|
slurpRawLabelledCSV,
|
||||||
-- * Conduits
|
-- * Conduits
|
||||||
-- ** Encoding
|
-- ** Encoding
|
||||||
|
encodeRows,
|
||||||
encodeRawRows,
|
encodeRawRows,
|
||||||
-- ** Decoding
|
-- ** Decoding
|
||||||
labelFields,
|
labelFields,
|
||||||
|
@ -60,7 +61,7 @@ import Data.Conduit.List (consume)
|
||||||
import qualified Data.Map as M
|
import qualified Data.Map as M
|
||||||
import Data.Maybe (fromMaybe)
|
import Data.Maybe (fromMaybe)
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
import Data.Text.Encoding (decodeUtf8')
|
import Data.Text.Encoding (decodeUtf8', encodeUtf8)
|
||||||
import Data.Word (Word8)
|
import Data.Word (Word8)
|
||||||
|
|
||||||
-- | read an entire CSV file
|
-- | read an entire CSV file
|
||||||
|
@ -97,6 +98,11 @@ slurpRawLabelledCSV
|
||||||
slurpRawLabelledCSV file = runConduit $
|
slurpRawLabelledCSV file = runConduit $
|
||||||
sourceFile file .| decodeRawRows .| labelFields .|consume
|
sourceFile file .| decodeRawRows .| labelFields .|consume
|
||||||
|
|
||||||
|
-- | encode a CSV stream row by row, each element in the list read
|
||||||
|
-- represents a field, with the entire list representing a row
|
||||||
|
encodeRows :: Monad m => ConduitT [T.Text] BS.ByteString m ()
|
||||||
|
encodeRows = mapC (map encodeUtf8) .| encodeRawRows
|
||||||
|
|
||||||
-- | encode raw CSV stream row by row, each element in the list read
|
-- | encode raw CSV stream row by row, each element in the list read
|
||||||
-- represents a field, with the entire list representing a row
|
-- represents a field, with the entire list representing a row
|
||||||
encodeRawRows :: Monad m => ConduitT [BS.ByteString] BS.ByteString m ()
|
encodeRawRows :: Monad m => ConduitT [BS.ByteString] BS.ByteString m ()
|
||||||
|
|
|
@ -33,6 +33,7 @@ import Data.CSV.Sip
|
||||||
|
|
||||||
spec :: Spec
|
spec :: Spec
|
||||||
spec = describe "Data.CSV.Sip" $ do
|
spec = describe "Data.CSV.Sip" $ do
|
||||||
|
encodeRowsSpec
|
||||||
encodeRawRowsSpec
|
encodeRawRowsSpec
|
||||||
labelFieldsSpec
|
labelFieldsSpec
|
||||||
decodeRowsSpec
|
decodeRowsSpec
|
||||||
|
@ -40,6 +41,24 @@ spec = describe "Data.CSV.Sip" $ do
|
||||||
decodeUTF8Spec
|
decodeUTF8Spec
|
||||||
toBytesSpec
|
toBytesSpec
|
||||||
|
|
||||||
|
encodeRowsSpec :: Spec
|
||||||
|
encodeRowsSpec = describe "encodeRows" $ do
|
||||||
|
result <- BS.concat <$> runConduit
|
||||||
|
(sourceList input .| encodeRows .| consume)
|
||||||
|
it ("shouldBe " ++ show expected) $
|
||||||
|
result `shouldBe` expected
|
||||||
|
where
|
||||||
|
|
||||||
|
input =
|
||||||
|
[ [ "foo", "a\"b" ]
|
||||||
|
, [ "a\rb", "a\nb" ]
|
||||||
|
]
|
||||||
|
|
||||||
|
expected = BS.concat
|
||||||
|
[ "\"foo\",\"a\"\"b\"\r\n"
|
||||||
|
, "\"a\rb\",\"a\nb\"\r\n"
|
||||||
|
]
|
||||||
|
|
||||||
encodeRawRowsSpec :: Spec
|
encodeRawRowsSpec :: Spec
|
||||||
encodeRawRowsSpec = describe "encodeRawRows" $ do
|
encodeRawRowsSpec = describe "encodeRawRows" $ do
|
||||||
result <- BS.concat <$> runConduit
|
result <- BS.concat <$> runConduit
|
||||||
|
|
Loading…
Reference in New Issue
Block a user