implemented encodeRows

This commit is contained in:
Jonathan Lamothe 2022-04-24 15:17:52 -04:00
parent 5d6a7db6c5
commit 3df133147f
2 changed files with 26 additions and 1 deletions

View File

@ -33,6 +33,7 @@ module Data.CSV.Sip (
slurpRawLabelledCSV,
-- * Conduits
-- ** Encoding
encodeRows,
encodeRawRows,
-- ** Decoding
labelFields,
@ -60,7 +61,7 @@ import Data.Conduit.List (consume)
import qualified Data.Map as M
import Data.Maybe (fromMaybe)
import qualified Data.Text as T
import Data.Text.Encoding (decodeUtf8')
import Data.Text.Encoding (decodeUtf8', encodeUtf8)
import Data.Word (Word8)
-- | read an entire CSV file
@ -97,6 +98,11 @@ slurpRawLabelledCSV
slurpRawLabelledCSV file = runConduit $
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
-- represents a field, with the entire list representing a row
encodeRawRows :: Monad m => ConduitT [BS.ByteString] BS.ByteString m ()

View File

@ -33,6 +33,7 @@ import Data.CSV.Sip
spec :: Spec
spec = describe "Data.CSV.Sip" $ do
encodeRowsSpec
encodeRawRowsSpec
labelFieldsSpec
decodeRowsSpec
@ -40,6 +41,24 @@ spec = describe "Data.CSV.Sip" $ do
decodeUTF8Spec
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 = describe "encodeRawRows" $ do
result <- BS.concat <$> runConduit